Skip to content

Commit 8be222e

Browse files
Copilotkrlmlr
andcommitted
Replace is_toggle() and check_needle() with rlang standalone types-check
Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
1 parent b84d86b commit 8be222e

File tree

5 files changed

+96
-13
lines changed

5 files changed

+96
-13
lines changed

R/drive_examples.R

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ many_files <- function(needle, haystack, where = c("local", "remote")) {
7979
out <- haystack
8080

8181
if (!missing(needle)) {
82-
check_needle(needle)
82+
check_string(needle, arg = "matches")
8383
sel <- grepl(needle, haystack$name, ignore.case = TRUE)
8484
if (!any(sel)) {
8585
drive_abort(
@@ -142,11 +142,4 @@ remote_example_files <- function() {
142142
env_get(.googledrive, "remote_example_files")
143143
}
144144

145-
check_needle <- function(needle) {
146-
if (is_string(needle)) {
147-
return()
148-
}
149-
drive_abort(c(
150-
"{.arg matches} must be a string, not {.cls class(needle)}"
151-
))
152-
}
145+

R/drive_find.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ drive_find <- function(
141141
if (!is.null(pattern) && !(is_string(pattern))) {
142142
drive_abort("{.arg pattern} must be a character string.")
143143
}
144-
stopifnot(is_toggle(trashed))
144+
check_bool(trashed)
145145
stopifnot(is.numeric(n_max), n_max >= 0, length(n_max) == 1)
146146

147147
if (lifecycle::is_present(team_drive)) {

R/standalone-types-check.R

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# ---
2+
# repo: r-lib/rlang
3+
# file: standalone-types-check.R
4+
# last-updated: 2023-03-13
5+
# license: https://unlicense.org
6+
# dependencies: none (simplified version for googledrive)
7+
# imports: rlang (>= 1.1.0)
8+
# ---
9+
#
10+
# This is a simplified version of the rlang standalone types-check
11+
# focusing only on the functions needed by googledrive
12+
13+
# nocov start
14+
15+
check_bool <- function(
16+
x,
17+
...,
18+
allow_na = TRUE,
19+
allow_null = FALSE,
20+
arg = rlang::caller_arg(x),
21+
call = rlang::caller_env()
22+
) {
23+
if (!missing(x)) {
24+
if (allow_null && is.null(x)) {
25+
return(invisible(NULL))
26+
}
27+
if (length(x) == 1L && is.logical(x)) {
28+
if (allow_na || !is.na(x)) {
29+
return(invisible(NULL))
30+
}
31+
}
32+
}
33+
34+
# Create error message based on what we allow
35+
expected <- "`TRUE` or `FALSE`"
36+
if (allow_na) {
37+
expected <- paste(expected, "or `NA`")
38+
}
39+
if (allow_null) {
40+
expected <- paste(expected, "or `NULL`")
41+
}
42+
43+
rlang::abort(
44+
message = paste0("`", arg, "` must be ", expected, "."),
45+
...,
46+
call = call
47+
)
48+
}
49+
50+
check_string <- function(
51+
x,
52+
...,
53+
allow_empty = TRUE,
54+
allow_na = FALSE,
55+
allow_null = FALSE,
56+
arg = rlang::caller_arg(x),
57+
call = rlang::caller_env()
58+
) {
59+
if (!missing(x)) {
60+
if (allow_null && is.null(x)) {
61+
return(invisible(NULL))
62+
}
63+
if (length(x) == 1L && is.character(x)) {
64+
if (allow_na && is.na(x)) {
65+
return(invisible(NULL))
66+
}
67+
if (!is.na(x) && (allow_empty || nzchar(x))) {
68+
return(invisible(NULL))
69+
}
70+
}
71+
}
72+
73+
# Create error message
74+
expected <- "a single string"
75+
if (!allow_empty) {
76+
expected <- "a single non-empty string"
77+
}
78+
if (allow_na) {
79+
expected <- paste(expected, "or `NA`")
80+
}
81+
if (allow_null) {
82+
expected <- paste(expected, "or `NULL`")
83+
}
84+
85+
rlang::abort(
86+
message = paste0("`", arg, "` must be ", expected, "."),
87+
...,
88+
call = call
89+
)
90+
}
91+
92+
# nocov end

R/utils-paths.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ check_for_overwrite <- function(parent = NULL, name, overwrite) {
8484
}
8585

8686
overwrite_hits <- function(parent = NULL, name, overwrite) {
87-
stopifnot(is_toggle(overwrite))
87+
check_bool(overwrite)
8888
if (is.na(overwrite)) {
8989
return(invisible())
9090
}

R/utils.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
isFALSE <- function(x) identical(x, FALSE)
22

3-
is_toggle <- function(x) length(x) == 1L && is.logical(x)
4-
53
last <- function(x) pluck(x, length(x))
64

75
escape_regex <- function(x) {

0 commit comments

Comments
 (0)