R/toml.R

Defines functions is_datetime format_iso8601 toTOML

Documented in format_iso8601 toTOML

#' Simple converter from list to TOML.
#'
#' @param data A list.
#' @rdname toml
#' @export
toTOML = function(data, usetz = TRUE) {
  v = data |>
    purrr::compact() |>
    purrr::discard(is.na) |>
    purrr::modify_if(is.character, \(x) paste0('"', x, '"')) |>
    purrr::modify_if(is_datetime, format_iso8601) |>
    purrr::modify_if(is.logical, tolower)
  paste0(names(v), " = ", v, collapse = "\n")
}

#' @description
#' `format_iso8601()` is compliant with TOML.
#' @inheritParams lubridate::format_ISO8601
#' @rdname toml
#' @export
format_iso8601 = function(x, usetz = TRUE, precision = NULL, ...) {
  lubridate::format_ISO8601(x, usetz = usetz, precision = precision, ...) |>
    stringr::str_replace("([-+]\\d\\d)(\\d\\d)$", "\\1:\\2")
}

is_datetime = function(x) {
  lubridate::is.Date(x) | lubridate::is.POSIXct(x)
}
heavywatal/rwtl documentation built on April 29, 2024, 2:02 a.m.