Skip to content

Commit

Permalink
remove dots, add optional
Browse files Browse the repository at this point in the history
  • Loading branch information
kbvernon committed Aug 1, 2024
1 parent 2ca514b commit e737f7f
Showing 1 changed file with 50 additions and 19 deletions.
69 changes: 50 additions & 19 deletions R/use_crate.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#'
#' Analogous to `usethis::use_package()` but for crate dependencies.
#'
#' @param crate a character scalar, the name of the crate to add
#' @param features a character vector, a list of features to include from the
#' @param crate character scalar, the name of the crate to add
#' @param features character vector, a list of features to include from the
#' crate
#' @param git a character scalar, the URL of the Github repository
#' @param version a character scalar, the version of the crate to add
#' @param path a character scalar, the package directory
#' @param ... additional options to include
#' @param git character scalar, the URL of the Github repository
#' @param version character scalar, the version of the crate to add
#' @param optional boolean scalar, whether to mark the dependency as optional

Check warning on line 10 in R/use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_crate.R,line=10,col=78,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' (FALSE by default)
#' @param path character scalar, the package directory
#'
#' @details
#' For a list of all available options, see \href{https://doc.rust-lang.org/cargo/commands/cargo-add.html}{Cargo docs}
#' For more details regarding these and other options, see the

Check warning on line 15 in R/use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_crate.R,line=15,col=63,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' \href{https://doc.rust-lang.org/cargo/commands/cargo-add.html}{Cargo docs}
#' for `cargo-add`.
#'
#' @return `NULL`, invisibly
Expand All @@ -31,22 +33,31 @@
#'
#' # add to [dependencies] with specific version
#' use_crate("serde", version = "1.0.1")
#'

Check warning on line 36 in R/use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_crate.R,line=36,col=3,[trailing_whitespace_linter] Trailing whitespace is superfluous.
#' # add to [dependencies] with optional compilation
#' use_crate("serde", optional = TRUE)
#' }
use_crate <- function(

Check warning on line 40 in R/use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_crate.R,line=40,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 15, this has 18.
crate,

Check warning on line 41 in R/use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_crate.R,line=41,col=4,[indentation_linter] Indentation should be 2 spaces but is 4 spaces.
features = NULL,
git = NULL,
version = NULL,
optional = FALSE,
path = ".",
...
){

Check warning on line 48 in R/use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_crate.R,line=48,col=2,[brace_linter] There should be a space before an opening curly brace.

Check warning on line 48 in R/use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_crate.R,line=48,col=2,[paren_body_linter] There should be a space between a right parenthesis and a body expression.

# check crate
if (!rlang::is_character(crate) && length(crate) != 1){

Check warning on line 51 in R/use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_crate.R,line=51,col=57,[brace_linter] There should be a space before an opening curly brace.

Check warning on line 51 in R/use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=R/use_crate.R,line=51,col=57,[paren_body_linter] There should be a space between a right parenthesis and a body expression.

info <- paste(
"You supplied an object of class {class(crate)[1]}",
"with length {length(crate)}."
)

cli::cli_abort(
"{.var crate} should be a length one character vector.",
"i" = "You supplied an object of class {class(crate)} with length {length(crate)}.",
"i" = info,
class = "rextendr_error"
)

Expand All @@ -57,7 +68,7 @@ use_crate <- function(

cli::cli_abort(
"{.var features} should be a character vector.",
"i" = "You supplied an object of class {class(features)}.",
"i" = "You supplied an object of class {class(features)[1]}.",
class = "rextendr_error"
)

Expand All @@ -68,9 +79,14 @@ use_crate <- function(

if (!rlang::is_character(git) && length(git) != 1){

info <- paste(
"You supplied an object of class {class(git)[1]}",
"with length {length(git)}."
)

cli::cli_abort(
"{.var git} should be a length one character vector.",
"i" = "You supplied an object of class {class(git)} with length {length(git)}.",
"i" = info,
class = "rextendr_error"
)

Expand All @@ -85,9 +101,14 @@ use_crate <- function(

if (!rlang::is_character(version) && length(version) != 1){

info <- paste(
"You supplied an object of class {class(version)}",
"with length {length(version)}."
)

cli::cli_abort(
"{.var version} should be a length one character vector.",
"i" = "You supplied an object of class {class(version)} with length {length(version)}.",
"i" = info,
class = "rextendr_error"
)

Expand All @@ -97,19 +118,29 @@ use_crate <- function(

}

#check optional
if (!rlang::is_bool(optional) && length(optional) != 1){

info <- paste(
"You supplied an object of class {class(optional)[1]}",
"with length {length(optional)}."
)

cli::cli_abort(
"{.var optional} should be a length one boolean vector.",
"i" = info,
class = "rextendr_error"
)

}

# combine main options
cargo_add_opts <- list(
"--features" = features,
"--git" = git
"--git" = git,
"--optional" = tolower(as.character(optional))
)

# add additional options from ...
lst <- rlang::dots_list(...)

if (length(lst) > 0){ names(lst) <- paste0("--", names(lst)) }

cargo_add_opts <- c(cargo_add_opts, lst)

# clear empty options
cargo_add_opts <- Filter(length, cargo_add_opts)

Expand Down

0 comments on commit e737f7f

Please sign in to comment.