File: strict.R

package info (click to toggle)
r-cran-clock 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,856 kB
  • sloc: cpp: 19,564; sh: 17; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,736 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# ------------------------------------------------------------------------------

strict_validate_invalid <- function(invalid) {
  if (!is_null(invalid)) {
    return(invalid)
  }

  if (in_strict_mode()) {
    abort(
      paste0(
        "The global option, `clock.strict`, is currently set to `TRUE`. ",
        "In this mode, `invalid` must be set and cannot be left as `NULL`."
      )
    )
  }

  "error"
}

# ------------------------------------------------------------------------------

check_nonexistent_strict <- function(nonexistent, call = caller_env()) {
  if (!is_null(nonexistent)) {
    return(nonexistent)
  }

  if (in_strict_mode()) {
    message <- paste0(
      "The global option, `clock.strict`, is currently set to `TRUE`. ",
      "In this mode, `nonexistent` must be set and cannot be left as `NULL`."
    )
    abort(message, call = call)
  }

  "error"
}

# ------------------------------------------------------------------------------

check_ambiguous_strict <- function(ambiguous, call = caller_env()) {
  if (!is_null(ambiguous)) {
    return(ambiguous)
  }

  if (in_strict_mode()) {
    message <- paste0(
      "The global option, `clock.strict`, is currently set to `TRUE`. ",
      "In this mode, `ambiguous` must be set and cannot be left as `NULL`. ",
      "Additionally, `ambiguous` cannot be set to a zoned-time or POSIXct ",
      "unless it is paired with an ambiguous time resolution strategy, like: ",
      "`list(<zoned-time>, 'earliest')`."
    )
    abort(message, call = call)
  }

  "error"
}

# ------------------------------------------------------------------------------

in_strict_mode <- function() {
  strict <- getOption("clock.strict", default = FALSE)
  is_true(strict)
}