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
0 commit comments