difftreelog
feat `*stripChars` builtins
in: master
12 files changed
.editorconfigdiffbeforeafterbothno changes
Cargo.lockdiffbeforeafterboth909091[[package]]91[[package]]92name = "anyhow"92name = "anyhow"93version = "1.0.83"93version = "1.0.86"94source = "registry+https://github.com/rust-lang/crates.io-index"94source = "registry+https://github.com/rust-lang/crates.io-index"95checksum = "25bdb32cbbdce2b519a9cd7df3a678443100e265d5e25ca763b7572a5104f5f3"95checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"969697[[package]]97[[package]]98name = "autocfg"98name = "autocfg"102102103[[package]]103[[package]]104name = "base64"104name = "base64"105version = "0.21.7"105version = "0.22.1"106source = "registry+https://github.com/rust-lang/crates.io-index"106source = "registry+https://github.com/rust-lang/crates.io-index"107checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567"107checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"108108109[[package]]109[[package]]110name = "beef"110name = "beef"194 "heck",194 "heck",195 "proc-macro2",195 "proc-macro2",196 "quote",196 "quote",197 "syn 2.0.61",197 "syn 2.0.64",198]198]199199200[[package]]200[[package]]257 "syn 1.0.109",257 "syn 1.0.109",258]258]259260[[package]]261name = "difflib"262version = "0.4.0"263source = "registry+https://github.com/rust-lang/crates.io-index"264checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8"259265260[[package]]266[[package]]261name = "digest"267name = "digest"289295290[[package]]296[[package]]291name = "either"297name = "either"292version = "1.11.0"298version = "1.12.0"293source = "registry+https://github.com/rust-lang/crates.io-index"299source = "registry+https://github.com/rust-lang/crates.io-index"294checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2"300checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b"295301296[[package]]302[[package]]297name = "encode_unicode"303name = "encode_unicode"412418413[[package]]419[[package]]414name = "insta"420name = "insta"415version = "1.38.0"421version = "1.39.0"416source = "registry+https://github.com/rust-lang/crates.io-index"422source = "registry+https://github.com/rust-lang/crates.io-index"417checksum = "3eab73f58e59ca6526037208f0e98851159ec1633cf17b6cd2e1f2c3fd5d53cc"423checksum = "810ae6042d48e2c9e9215043563a58a80b877bc863228a74cf10c49d4620a6f5"418dependencies = [424dependencies = [419 "console",425 "console",420 "lazy_static",426 "lazy_static",430436431[[package]]437[[package]]432name = "itertools"438name = "itertools"433version = "0.12.1"439version = "0.13.0"434source = "registry+https://github.com/rust-lang/crates.io-index"440source = "registry+https://github.com/rust-lang/crates.io-index"435checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"441checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"436dependencies = [442dependencies = [437 "either",443 "either",438]444]547dependencies = [553dependencies = [548 "proc-macro2",554 "proc-macro2",549 "quote",555 "quote",550 "syn 2.0.61",556 "syn 2.0.64",551]557]552558553[[package]]559[[package]]607 "peg",613 "peg",608]614]615616[[package]]617name = "json-structural-diff"618version = "0.1.0"619source = "registry+https://github.com/rust-lang/crates.io-index"620checksum = "25c7940d3c84d2079306c176c7b2b37622b6bc5e43fbd1541b1e4a4e1fd02045"621dependencies = [622 "difflib",623 "regex",624 "serde_json",625]609626610[[package]]627[[package]]611name = "keccak"628name = "keccak"624641625[[package]]642[[package]]626name = "libc"643name = "libc"627version = "0.2.154"644version = "0.2.155"628source = "registry+https://github.com/rust-lang/crates.io-index"645source = "registry+https://github.com/rust-lang/crates.io-index"629checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"646checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"630647631[[package]]648[[package]]632name = "libjsonnet"649name = "libjsonnet"646663647[[package]]664[[package]]648name = "linux-raw-sys"665name = "linux-raw-sys"649version = "0.4.13"666version = "0.4.14"650source = "registry+https://github.com/rust-lang/crates.io-index"667source = "registry+https://github.com/rust-lang/crates.io-index"651checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c"668checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"652669653[[package]]670[[package]]654name = "lock_api"671name = "lock_api"681 "proc-macro2",698 "proc-macro2",682 "quote",699 "quote",683 "regex-syntax",700 "regex-syntax",684 "syn 2.0.61",701 "syn 2.0.64",685]702]686703687[[package]]704[[package]]9891006990[[package]]1007[[package]]991name = "serde"1008name = "serde"992version = "1.0.201"1009version = "1.0.202"993source = "registry+https://github.com/rust-lang/crates.io-index"1010source = "registry+https://github.com/rust-lang/crates.io-index"994checksum = "780f1cebed1629e4753a1a38a3c72d30b97ec044f0aef68cb26650a3c5cf363c"1011checksum = "226b61a0d411b2ba5ff6d7f73a476ac4f8bb900373459cd00fab8512828ba395"995dependencies = [1012dependencies = [996 "serde_derive",1013 "serde_derive",997]1014]9981015999[[package]]1016[[package]]1000name = "serde_derive"1017name = "serde_derive"1001version = "1.0.201"1018version = "1.0.202"1002source = "registry+https://github.com/rust-lang/crates.io-index"1019source = "registry+https://github.com/rust-lang/crates.io-index"1003checksum = "c5e405930b9796f1c00bee880d03fc7e0bb4b9a11afc776885ffe84320da2865"1020checksum = "6048858004bcff69094cd972ed40a32500f153bd3be9f716b2eed2e8217c4838"1004dependencies = [1021dependencies = [1005 "proc-macro2",1022 "proc-macro2",1006 "quote",1023 "quote",1007 "syn 2.0.61",1024 "syn 2.0.64",1008]1025]100910261010[[package]]1027[[package]]112111381122[[package]]1139[[package]]1123name = "syn"1140name = "syn"1124version = "2.0.61"1141version = "2.0.64"1125source = "registry+https://github.com/rust-lang/crates.io-index"1142source = "registry+https://github.com/rust-lang/crates.io-index"1126checksum = "c993ed8ccba56ae856363b1845da7266a7cb78e1d146c8a32d54b45a8b831fc9"1143checksum = "7ad3dee41f36859875573074334c200d1add8e4a87bb37113ebd31d926b7b11f"1127dependencies = [1144dependencies = [1128 "proc-macro2",1145 "proc-macro2",1129 "quote",1146 "quote",1149 "jrsonnet-evaluator",1166 "jrsonnet-evaluator",1150 "jrsonnet-gcmodule",1167 "jrsonnet-gcmodule",1151 "jrsonnet-stdlib",1168 "jrsonnet-stdlib",1169 "json-structural-diff",1152 "serde",1170 "serde",1171 "serde_json",1153]1172]115411731155[[package]]1174[[package]]116011791161[[package]]1180[[package]]1162name = "thiserror"1181name = "thiserror"1163version = "1.0.60"1182version = "1.0.61"1164source = "registry+https://github.com/rust-lang/crates.io-index"1183source = "registry+https://github.com/rust-lang/crates.io-index"1165checksum = "579e9083ca58dd9dcf91a9923bb9054071b9ebbd800b342194c9feb0ee89fc18"1184checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709"1166dependencies = [1185dependencies = [1167 "thiserror-impl",1186 "thiserror-impl",1168]1187]116911881170[[package]]1189[[package]]1171name = "thiserror-impl"1190name = "thiserror-impl"1172version = "1.0.60"1191version = "1.0.61"1173source = "registry+https://github.com/rust-lang/crates.io-index"1192source = "registry+https://github.com/rust-lang/crates.io-index"1174checksum = "e2470041c06ec3ac1ab38d0356a6119054dedaea53e12fbefc0de730a1c08524"1193checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533"1175dependencies = [1194dependencies = [1176 "proc-macro2",1195 "proc-macro2",1177 "quote",1196 "quote",1178 "syn 2.0.61",1197 "syn 2.0.64",1179]1198]118011991181[[package]]1200[[package]]1347dependencies = [1366dependencies = [1348 "proc-macro2",1367 "proc-macro2",1349 "quote",1368 "quote",1350 "syn 2.0.61",1369 "syn 2.0.64",1351]1370]13521371Cargo.tomldiffbeforeafterboth44serde_yaml_with_quirks = "0.8.24"44serde_yaml_with_quirks = "0.8.24"454546# Error handling46# Error handling47anyhow = "1.0.80"47anyhow = "1.0.83"48thiserror = "1.0"48thiserror = "1.0.60"494950# Code formatting50# Code formatting51dprint-core = "0.65.0"51dprint-core = "0.65.0"63# Source code parsing.63# Source code parsing.64# Jrsonnet has two parsers for jsonnet - one is for execution, and another is for better parsing diagnostics/lints/LSP.64# Jrsonnet has two parsers for jsonnet - one is for execution, and another is for better parsing diagnostics/lints/LSP.65# First (and fast one) is based on peg, second is based on rowan.65# First (and fast one) is based on peg, second is based on rowan.66peg = "0.8.2"66peg = "0.8.3"67logos = "0.14.0"67logos = "0.14.0"68ungrammar = "1.16.1"68ungrammar = "1.16.1"69rowan = "0.15"69rowan = "0.15.15"707071mimallocator = "0.1.3"71mimallocator = "0.1.3"72indoc = "2.0"72indoc = "2.0"73insta = "1.35"73insta = "1.39"74tempfile = "3.10"74tempfile = "3.10"75pathdiff = "0.2.1"75pathdiff = "0.2.1"76hashbrown = "0.14.3"76hashbrown = "0.14.5"77static_assertions = "1.1"77static_assertions = "1.1"78rustc-hash = "1.1"78rustc-hash = "1.1"79num-bigint = "0.4.4"79num-bigint = "0.4.5"80derivative = "2.2.0"80derivative = "2.2.0"81strsim = "0.11.0"81strsim = "0.11.0"82structdump = "0.2.0"82structdump = "0.2.0"83proc-macro2 = "1.0"83proc-macro2 = "1.0"84quote = "1.0"84quote = "1.0"85syn = "2.0"85syn = "2.0"86drop_bomb = "0.1.5"86drop_bomb = "0.1.5"87base64 = "0.21.7"87base64 = "0.22.1"88indexmap = "2.2.3"88indexmap = "2.2.3"89itertools = "0.12.1"89itertools = "0.13.0"90xshell = "0.2.5"90xshell = "0.2.6"919192lsp-server = "0.7.6"92lsp-server = "0.7.6"93lsp-types = "0.95.0"93lsp-types = "0.96.0"949495regex = "1.10.3"95regex = "1.10"96lru = "0.12.2"96lru = "0.12.3"9798json-structural-diff = "0.1.0"979998[workspace.lints.rust]100[workspace.lints.rust]99unsafe_op_in_unsafe_fn = "deny"101unsafe_op_in_unsafe_fn = "deny"crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth111112/// Represents a Jsonnet array value.12/// Represents a Jsonnet array value.13#[derive(Debug, Clone, Trace)]13#[derive(Debug, Clone, Trace)]14// may contrain other ArrValue14// may contain other ArrValue15#[trace(tracking(force))]15#[trace(tracking(force))]16pub struct ArrValue(Cc<TraceBox<dyn ArrayLike>>);16pub struct ArrValue(Cc<TraceBox<dyn ArrayLike>>);1717crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth220 Arr(ArrValue),220 Arr(ArrValue),221}221}222impl IndexableVal {222impl IndexableVal {223 pub fn is_empty(&self) -> bool {224 match self {225 Self::Str(s) => s.is_empty(),226 Self::Arr(s) => s.is_empty(),227 }228 }229223 pub fn to_array(self) -> ArrValue {230 pub fn to_array(self) -> ArrValue {224 match self {231 match self {crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth201 ("parseOctal", builtin_parse_octal::INST),201 ("parseOctal", builtin_parse_octal::INST),202 ("parseHex", builtin_parse_hex::INST),202 ("parseHex", builtin_parse_hex::INST),203 ("stringChars", builtin_string_chars::INST),203 ("stringChars", builtin_string_chars::INST),204 ("lstripChars", builtin_lstrip_chars::INST),205 ("rstripChars", builtin_rstrip_chars::INST),206 ("stripChars", builtin_strip_chars::INST),204 // Misc207 // Misc205 ("length", builtin_length::INST),208 ("length", builtin_length::INST),206 ("get", builtin_get::INST),209 ("get", builtin_get::INST),crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth334 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',4 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',556 lstripChars(str, chars)::7 if std.length(str) > 0 && std.member(chars, str[0]) then8 std.lstripChars(str[1:], chars)9 else10 str,1112 rstripChars(str, chars)::13 local len = std.length(str);14 if len > 0 && std.member(chars, str[len - 1]) then15 std.rstripChars(str[:len - 1], chars)16 else17 str,1819 stripChars(str, chars)::20 std.lstripChars(std.rstripChars(str, chars), chars),2122 mapWithIndex(func, arr)::6 mapWithIndex(func, arr)::23 if !std.isFunction(func) then7 if !std.isFunction(func) then24 error ('std.mapWithIndex first param must be function, got ' + std.type(func))8 error ('std.mapWithIndex first param must be function, got ' + std.type(func))crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth1use std::collections::BTreeSet;21use jrsonnet_evaluator::{3use jrsonnet_evaluator::{2 bail,4 bail,3 error::{ErrorKind::*, Result},5 error::{ErrorKind::*, Result},4 function::builtin,6 function::builtin,5 typed::{Either2, M1},7 typed::{Either2, Typed, M1},6 val::ArrValue,8 val::{ArrValue, IndexableVal},7 Either, IStr, Val,9 Either, IStr, Val,8};10};911215 })217 })216}218}219220#[builtin]221pub fn builtin_string_chars(str: IStr) -> ArrValue {222 ArrValue::chars(str.chars())223}224225#[builtin]226pub fn builtin_lstrip_chars(str: IStr, chars: IndexableVal) -> Result<IStr> {227 if str.is_empty() || chars.is_empty() {228 return Ok(str);229 }230231 let pattern = new_trim_pattern(chars)?;232 Ok(str.as_str().trim_start_matches(pattern).into())233}234235#[builtin]236pub fn builtin_rstrip_chars(str: IStr, chars: IndexableVal) -> Result<IStr> {237 if str.is_empty() || chars.is_empty() {238 return Ok(str);239 }240241 let pattern = new_trim_pattern(chars)?;242 Ok(str.as_str().trim_end_matches(pattern).into())243}244245#[builtin]246pub fn builtin_strip_chars(str: IStr, chars: IndexableVal) -> Result<IStr> {247 if str.is_empty() || chars.is_empty() {248 return Ok(str);249 }250251 let pattern = new_trim_pattern(chars)?;252 Ok(str.as_str().trim_matches(pattern).into())253}254255fn new_trim_pattern(chars: IndexableVal) -> Result<impl Fn(char) -> bool> {256 let chars: BTreeSet<char> = match chars {257 IndexableVal::Str(chars) => chars.chars().collect(),258 IndexableVal::Arr(chars) => chars259 .iter()260 .filter_map(|it| it.map(|it| char::from_untyped(it).ok()).transpose())261 .collect::<Result<_, _>>()?,262 };263264 Ok(move |char| chars.contains(&char))265}217266218#[cfg(test)]267#[cfg(test)]219mod tests {268mod tests {244 }293 }245}294}246247#[builtin]248pub fn builtin_string_chars(str: IStr) -> ArrValue {249 ArrValue::chars(str.chars())250}251295tests/Cargo.tomldiffbeforeafterboth12jrsonnet-gcmodule.workspace = true12jrsonnet-gcmodule.workspace = true13jrsonnet-stdlib.workspace = true13jrsonnet-stdlib.workspace = true14serde.workspace = true14serde.workspace = true15json-structural-diff.workspace = true16serde_json.workspace = true1517tests/golden/builtin_strings_string.jsonnetdiffbeforeafterbothno changes
tests/golden/builtin_strings_string.jsonnet.goldendiffbeforeafterbothno changes
tests/tests/golden.rsdiffbeforeafterboth353436#[test]35#[test]37fn test() -> io::Result<()> {36fn test() -> io::Result<()> {37 use json_structural_diff::JsonDiff;3838 let mut root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));39 let mut root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));39 root.push("golden");40 root.push("golden");54 } else {55 } else {55 let golden = fs::read_to_string(golden_path)?;56 let golden = fs::read_to_string(golden_path)?;5758 match (serde_json::from_str(&result), serde_json::from_str(&golden)) {59 (Err(_), Ok(_)) => assert_eq!(60 result,61 golden,62 "unexpected error for golden {}",63 entry.path().display()64 ),65 (Ok(_), Err(_)) => assert_eq!(66 result,67 golden,68 "expected error for golden {}",69 entry.path().display()70 ),71 (Ok(result), Ok(golden)) => {72 // Show diff relative to golden`.73 let diff = JsonDiff::diff_string(&golden, &result, false);74 if let Some(diff) = diff {75 panic!(76 "Result \n{result:#}\n\77 and golden \n{golden:#}\n\78 did not match structurally:\n{diff:#}\n\79 for golden {}",80 entry.path().display()81 );82 }83 }84 (Err(_), Err(_)) => {}85 };568657 assert_eq!(87 assert_eq!(58 result,88 result,