Skip to content

remove glue #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ Imports:
callr,
cli,
desc,
dplyr,
glue (>= 1.7.0),
dplyr,
jsonlite,
pkgbuild (>= 1.4.0),
processx,
Expand All @@ -67,6 +66,6 @@ Config/testthat/edition: 3
Config/testthat/parallel: true
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
RoxygenNote: 7.3.2.9000
SystemRequirements: Rust 'cargo'; the crate 'libR-sys' must compile
without error
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export(use_msrv)
export(vendor_pkgs)
export(write_license_note)
importFrom(dplyr,mutate)
importFrom(glue,glue)
importFrom(glue,glue_collapse)
importFrom(rlang,"%||%")
importFrom(rlang,.data)
importFrom(rlang,.env)
Expand Down
4 changes: 2 additions & 2 deletions R/clean.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ clean <- function(path = ".", echo = TRUE) {

args <- c(
"clean",
glue::glue("--manifest-path={manifest_path}"),
glue::glue("--target-dir={target_dir}"),
paste0("--manifest-path=", manifest_path),
paste0("--target-dir=", target_dir),
if (tty_has_colors()) {
"--color=always"
} else {
Expand Down
20 changes: 10 additions & 10 deletions R/eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#' }
#' @export
rust_eval <- function(code, env = parent.frame(), ...) {
check_character(code, call = rlang::caller_call(), class = "rextendr_error")
check_environment(env, call = rlang::caller_call(), class = "rextendr_error")

rust_eval_deferred(code = code, env = env, ...)()
}

Expand All @@ -45,7 +48,7 @@ rust_eval <- function(code, env = parent.frame(), ...) {
#' @noRd
rust_eval_deferred <- function(code, env = parent.frame(), ...) {
# make sure code is given as a single character string
code <- glue_collapse(code, sep = "\n")
code <- paste0(code, collapse = "\n")

# Snippet hash is constructed from the Rust source code and
# a unique identifier of the compiled dll.
Expand All @@ -57,17 +60,14 @@ rust_eval_deferred <- function(code, env = parent.frame(), ...) {
snippet_hash <- rlang::hash(list(the$count, code)) # nolint: object_usage_linter

# The unique hash is then used to generate unique function names
fn_name <- glue("rextendr_rust_eval_fun_{snippet_hash}")
fn_name <- paste0("rextendr_rust_eval_fun_", snippet_hash)

# wrap code into Rust function
code_wrapped <- glue(r"(
fn {fn_name}() -> Result<Robj> {{
let x = {{
{code}
}};
Ok(x.into())
}}
)")
code_wrapped <- sprintf(
"fn %s() -> Result<Robj> {\n let x = {\n %s\n };\n Ok(x.into())\n}",
fn_name,
code
)

# Attempt to figure out whether the Rust code returns a result or not,
# and make the result invisible or not accordingly. This regex approach
Expand Down
4 changes: 2 additions & 2 deletions R/find_exports.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ find_extendr_attrs_ids <- function(lns) {
extract_meta <- function(lns) {
# Matches fn|impl<'a> item_name
result <- stringi::stri_match_first_regex(
glue_collapse(lns, sep = "\n"),
paste0(lns, collapse = "\n"),
"(?:(?<struct>struct)|(?<enum>enum)|(?<fn>fn)|(?<impl>impl)(?:\\s*<(?<lifetime>.+?)>)?)\\s+(?<name>(?:r#)?(?:_\\w+|[A-z]\\w*))" # nolint: line_length_linter
) |>
as.data.frame() |>
Expand All @@ -37,7 +37,7 @@ extract_meta <- function(lns) {
# This unfortunately does not provide
# meaningful output or source line numbers.
code_sample <- stringi::stri_sub(
glue_collapse(lns, sep = "\n "),
paste0(lns, collapse = "\n "),
1, 80
)

Expand Down
2 changes: 1 addition & 1 deletion R/helpers.r
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ get_cargo_envvars <- function() {
path_envvar <- Sys.getenv("PATH", unset = "") # nolint: object_usage_linter
cargo_path <- path.expand("~/.cargo/bin") # nolint: object_usage_linter
# "current" means appending or overwriting the envvars in addition to the current ones.
cargo_envvars <- c("current", PATH = glue("{path_envvar}:{cargo_path}"))
cargo_envvars <- c("current", PATH = paste(path_envvar, cargo_path, sep = ":"))
}
cargo_envvars
}
6 changes: 2 additions & 4 deletions R/knitr_engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ eng_extendrsrc <- function(options) {
eng_impl(options, rust_source)
}



eng_impl <- function(options, extendr_engine) {
if (!requireNamespace("knitr", quietly = TRUE)) {
cli::cli_abort(
Expand All @@ -36,8 +34,8 @@ eng_impl <- function(options, extendr_engine) {
code <- options$code
}

code <- glue_collapse(code, sep = "\n") # code to compile
code_out <- glue_collapse(options$code, sep = "\n") # code to output to html
code <- paste0(code, collapse = "\n") # code to compile
code_out <- paste0(options$code, collapse = "\n") # code to output to html

# engine.opts is a list of arguments to be passed to rust_eval, e.g.
# engine.opts = list(dependencies = list(`pulldown-cmark` = "0.8"))
Expand Down
13 changes: 8 additions & 5 deletions R/make_module_macro.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
make_module_macro <- function(code, module_name = "rextendr") {
# make sure we have cleanly separated lines
lines <- stringi::stri_split_lines(
glue_collapse(code, sep = "\n"),
paste0(code, collapse = "\n"),
omit_empty = TRUE
)[[1]]

idents <- find_exports(sanitize_rust_code(lines))
outlines <- c("extendr_module! {", glue("mod {module_name};"))
outlines <- c(outlines, glue::glue_data(idents, "{type} {name};"))
outlines <- c(outlines, "}")
outlines

c(
"extendr_module! {",
paste0("mod ", module_name, ";"),
paste0(idents[["type"]], " ", idents[["name"]], ";"),
"}"
)
}
7 changes: 6 additions & 1 deletion R/register_extendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#' @seealso [rextendr::document()]
#' @export
register_extendr <- function(path = ".", quiet = FALSE, force = FALSE, compile = NA) {
check_string(path, call = rlang::caller_call(), class = "rextendr_error")
check_bool(quiet, call = rlang::caller_call(), class = "rextendr_error")
check_bool(force, call = rlang::caller_call(), class = "rextendr_error")
check_bool(compile, allow_na = TRUE, call = rlang::caller_call(), class = "rextendr_error")

local_quiet_cli(quiet)

rextendr_setup(path = path)
Expand Down Expand Up @@ -130,7 +135,7 @@ register_extendr <- function(path = ".", quiet = FALSE, force = FALSE, compile =
#' @noRd
make_wrappers <- function(module_name, package_name, outfile,
path = ".", use_symbols = FALSE, quiet = FALSE) {
wrapper_function <- glue("wrap__make_{module_name}_wrappers")
wrapper_function <- paste0("wrap__make_", module_name, "_wrappers")
x <- .Call(
wrapper_function,
use_symbols = use_symbols,
Expand Down
1 change: 0 additions & 1 deletion R/rextendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"_PACKAGE"

#' @importFrom dplyr mutate
#' @importFrom glue glue glue_collapse
#' @importFrom rlang dots_list names2 as_function is_missing is_atomic is_null
#' @importFrom rlang is_na .data .env caller_env as_name as_label enquo %||%
#' @importFrom stringi stri_replace_all_regex
Expand Down
10 changes: 7 additions & 3 deletions R/rextendr_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
#' @return No return value, called for side effects.
#' @export
document <- function(pkg = ".", quiet = FALSE, roclets = NULL) {
check_string(pkg, call = rlang::caller_call(), class = "rextendr_error")
check_bool(quiet, call = rlang::caller_call(), class = "rextendr_error")
check_character(roclets, allow_null = TRUE, call = rlang::caller_call(), class = "rextendr_error")

withr::local_envvar(devtools::r_env_vars())

register_extendr(path = pkg, quiet = quiet)

rlang::check_installed("devtools")
devtools::document(pkg = pkg, roclets = roclets, quiet = quiet)
if (!isTRUE(quiet)) {
if (!quiet) {
check_namespace_file(pkg)
}
}
Expand All @@ -26,7 +30,7 @@ check_if_roxygen_used <- function(namespace_content) {
}

check_if_dyn_lib_used <- function(namespace_content, pkg_name) {
expected_pattern <- glue::glue("useDynLib\\({pkg_name},\\s*\\.registration = TRUE\\)")
expected_pattern <- paste0("useDynLib\\(", pkg_name, ",\\s*\\.registration = TRUE\\)")

any(stringi::stri_detect_regex(namespace_content, expected_pattern))
}
Expand All @@ -50,7 +54,7 @@ check_namespace_file <- function(path = ".") {
)
)

use_dyn_lib_ref <- glue::glue("useDynLib({package_name}, .registration = TRUE)") # nolint: object_usage_linter.
use_dyn_lib_ref <- paste0("useDynLib(", package_name, ", .registration = TRUE)") # nolint: object_usage_linter.

cli::cli_warn(
c(
Expand Down
4 changes: 2 additions & 2 deletions R/rust_sitrep.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ rustup_toolchain_target <- function() {
#' @noRd
verify_toolchains <- function(toolchains, host) {
if (rlang::is_empty(toolchains)) {
return(list(toolchains = toolchains, missing_toolchain = glue("stable-{host}")))
return(list(toolchains = toolchains, missing_toolchain = paste0("stable-", host)))
}

default_toolchain_index <- stringi::stri_detect_fixed(toolchains, "(default)")
Expand All @@ -187,7 +187,7 @@ verify_toolchains <- function(toolchains, host) {
candidate_toolchains <- toolchains[candidates]
toolchains[candidates] <- cli::col_yellow(toolchains[candidates])
} else {
missing_toolchain <- glue("stable-{host}")
missing_toolchain <- paste0("stable-", host)
}
}
list(toolchains = toolchains, missing_toolchain = missing_toolchain, candidate_toolchains = candidate_toolchains)
Expand Down
2 changes: 1 addition & 1 deletion R/sanitize_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ remove_line_comments <- function(lns) {
# comments.
# 4. We fill in space between remaining delimiters with spaces (simplest way).
fill_block_comments <- function(lns, fill_with = " ") { # nolint: object_usage_linter
lns <- glue_collapse(lns, sep = "\n")
lns <- paste0(lns, collapse = "\n")

# Fast path if character input is empty
if (length(lns) == 0L || !nzchar(lns)) {
Expand Down
31 changes: 17 additions & 14 deletions R/source.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,17 @@ rust_function <- function(code,
if (vctrs::vec_is_empty(options)) {
attr_arg <- ""
} else {
attr_arg <- options |>
glue::glue_data("{Name} = {RustValue}") |>
glue::glue_collapse(sep = ", ")
attr_arg <- glue::glue("({attr_arg})")
attr_arg <- paste(
options[["Name"]],
options[["RustValue"]],
sep = " = ",
collapse = ", "
)
attr_arg <- paste0("(", attr_arg, ")")
}

code <- c(
glue::glue("#[extendr{attr_arg}]"),
paste0("#[extendr", attr_arg, "]"),
stringi::stri_trim(code)
)

Expand Down Expand Up @@ -328,8 +331,8 @@ invoke_cargo <- function(toolchain, specific_target, dir, profile,

rtools_home <- normalizePath(
Sys.getenv(
glue("RTOOLS{rtools_version}_HOME"),
glue("C:\\rtools{rtools_version}")
paste0("RTOOLS", rtools_version, "_HOME"),
paste0("C:\\rtools", rtools_version)
),
mustWork = TRUE
)
Expand Down Expand Up @@ -362,13 +365,13 @@ invoke_cargo <- function(toolchain, specific_target, dir, profile,
compilation_result <- processx::run(
command = "cargo",
args = c(
glue("+{toolchain}"),
paste0("+", toolchain),
"build",
"--lib",
glue("--target={specific_target}"),
glue("--manifest-path={file.path(dir, 'Cargo.toml')}"),
glue("--target-dir={file.path(dir, 'target')}"),
glue("--profile={profile}"),
paste0("--target=", specific_target),
paste0("--manifest-path=", file.path(dir, "Cargo.toml")),
paste0("--target-dir=", file.path(dir, "target")),
paste0("--profile=", profile),
"--message-format=json-diagnostic-rendered-ansi",
if (tty_has_colors()) {
"--color=always"
Expand All @@ -394,14 +397,14 @@ invoke_cargo <- function(toolchain, specific_target, dir, profile,
#'
#' Checks the output of cargo and filters messages according to `level`.
#' Retrieves rendered ANSI strings and prepares them
#' for `cli` and `glue` formatting.
#' for `cli` formatting.
#' @param json_output \[ JSON(n) \] JSON messages produced by cargo.
#' @param level \[ string \] Log level.
#' Commonly used values are `"error"` and `"warning"`.
#' @param tty_has_colors \[ logical(1) \] Indicates if output
#' supports ANSI sequences. If `FALSE`, ANSI sequences are stripped off.
#' @return \[ character(n) \] Vector of strings
#' that can be passed to `cli` or `glue` functions.
#' that can be passed to `cli` functions.
#' @noRd
gather_cargo_output <- function(json_output, level, tty_has_colors) {
rendered_output <-
Expand Down
23 changes: 11 additions & 12 deletions R/toml_serialization.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,25 @@ to_toml <- function(...,
.format_int = .format_int,
.format_dbl = .format_dbl
)
body <- glue_collapse(body, "\n")
body <- paste0(body, collapse = "\n")
if (!nzchar(body)) {
body <- NULL
}

# The values can be (1) header and body, (2) header only, or (3) body only.
# In the case of (2) and (3) the other element is of length 0, so we need to
# remove them by `c()` first, and then concatenate by "\n" if both exists
glue_collapse(c(header, body), "\n")
paste0(c(header, body), collapse = "\n")
})

glue_collapse(tables, "\n\n")
paste0(tables, collapse = "\n\n")
}

make_header <- function(nm, arg) {
# For future support of array-of-table
# https://toml.io/en/v1.0.0-rc.3#array-of-tables
if (nzchar(nm) && !is.data.frame(arg)) {
as.character(glue("[{nm}]"))
paste0("[", nm, "]")
} else {
character(0)
}
Expand Down Expand Up @@ -123,7 +123,7 @@ format_toml.data.frame <- function(x,
.tbl_name,
.top_level = FALSE) {
rows <- nrow(x)
header <- glue("[[{.tbl_name}]]")
header <- paste0("[[", .tbl_name, "]]")
if (rows == 0L) {
return(as.character(header))
}
Expand All @@ -135,7 +135,6 @@ format_toml.data.frame <- function(x,
if (length(item) == 0L) {
result <- character(0)
} else {

result <- format_toml(
as.list(item),
...,
Expand Down Expand Up @@ -203,9 +202,9 @@ format_toml_atomic <- function(x,
"[ ]"
} else {
formatter <- rlang::as_function(.formatter)
items <- glue_collapse(formatter(x), ", ")
items <- paste0(formatter(x), collapse = ", ")
if (len > 1L || !is.null(dims)) {
items <- glue("[ {items} ]")
items <- paste0("[ ", items, " ]")
}
# Ensure type-stability
as.character(items)
Expand All @@ -223,9 +222,9 @@ format_toml.character <- function(x,
.str_as_literal = TRUE,
.top_level = FALSE) {
if (isTRUE(.str_as_literal)) {
.formatter <- \(.x) glue("'{.x}'")
.formatter <- \(.x) paste0("'", .x, "'")
} else {
.formatter <- \(.x) glue("\"{escape_dbl_quotes(.x)}\"")
.formatter <- \(.x) paste0("\"", escape_dbl_quotes(.x), "\"")
}
format_toml_atomic(
x,
Expand Down Expand Up @@ -291,11 +290,11 @@ format_toml.list <- function(x, ..., .top_level = FALSE) {
)
}
result <- map2(names, x, function(nm, val) {
glue("{nm} = {format_toml(val, ..., .top_level = FALSE)}")
paste0(nm, " = ", format_toml(val, ..., .top_level = FALSE))
})

if (!.top_level) {
result <- glue("{{ {paste0(result, collapse = \", \")} }}")
result <- paste("{", paste0(result, collapse = ", "), "}")
}
if (!is_atomic(result)) {
result <- list_c(result)
Expand Down
Loading
Loading