Skip to content

Commit

Permalink
avoid code duplicstion
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Oct 24, 2024
1 parent e8c1f16 commit 55d0a98
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions R/eval-walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,19 @@ ensure_named <- function(pos,
error_arg = NULL,
call = caller_env()) {
check_empty(pos, allow_empty, error_arg, call = call)


if (!allow_rename && any(names2(pos) != "")) {
if (is.null(error_arg)) {
cli::cli_abort(
"Can't rename variables in this context.",
class = "tidyselect:::error_disallowed_rename",
call = call
)
} else {
cli::cli_abort(c(
"Can't rename variables in this context.",
i = "{.arg {error_arg}} can't be renamed."
),
class = "tidyselect:::error_disallowed_rename",
call = call
)
msg <- "Can't rename variables in this context."
# Add more context if error_arg is supplied.
if (!is.null(error_arg)) {
msg <- c(msg, "i" = "{.arg {error_arg}} can't be renamed.")
}

cli::cli_abort(
msg,
class = "tidyselect:::error_disallowed_rename",
call = call
)
}

nms <- names(pos) <- names2(pos)
Expand All @@ -143,18 +138,15 @@ ensure_named <- function(pos,
check_empty <- function(x, allow_empty = TRUE, error_arg = NULL, call = caller_env()) {
if (!allow_empty && length(x) == 0) {
if (is.null(error_arg)) {
cli::cli_abort(
"Must select at least one item.",
call = call,
class = "tidyselect_error_empty_selection"
)
msg <- "Must select at least one item."
} else {
cli::cli_abort(
"{.arg {error_arg}} must select at least one column.",
call = call,
class = "tidyselect_error_empty_selection"
)
msg <- "{.arg {error_arg}} must select at least one column."
}
cli::cli_abort(
msg,
call = call,
class = "tidyselect_error_empty_selection"
)
}
}

Expand Down

0 comments on commit 55d0a98

Please sign in to comment.