From a847401db7a1f2ce9ea7c37bad59a1089daf3c17 Mon Sep 17 00:00:00 2001 From: Petr Portnov Date: Fri, 17 May 2024 03:09:59 +0300 Subject: [PATCH] feat: `*stripChars` builtins --- .editorconfig | 24 +++++++++ Cargo.lock | 68 +++++++++++++----------- Cargo.toml | 36 ++++++------- crates/jrsonnet-evaluator/src/arr/mod.rs | 2 +- crates/jrsonnet-evaluator/src/val.rs | 18 +++++++ crates/jrsonnet-stdlib/src/lib.rs | 3 ++ crates/jrsonnet-stdlib/src/std.jsonnet | 16 ------ crates/jrsonnet-stdlib/src/strings.rs | 56 ++++++++++++++++--- 8 files changed, 150 insertions(+), 73 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..9f6fb00d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,24 @@ +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +indent_style = tab +tab_width = 4 + +[*.{yml,yaml}] +indent_size = 2 + +[LICENSE] +generated_code = true + +[Cargo.lock] +generated_code = true +indent_size = 1 + +[tests/golden/*.jsonnet] +generated_code = true +indent_style = space +indent_size = 4 diff --git a/Cargo.lock b/Cargo.lock index 05039bdd..b43179f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,9 +90,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.83" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25bdb32cbbdce2b519a9cd7df3a678443100e265d5e25ca763b7572a5104f5f3" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" [[package]] name = "autocfg" @@ -102,9 +102,9 @@ checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "base64" -version = "0.21.7" +version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" [[package]] name = "beef" @@ -141,6 +141,9 @@ name = "bumpalo" version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +dependencies = [ + "allocator-api2", +] [[package]] name = "cc" @@ -194,7 +197,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.64", ] [[package]] @@ -269,12 +272,13 @@ dependencies = [ [[package]] name = "dprint-core" -version = "0.65.0" +version = "0.66.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b569f4e3085ae957ecc37588e6b2227791b72745434eae966db29e122ba27f0d" +checksum = "f3ab0dd2bedc109d25f0d21afb09b7d329f6c6fa83b095daf31d2d967e091548" dependencies = [ "anyhow", "bumpalo", + "hashbrown 0.14.5", "indexmap 2.2.6", "rustc-hash", "serde", @@ -289,9 +293,9 @@ checksum = "9bda8e21c04aca2ae33ffc2fd8c23134f3cac46db123ba97bd9d3f3b8a4a85e1" [[package]] name = "either" -version = "1.11.0" +version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2" +checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" [[package]] name = "encode_unicode" @@ -412,9 +416,9 @@ checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" [[package]] name = "insta" -version = "1.38.0" +version = "1.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eab73f58e59ca6526037208f0e98851159ec1633cf17b6cd2e1f2c3fd5d53cc" +checksum = "810ae6042d48e2c9e9215043563a58a80b877bc863228a74cf10c49d4620a6f5" dependencies = [ "console", "lazy_static", @@ -430,9 +434,9 @@ checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" [[package]] name = "itertools" -version = "0.12.1" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" dependencies = [ "either", ] @@ -547,7 +551,7 @@ version = "0.5.0-pre96" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.64", ] [[package]] @@ -624,9 +628,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.154" +version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "libjsonnet" @@ -646,9 +650,9 @@ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" [[package]] name = "linux-raw-sys" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "lock_api" @@ -681,7 +685,7 @@ dependencies = [ "proc-macro2", "quote", "regex-syntax", - "syn 2.0.61", + "syn 2.0.64", ] [[package]] @@ -989,22 +993,22 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.201" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "780f1cebed1629e4753a1a38a3c72d30b97ec044f0aef68cb26650a3c5cf363c" +checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.201" +version = "1.0.202" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865" +checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.64", ] [[package]] @@ -1121,9 +1125,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.61" +version = "2.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c993ed8ccba56ae856363b1845da7266a7cb78e1d146c8a32d54b45a8b831fc9" +checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f" dependencies = [ "proc-macro2", "quote", @@ -1160,22 +1164,22 @@ checksum = "f18aa187839b2bdb1ad2fa35ead8c4c2976b64e4363c386d45ac0f7ee85c9233" [[package]] name = "thiserror" -version = "1.0.60" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "579e9083ca58dd9dcf91a9923bb9054071b9ebbd800b342194c9feb0ee89fc18" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.60" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.64", ] [[package]] @@ -1347,5 +1351,5 @@ checksum = "15e934569e47891f7d9411f1a451d947a60e000ab3bd24fbb970f000387d1b3b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.61", + "syn 2.0.64", ] diff --git a/Cargo.toml b/Cargo.toml index 2ace9a6e..3fd601e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,13 +39,13 @@ clap_complete = "4.5" # Parsing, manifestification is implemented manually everywhere # Note on serde_yaml_with_quirks: This is a fork of serde-yaml with legacy yaml 1.1 support: # https://github.com/dtolnay/serde-yaml/pull/225 -serde = "1.0.197" -serde_json = "1.0.114" +serde = "1.0.202" +serde_json = "1.0.117" serde_yaml_with_quirks = "0.8.24" # Error handling -anyhow = "1.0.80" -thiserror = "1.0" +anyhow = "1.0.83" +thiserror = "1.0.60" # Code formatting dprint-core = "0.65.0" @@ -63,37 +63,37 @@ bincode = "1.3" # Source code parsing. # Jrsonnet has two parsers for jsonnet - one is for execution, and another is for better parsing diagnostics/lints/LSP. # First (and fast one) is based on peg, second is based on rowan. -peg = "0.8.2" +peg = "0.8.3" logos = "0.14.0" ungrammar = "1.16.1" -rowan = "0.15" +rowan = "0.15.15" mimallocator = "0.1.3" indoc = "2.0" -insta = "1.35" +insta = "1.39" tempfile = "3.10" pathdiff = "0.2.1" -hashbrown = "0.14.3" +hashbrown = "0.14.5" static_assertions = "1.1" rustc-hash = "1.1" -num-bigint = "0.4.4" +num-bigint = "0.4.5" derivative = "2.2.0" strsim = "0.11.0" structdump = "0.2.0" -proc-macro2 = "1.0" -quote = "1.0" -syn = "2.0" +proc-macro2 = "1.0.82" +quote = "1.0.36" +syn = "2.0.63" drop_bomb = "0.1.5" -base64 = "0.21.7" +base64 = "0.22.1" indexmap = "2.2.3" -itertools = "0.12.1" -xshell = "0.2.5" +itertools = "0.13.0" +xshell = "0.2.6" lsp-server = "0.7.6" -lsp-types = "0.95.0" +lsp-types = "0.96.0" -regex = "1.10.3" -lru = "0.12.2" +regex = "1.10" +lru = "0.12.3" [workspace.lints.rust] unsafe_op_in_unsafe_fn = "deny" diff --git a/crates/jrsonnet-evaluator/src/arr/mod.rs b/crates/jrsonnet-evaluator/src/arr/mod.rs index 7aefaa48..05e929a0 100644 --- a/crates/jrsonnet-evaluator/src/arr/mod.rs +++ b/crates/jrsonnet-evaluator/src/arr/mod.rs @@ -14,7 +14,7 @@ pub use spec::{ArrayLike, *}; /// Represents a Jsonnet array value. #[derive(Debug, Clone, Trace)] -// may contrain other ArrValue +// may contain other ArrValue #[trace(tracking(force))] pub struct ArrValue(Cc>); diff --git a/crates/jrsonnet-evaluator/src/val.rs b/crates/jrsonnet-evaluator/src/val.rs index d0a42cc1..177b89d9 100644 --- a/crates/jrsonnet-evaluator/src/val.rs +++ b/crates/jrsonnet-evaluator/src/val.rs @@ -220,6 +220,13 @@ pub enum IndexableVal { Arr(ArrValue), } impl IndexableVal { + pub fn empty(&self) -> bool { + match self { + Self::Str(s) => s.is_empty(), + Self::Arr(s) => s.is_empty(), + } + } + pub fn to_array(self) -> ArrValue { match self { Self::Str(s) => ArrValue::chars(s.chars()), @@ -465,6 +472,17 @@ impl Val { } } + pub fn as_str_char(&self) -> Option { + let as_str = self.as_str()?; + let mut chars = as_str.chars(); + let first_char = chars.next()?; + if chars.next().is_some() { + None + } else { + Some(first_char) + } + } + /// Creates `Val::Num` after checking for numeric overflow. /// As numbers are `f64`, we can just check for their finity. pub fn new_checked_num(num: f64) -> Result { diff --git a/crates/jrsonnet-stdlib/src/lib.rs b/crates/jrsonnet-stdlib/src/lib.rs index 99333fc3..a608c037 100644 --- a/crates/jrsonnet-stdlib/src/lib.rs +++ b/crates/jrsonnet-stdlib/src/lib.rs @@ -201,6 +201,9 @@ pub fn stdlib_uncached(settings: Rc>) -> ObjValue { ("parseOctal", builtin_parse_octal::INST), ("parseHex", builtin_parse_hex::INST), ("stringChars", builtin_string_chars::INST), + ("lstripChars", builtin_lstrip_chars::INST), + ("rstripChars", builtin_rstrip_chars::INST), + ("stripChars", builtin_strip_chars::INST), // Misc ("length", builtin_length::INST), ("get", builtin_get::INST), diff --git a/crates/jrsonnet-stdlib/src/std.jsonnet b/crates/jrsonnet-stdlib/src/std.jsonnet index 6f793848..020b69b2 100644 --- a/crates/jrsonnet-stdlib/src/std.jsonnet +++ b/crates/jrsonnet-stdlib/src/std.jsonnet @@ -3,22 +3,6 @@ thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though', - lstripChars(str, chars):: - if std.length(str) > 0 && std.member(chars, str[0]) then - std.lstripChars(str[1:], chars) - else - str, - - rstripChars(str, chars):: - local len = std.length(str); - if len > 0 && std.member(chars, str[len - 1]) then - std.rstripChars(str[:len - 1], chars) - else - str, - - stripChars(str, chars):: - std.lstripChars(std.rstripChars(str, chars), chars), - mapWithIndex(func, arr):: if !std.isFunction(func) then error ('std.mapWithIndex first param must be function, got ' + std.type(func)) diff --git a/crates/jrsonnet-stdlib/src/strings.rs b/crates/jrsonnet-stdlib/src/strings.rs index 39a2281b..fa9b318d 100644 --- a/crates/jrsonnet-stdlib/src/strings.rs +++ b/crates/jrsonnet-stdlib/src/strings.rs @@ -1,9 +1,11 @@ +use std::collections::BTreeSet; + use jrsonnet_evaluator::{ bail, error::{ErrorKind::*, Result}, function::builtin, typed::{Either2, M1}, - val::ArrValue, + val::{ArrValue, IndexableVal}, Either, IStr, Val, }; @@ -215,6 +217,53 @@ pub fn builtin_bigint(v: Either![f64, IStr]) -> Result { }) } +#[builtin] +pub fn builtin_string_chars(str: IStr) -> ArrValue { + ArrValue::chars(str.chars()) +} + +#[builtin] +pub fn builtin_lstrip_chars(str: IStr, chars: IndexableVal) -> Result { + if str.is_empty() || chars.empty() { + return Ok(str); + } + + let pattern = new_trim_pattern(chars)?; + Ok(str.as_str().trim_start_matches(pattern).into()) +} + +#[builtin] +pub fn builtin_rstrip_chars(str: IStr, chars: IndexableVal) -> Result { + if str.is_empty() || chars.empty() { + return Ok(str); + } + + let pattern = new_trim_pattern(chars)?; + Ok(str.as_str().trim_start_matches(pattern).into()) +} + +#[builtin] +pub fn builtin_strip_chars(str: IStr, chars: IndexableVal) -> Result { + if str.is_empty() || chars.empty() { + return Ok(str); + } + + let pattern = new_trim_pattern(chars)?; + Ok(str.as_str().trim_start_matches(pattern).into()) +} + +fn new_trim_pattern(chars: IndexableVal) -> Result bool> { + let chars: BTreeSet = match chars { + IndexableVal::Str(chars) => chars.chars().collect(), + IndexableVal::Arr(chars) => chars + .iter() + .filter_map(|it| it.map(|it| it.as_str_char()).transpose()) + .collect::>()?, + }; + + Ok(move |char| chars.contains(&char)) +} + #[cfg(test)] mod tests { use super::*; @@ -243,8 +292,3 @@ mod tests { assert_eq!(parse_nat::<16>("BbC").unwrap(), 0xBBC as f64); } } - -#[builtin] -pub fn builtin_string_chars(str: IStr) -> ArrValue { - ArrValue::chars(str.chars()) -}