Skip to content

Commit

Permalink
fix processx and cargo issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kbvernon committed Nov 10, 2024
1 parent e24b7b3 commit c3a1bbc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
39 changes: 20 additions & 19 deletions R/use_crate.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,32 @@ use_crate <- function(
check_bool(optional)
check_string(path)

if (!is.null(version) && !is.null(git)) {
cli::cli_abort(
"Cannot specify a git URL ('{git}') with a version ('{version}')."
)
}

if (!is.null(version)) {
crate <- paste0(crate, "@", version)
}

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

# clear empty options
is_null_or_empty_string <- function(x) {
!nzchar(x) || rlang::is_empty(x)
if (!is.null(features)) {
features <- c(
"--features",
paste(crate, features, sep = "/", collapse = ",")
)
}

cargo_add_opts <- purrr::discard(cargo_add_opts, is_null_or_empty_string)
if (!is.null(git)) {
git <- c("--git", git)
}

# combine option names and values into single strings
adtl_args <- unname(purrr::imap_chr(
cargo_add_opts,
function(x, i) {
paste(i, paste0(x, collapse = " "))
}
))
if (optional) {
optional <- "--optional"
} else {
optional <- NULL
}

# get rust directory in project folder
root <- rprojroot::find_package_root_file(path = path)
Expand All @@ -90,7 +91,7 @@ use_crate <- function(
# run the commmand
processx::run(
"cargo",
c("add", crate, adtl_args),
c("add", crate, features, git),
echo_cmd = TRUE,
wd = rust_folder
)
Expand Down
8 changes: 3 additions & 5 deletions tests/testthat/test-use_crate.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ test_that("use_crate() adds dependency to package or workspace", {

use_crate(
"serde",
features = "derive",
git = "https://github.com/serde-rs/serde",
version = "1.0.1"
features = "derive",

Check warning on line 13 in tests/testthat/test-use_crate.R

View workflow job for this annotation

GitHub Actions / lint

file=tests/testthat/test-use_crate.R,line=13,col=25,[trailing_whitespace_linter] Trailing whitespace is superfluous.
version = "1.0.1",
path = path
)

metadata <- read_cargo_metadata(path)
Expand All @@ -24,7 +24,5 @@ test_that("use_crate() adds dependency to package or workspace", {

expect_equal(dependency[["features"]], "derive")

expect_equal(dependency[["source"]], "git+https://github.com/serde-rs/serde")

expect_equal(dependency[["req"]], "^1.0.1")
})

0 comments on commit c3a1bbc

Please sign in to comment.