From b3d20a574a6f87e2c7efd96627bd14661d055153 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Sun, 26 Jul 2020 00:25:58 -0400 Subject: [PATCH] alias `sass:string` builtin functions --- src/builtin/functions/string.rs | 18 +++++++++--------- src/builtin/mod.rs | 2 +- src/builtin/modules/string.rs | 27 +++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/builtin/functions/string.rs b/src/builtin/functions/string.rs index 989d393..8e95059 100644 --- a/src/builtin/functions/string.rs +++ b/src/builtin/functions/string.rs @@ -15,7 +15,7 @@ use crate::{ value::{Number, Value}, }; -fn to_upper_case(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn to_upper_case(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { Value::String(mut i, q) => { @@ -30,7 +30,7 @@ fn to_upper_case(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn to_lower_case(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { Value::String(mut i, q) => { @@ -45,7 +45,7 @@ fn to_lower_case(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult) -> SassResult { +pub(crate) fn str_length(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { Value::String(i, _) => Ok(Value::Dimension( @@ -61,7 +61,7 @@ fn str_length(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult } } -fn quote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn quote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { Value::String(i, _) => Ok(Value::String(i, QuoteKind::Quoted)), @@ -73,7 +73,7 @@ fn quote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { } } -fn unquote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn unquote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { args.max_args(1)?; match args.get_err(0, "string")? { i @ Value::String(..) => Ok(i.unquote()), @@ -85,7 +85,7 @@ fn unquote(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { } } -fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { args.max_args(3)?; let (string, quotes) = match args.get_err(0, "string")? { Value::String(s, q) => (s, q), @@ -178,7 +178,7 @@ fn str_slice(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { } } -fn str_index(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn str_index(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { args.max_args(2)?; let s1 = match args.get_err(0, "string")? { Value::String(i, _) => i, @@ -208,7 +208,7 @@ fn str_index(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { }) } -fn str_insert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { +pub(crate) fn str_insert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult { args.max_args(3)?; let (s1, quotes) = match args.get_err(0, "string")? { Value::String(i, q) => (i, q), @@ -305,7 +305,7 @@ fn str_insert(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult #[cfg(feature = "random")] #[allow(clippy::needless_pass_by_value)] -fn unique_id(args: CallArgs, _: &mut Parser<'_>) -> SassResult { +pub(crate) fn unique_id(args: CallArgs, _: &mut Parser<'_>) -> SassResult { args.max_args(0)?; let mut rng = thread_rng(); let string = std::iter::repeat(()) diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index 41efe0e..e31fb8c 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -1,4 +1,4 @@ mod functions; pub(crate) mod modules; -pub(crate) use functions::{list, map, math, meta, selector, Builtin, GLOBAL_FUNCTIONS}; +pub(crate) use functions::{list, map, math, meta, selector, string, Builtin, GLOBAL_FUNCTIONS}; diff --git a/src/builtin/modules/string.rs b/src/builtin/modules/string.rs index 47d58dc..5e63c40 100644 --- a/src/builtin/modules/string.rs +++ b/src/builtin/modules/string.rs @@ -1,5 +1,28 @@ use crate::{ - args::CallArgs, builtin::modules::Module, error::SassResult, parse::Parser, value::Value, + args::CallArgs, + builtin::{ + modules::Module, + string::{ + quote, str_index, str_insert, str_length, str_slice, to_lower_case, to_upper_case, + unquote, + }, + }, + error::SassResult, + parse::Parser, + value::Value, }; -pub(crate) fn declare(_f: &mut Module) {} +#[cfg(feature = "random")] +use crate::builtin::string::unique_id; + +pub(crate) fn declare(f: &mut Module) { + f.insert_builtin("quote", quote); + f.insert_builtin("index", str_index); + f.insert_builtin("insert", str_insert); + f.insert_builtin("length", str_length); + f.insert_builtin("slice", str_slice); + f.insert_builtin("to-lower-case", to_lower_case); + f.insert_builtin("to-upper-case", to_upper_case); + #[cfg(feature = "random")] + f.insert_builtin("unique-id", unique_id); +}