Skip to content

Commit

Permalink
feat: use ympes for some assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
TimTaylor committed May 31, 2024
1 parent 52a3952 commit ab28513
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 60 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Imports:
dplyr (>= 1.1.0),
tidyselect,
rlang,
vctrs
vctrs,
ympes (>= 1.3.0)
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Suggests:
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ importFrom(dplyr,summarise)
importFrom(pillar,tbl_sum)
importFrom(tibble,as_tibble)
importFrom(tidyr,nest)
importFrom(ympes,assert_bool)
importFrom(ympes,assert_scalar_character)
importFrom(ympes,assert_scalar_numeric_not_na)
importFrom(ympes,assert_scalar_whole)
87 changes: 28 additions & 59 deletions R/assertions.R
Original file line number Diff line number Diff line change
@@ -1,69 +1,38 @@
.stop <- function(msg, ..., .call = sys.call(-1L), .subclass = NULL) {
stop(errorCondition(msg, ..., class = .subclass, call = .call[1L]))
}

.assert_not_missing <- function(x, ..., .arg, .call, .subclass) {
if (missing(x)) {
msg <- sprintf("argument `%s` is missing, with no default.", .arg)
.stop(msg, .call = .call, .subclass = .subclass)
}
}

.assert_bool <- function(
x,
...,
.arg = deparse(substitute(x)),
.call = sys.call(-1L),
.subclass = NULL
) {
.assert_not_missing(x, ..., .arg = .arg, .call = .call, .subclass = .subclass)
if (!(is.logical(x) && length(x) == 1L) || is.na(x)) {
msg <- sprintf("`%s` must be boolean (TRUE/FALSE).", .arg)
.stop(msg, ..., .call = .call, .subclass = .subclass)
}
.assert_bool <- function(x) {
assert_bool(
x,
.arg = deparse(substitute(x)),
.call = sys.call(-1L),
.subclass = NULL
)
}

# -------------------------------------------------------------------------
.assert_scalar_character <- function(
x,
...,
.arg = deparse(substitute(x)),
.call = sys.call(-1L),
.subclass = NULL
) {
.assert_not_missing(x, ..., .arg = .arg, .call = .call, .subclass = .subclass)
if (!(is.character(x) && length(x) == 1L)) {
msg <- sprintf("`%s` must be a character vector of length 1.", .arg)
.stop(msg, ..., .call = .call, .subclass = .subclass)
}
.assert_scalar_character <- function(x) {
assert_scalar_character(
x,
.arg = deparse(substitute(x)),
.call = sys.call(-1L),
.subclass = NULL
)
}

# -------------------------------------------------------------------------
.assert_scalar_numeric_not_na <- function(
x,
...,
.arg = deparse(substitute(x)),
.call = sys.call(-1L),
.subclass = NULL
) {
.assert_not_missing(x, ..., .arg = .arg, .call = .call, .subclass = .subclass)
if (!(is.numeric(x) && length(x) == 1L) || is.na(x)) {
msg <- sprintf("`%s` must be a numeric vector of length 1 and not NA.", .arg)
.stop(msg, ..., .call = .call, .subclass = .subclass)
}
.assert_scalar_numeric_not_na <- function(x) {
assert_scalar_numeric_not_na(
x,
.arg = deparse(substitute(x)),
.call = sys.call(-1L),
.subclass = NULL
)
}

# -------------------------------------------------------------------------
.assert_scalar_whole <- function(
x,
...,
.arg = deparse(substitute(x)),
.call = sys.call(-1L),
.subclass = NULL
) {
.assert_not_missing(x, ..., .arg = .arg, .call = .call, .subclass = .subclass)
if (!.is_scalar_whole(x)) {
msg <- sprintf("`%s` must be integerish and of length 1.", .arg)
.stop(msg, ..., .call = .call, .subclass = .subclass)
}
.assert_scalar_whole <- function(x) {
assert_scalar_whole(
x,
.arg = deparse(substitute(x)),
.call = sys.call(-1L),
.subclass = NULL
)
}
1 change: 1 addition & 0 deletions R/incidence2-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ NULL

#' @rawNamespace import(data.table, except = c(isoweek, year))
#' @import grates
#' @importFrom ympes assert_bool assert_scalar_character assert_scalar_numeric_not_na assert_scalar_whole
NULL

0 comments on commit ab28513

Please sign in to comment.