diff --git a/R/use_crate.R b/R/use_crate.R index 4103130a..38cfcd5d 100644 --- a/R/use_crate.R +++ b/R/use_crate.R @@ -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) @@ -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 ) diff --git a/tests/testthat/test-use_crate.R b/tests/testthat/test-use_crate.R index 7254168f..e94815ff 100644 --- a/tests/testthat/test-use_crate.R +++ b/tests/testthat/test-use_crate.R @@ -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", + version = "1.0.1", + path = path ) metadata <- read_cargo_metadata(path) @@ -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") })