R/strict.R

Defines functions in_strict_mode check_ambiguous_strict check_nonexistent_strict strict_validate_invalid

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

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)
}

Try the clock package in your browser

Any scripts or data that you put into this service are public.

clock documentation built on May 31, 2023, 9:39 p.m.