Skip to content

Commit

Permalink
feat: *stripChars builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisCraft committed May 18, 2024
1 parent cc8862c commit 9ebb3d8
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 72 deletions.
25 changes: 25 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.golden]
generated_code = true
indent_style = space
indent_size = 4
insert_final_newline = false
79 changes: 49 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 20 additions & 18 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -63,37 +63,39 @@ 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"

json-structural-diff = "0.1.0"

[workspace.lints.rust]
unsafe_op_in_unsafe_fn = "deny"
Expand Down
2 changes: 1 addition & 1 deletion crates/jrsonnet-evaluator/src/arr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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<TraceBox<dyn ArrayLike>>);

Expand Down
18 changes: 18 additions & 0 deletions crates/jrsonnet-evaluator/src/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -465,6 +472,17 @@ impl Val {
}
}

pub fn as_str_char(&self) -> Option<char> {
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<Self> {
Expand Down
3 changes: 3 additions & 0 deletions crates/jrsonnet-stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> 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),
Expand Down
16 changes: 0 additions & 16 deletions crates/jrsonnet-stdlib/src/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading

0 comments on commit 9ebb3d8

Please sign in to comment.