Skip to content

Commit

Permalink
improve handling (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmbarbone committed Dec 4, 2023
1 parent 87a8461 commit a6ecd18
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions R/namespace.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ require_namespace <- function(package, ...) {

for (i in seq_along(package)) {
do_require_namespace(
package = splits[[i]][1],
version = splits[[i]][2],
package = splits[[i]][1L],
version = splits[[i]][2L],
operator = comparisons[[i]]
)
}
Expand All @@ -27,6 +27,8 @@ require_namespace <- function(package, ...) {
# helpers -----------------------------------------------------------------

do_require_namespace <- function(package, version, operator) {
package <- trimws(package)

tryCatch(
loadNamespace(package),
packageNotFoundError = function(e) {
Expand All @@ -39,7 +41,7 @@ do_require_namespace <- function(package, version, operator) {
}

operator <- match.arg(trimws(operator), c(">", ">=", "==", "<=", "<"))
version <- as.package_version(version)
version <- as.package_version(trimws(version))

package_version <- function(package) {
# already loaded the namespace
Expand All @@ -52,38 +54,37 @@ do_require_namespace <- function(package, version, operator) {
)
}

if (switch(
found <- package_version(package)
if (!switch(
operator,
`>` = package_version(package) > version,
`>=` = package_version(package) >= version,
`==` = package_version(package) == version,
`<=` = package_version(package) <= version,
`<` = package_version(package) < version,
`>` = found > version,
`>=` = found >= version,
`==` = found == version,
`<=` = found <= version,
`<` = found < version,
any = TRUE
)) {
stop(cond_namespace_version(package, version, operator))
stop(cond_namespace_version(package, version, operator, found))
}
}

# conditions --------------------------------------------------------------

cond_namespace <- function(package) {
new_condition(
msg = sprintf(
"No package found called '%s'",
as.character(package)
),
msg = sprintf("No package found called '%s'", as.character(package)),
class = "namespace"
)
}

cond_namespace_version <- function(package, version, operator) {
cond_namespace_version <- function(package, version, operator, found) {
new_condition(
msg = sprintf(
"Package '%s' not found with version %s%s",
"Package version requirement not meet:\n%s: %s %s %s",
package,
format(version),
operator,
format(version)
format(found)
),
class = "namespace_version"
)
Expand Down

0 comments on commit a6ecd18

Please sign in to comment.