git.delta.rocks / jrsonnet / refs/commits / 2afd5ff0dd7a

difftreelog

refactor extended strings

Yaroslav Bolyukin2022-12-03parent: #81f0998.patch.diff
in: master

16 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
17 function::{CallLocation, FuncDesc, FuncVal},17 function::{CallLocation, FuncDesc, FuncVal},
18 tb, throw,18 tb, throw,
19 typed::Typed,19 typed::Typed,
20 val::{CachedUnbound, IndexableVal, Thunk, ThunkValue},20 val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},
21 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,21 Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,
22 Unbound, Val,22 Unbound, Val,
23};23};
36 }36 }
37 }37 }
38 Some(match &*expr.0 {38 Some(match &*expr.0 {
39 Expr::Str(s) => Val::Str(s.clone()),39 Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),
40 Expr::Num(n) => Val::Num(*n),40 Expr::Num(n) => Val::Num(*n),
41 Expr::Literal(LiteralType::False) => Val::Bool(false),41 Expr::Literal(LiteralType::False) => Val::Bool(false),
42 Expr::Literal(LiteralType::True) => Val::Bool(true),42 Expr::Literal(LiteralType::True) => Val::Bool(true),
135 let fctx = Pending::new();135 let fctx = Pending::new();
136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());136 let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());
137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![137 let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![
138 Thunk::evaluated(Val::Str(field.clone())),138 Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),
139 Thunk::new(tb!(ObjectFieldThunk {139 Thunk::new(tb!(ObjectFieldThunk {
140 field: field.clone(),140 field: field.clone(),
141 obj: obj.clone(),141 obj: obj.clone(),
436 Literal(LiteralType::False) => Val::Bool(false),436 Literal(LiteralType::False) => Val::Bool(false),
437 Literal(LiteralType::Null) => Val::Null,437 Literal(LiteralType::Null) => Val::Null,
438 Parened(e) => evaluate(ctx, e)?,438 Parened(e) => evaluate(ctx, e)?,
439 Str(v) => Val::Str(v.clone()),439 Str(v) => Val::Str(StrValue::Flat(v.clone())),
440 Num(v) => Val::new_checked_num(*v)?,440 Num(v) => Val::new_checked_num(*v)?,
441 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,441 BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,
442 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,442 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
457 ctx.super_obj()457 ctx.super_obj()
458 .clone()458 .clone()
459 .expect("no super found")459 .expect("no super found")
460 .get_for(name, ctx.this().clone().expect("no this found"))?460 .get_for(name.into_flat(), ctx.this().clone().expect("no this found"))?
461 .expect("value not found")461 .expect("value not found")
462 }462 }
463 Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {463 Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {
464 (Val::Obj(v), Val::Str(key)) => State::push(464 (Val::Obj(v), Val::Str(key)) => State::push(
465 CallLocation::new(loc),465 CallLocation::new(loc),
466 || format!("field <{key}> access"),466 || format!("field <{key}> access"),
467 || match v.get(key.clone()) {467 || match v.get(key.clone().into_flat()) {
468 Ok(Some(v)) => Ok(v),468 Ok(Some(v)) => Ok(v),
469 #[cfg(not(feature = "friendly-errors"))]469 #[cfg(not(feature = "friendly-errors"))]
470 Ok(None) => throw!(NoSuchField(key.clone(), vec![])),470 Ok(None) => throw!(NoSuchField(key.clone(), vec![])),
478 ) {478 ) {
479 let conf = strsim::jaro_winkler(&field as &str, &key as &str);479 let conf = strsim::jaro_winkler(
480 &field as &str,
481 &key.clone().into_flat() as &str,
482 );
480 if conf < 0.8 {483 if conf < 0.8 {
481 continue;484 continue;
485 heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));488 heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));
486489
487 throw!(NoSuchField(490 throw!(NoSuchField(
488 key.clone(),491 key.clone().into_flat(),
489 heap.into_iter().map(|(_, v)| v).collect()492 heap.into_iter().map(|(_, v)| v).collect()
490 ))493 ))
491 }494 }
505 v.get(n as usize)?508 v.get(n as usize)?
506 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?509 .ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?
507 }510 }
508 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),511 (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n.into_flat())),
509 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(512 (Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(
510 ValType::Arr,513 ValType::Arr,
511 ValType::Num,514 ValType::Num,
514517
515 (Val::Str(s), Val::Num(n)) => Val::Str({518 (Val::Str(s), Val::Num(n)) => Val::Str({
516 let v: IStr = s519 let v: IStr = s
520 .clone()
521 .into_flat()
517 .chars()522 .chars()
518 .skip(n as usize)523 .skip(n as usize)
519 .take(1)524 .take(1)
520 .collect::<String>()525 .collect::<String>()
521 .into();526 .into();
522 if v.is_empty() {527 if v.is_empty() {
523 let size = s.chars().count();528 let size = s.into_flat().chars().count();
524 throw!(StringBoundsError(n as usize, size))529 throw!(StringBoundsError(n as usize, size))
525 }530 }
526 v531 StrValue::Flat(v)
527 }),532 }),
528 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(533 (Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(
529 ValType::Str,534 ValType::Str,
654 || format!("import {:?}", path.clone()),659 || format!("import {:?}", path.clone()),
655 || s.import_resolved(resolved_path),660 || s.import_resolved(resolved_path),
656 )?,661 )?,
657 ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),662 ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),
658 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),663 ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),
659 _ => unreachable!(),664 _ => unreachable!(),
660 }665 }
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
9 stdlib::std_format,
10 throw,
11 typed::Typed,
7 val::equals, Context, Result, Val,12 val::{equals, StrValue},
13 Context, Result, Val,
8};14};
915
25 Ok(match (a, b) {31 Ok(match (a, b) {
26 (Str(a), Str(b)) if a.is_empty() => Val::Str(b.clone()),32 (Str(a), Str(b)) if a.is_empty() => Val::Str(b.clone()),
27 (Str(a), Str(b)) if b.is_empty() => Val::Str(a.clone()),33 (Str(a), Str(b)) if b.is_empty() => Val::Str(a.clone()),
28 (Str(v1), Str(v2)) => Str(((**v1).to_owned() + v2).into()),34 (Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),
2935
30 // Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)36 // Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
31 (Num(a), Str(b)) => Str(format!("{a}{b}").into()),37 (Num(a), Str(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
32 (Str(a), Num(b)) => Str(format!("{a}{b}").into()),38 (Str(a), Num(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
3339
34 (Str(a), o) | (o, Str(a)) if a.is_empty() => Val::Str(o.clone().to_string()?),40 (Str(a), o) | (o, Str(a)) if a.is_empty() => {
41 Val::Str(StrValue::Flat(o.clone().to_string()?))
42 }
35 (Str(a), o) => Str(format!("{a}{}", o.clone().to_string()?).into()),43 (Str(a), o) => Str(StrValue::Flat(
44 format!("{a}{}", o.clone().to_string()?).into(),
45 )),
36 (o, Str(a)) => Str(format!("{}{a}", o.clone().to_string()?).into()),46 (o, Str(a)) => Str(StrValue::Flat(
47 format!("{}{a}", o.clone().to_string()?).into(),
48 )),
3749
38 (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),50 (Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),
39 (Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),51 (Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),
56 }68 }
57 Ok(Num(a % b))69 Ok(Num(a % b))
58 }70 }
59 (Str(str), vals) => String::into_untyped(std_format(str.clone(), vals.clone())?),71 (Str(str), vals) => {
72 String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)
73 }
60 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(74 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(
61 BinaryOpType::Mod,75 BinaryOpType::Mod,
62 a.value_type(),76 a.value_type(),
120 (a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),134 (a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),
121 (a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),135 (a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),
122136
123 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),137 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),
124 (a, Mod, b) => evaluate_mod_op(a, b)?,138 (a, Mod, b) => evaluate_mod_op(a, b)?,
125139
126 (Str(v1), Mul, Num(v2)) => Str(v1.repeat(*v2 as usize).into()),140 (Str(v1), Mul, Num(v2)) => Str(StrValue::Flat(v1.to_string().repeat(*v2 as usize).into())),
127141
128 // Bool X Bool142 // Bool X Bool
129 (Bool(a), And, Bool(b)) => Bool(*a && *b),143 (Bool(a), And, Bool(b)) => Bool(*a && *b),
modifiedcrates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth
9 gc::GcHashMap,
10 tb,
11 typed::Typed,
12 val::{StrValue, ThunkValue},
13 Context, Thunk, Val,
8};14};
915
59impl ArgLike for TlaArg {65impl ArgLike for TlaArg {
60 fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {66 fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
61 match self {67 match self {
62 TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(s.clone()))),68 TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(StrValue::Flat(s.clone())))),
63 TlaArg::Code(code) => Ok(if tailstrict {69 TlaArg::Code(code) => Ok(if tailstrict {
64 Thunk::evaluated(evaluate(ctx, code)?)70 Thunk::evaluated(evaluate(ctx, code)?)
65 } else {71 } else {
modifiedcrates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth
7 Deserialize, Serialize,7 Deserialize, Serialize,
8};8};
99
10use crate::{arr::ArrValue, error::Result, ObjValueBuilder, State, Val};10use crate::{arr::ArrValue, error::Result, val::StrValue, ObjValueBuilder, State, Val};
1111
12impl<'de> Deserialize<'de> for Val {12impl<'de> Deserialize<'de> for Val {
13 fn deserialize<D>(deserializer: D) -> Result<Val, D::Error>13 fn deserialize<D>(deserializer: D) -> Result<Val, D::Error>
49 where49 where
50 E: serde::de::Error,50 E: serde::de::Error,
51 {51 {
52 Ok(Val::Str(v.into()))52 Ok(Val::Str(StrValue::Flat(v.into())))
53 }53 }
5454
55 // visit_num! {55 // visit_num! {
152 match self {152 match self {
153 Val::Bool(v) => serializer.serialize_bool(*v),153 Val::Bool(v) => serializer.serialize_bool(*v),
154 Val::Null => serializer.serialize_none(),154 Val::Null => serializer.serialize_none(),
155 Val::Str(s) => serializer.serialize_str(s),155 Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()),
156 Val::Num(n) => serializer.serialize_f64(*n),156 Val::Num(n) => serializer.serialize_f64(*n),
157 Val::Arr(arr) => {157 Val::Arr(arr) => {
158 let mut seq = serializer.serialize_seq(Some(arr.len()))?;158 let mut seq = serializer.serialize_seq(Some(arr.len()))?;
modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
147 }147 }
148 }148 }
149 Val::Null => buf.push_str("null"),149 Val::Null => buf.push_str("null"),
150 Val::Str(s) => escape_string_json_buf(s, buf),150 Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),
151 Val::Num(n) => write!(buf, "{n}").unwrap(),151 Val::Num(n) => write!(buf, "{n}").unwrap(),
152 Val::Arr(items) => {152 Val::Arr(items) => {
153 buf.push('[');153 buf.push('[');
256 let Val::Str(s) = val else {256 let Val::Str(s) = val else {
257 throw!("output should be string for string manifest format, got {}", val.value_type())257 throw!("output should be string for string manifest format, got {}", val.value_type())
258 };258 };
259 out.write_str(&s).unwrap();259 write!(out, "{s}").unwrap();
260 Ok(())260 Ok(())
261 }261 }
262}262}
modifiedcrates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth
589 .ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,589 .ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,
590 ),590 ),
591 Val::Str(s) => {591 Val::Str(s) => {
592 let s = s.into_flat();
592 if s.chars().count() != 1 {593 if s.chars().count() != 1 {
593 throw!("%c expected 1 char string, got {}", s.chars().count(),);594 throw!("%c expected 1 char string, got {}", s.chars().count(),);
594 }595 }
modifiedcrates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth
2#![allow(clippy::unnecessary_wraps)]2#![allow(clippy::unnecessary_wraps)]
33
4use format::{format_arr, format_obj};4use format::{format_arr, format_obj};
5use jrsonnet_interner::IStr;
65
7use crate::{error::Result, function::CallLocation, State, Val};6use crate::{error::Result, function::CallLocation, State, Val};
87
9pub mod format;8pub mod format;
109
11pub fn std_format(str: IStr, vals: Val) -> Result<String> {10pub fn std_format(str: &str, vals: Val) -> Result<String> {
12 State::push(11 State::push(
13 CallLocation::native(),12 CallLocation::native(),
14 || format!("std.format of {str}"),13 || format!("std.format of {str}"),
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
11 function::{native::NativeDesc, FuncDesc, FuncVal},11 function::{native::NativeDesc, FuncDesc, FuncVal},
12 throw,12 throw,
13 typed::CheckType,13 typed::CheckType,
14 val::IndexableVal,14 val::{IndexableVal, StrValue},
15 ObjValue, ObjValueBuilder, Val,15 ObjValue, ObjValueBuilder, Val,
16};16};
1717
187 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);187 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
188188
189 fn into_untyped(value: Self) -> Result<Val> {189 fn into_untyped(value: Self) -> Result<Val> {
190 Ok(Val::Str(value))190 Ok(Val::Str(StrValue::Flat(value)))
191 }191 }
192192
193 fn from_untyped(value: Val) -> Result<Self> {193 fn from_untyped(value: Val) -> Result<Self> {
194 <Self as Typed>::TYPE.check(&value)?;194 <Self as Typed>::TYPE.check(&value)?;
195 match value {195 match value {
196 Val::Str(s) => Ok(s),196 Val::Str(s) => Ok(s.into_flat()),
197 _ => unreachable!(),197 _ => unreachable!(),
198 }198 }
199 }199 }
203 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);203 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
204204
205 fn into_untyped(value: Self) -> Result<Val> {205 fn into_untyped(value: Self) -> Result<Val> {
206 Ok(Val::Str(value.into()))206 Ok(Val::Str(StrValue::Flat(value.into())))
207 }207 }
208208
209 fn from_untyped(value: Val) -> Result<Self> {209 fn from_untyped(value: Val) -> Result<Self> {
219 const TYPE: &'static ComplexValType = &ComplexValType::Char;219 const TYPE: &'static ComplexValType = &ComplexValType::Char;
220220
221 fn into_untyped(value: Self) -> Result<Val> {221 fn into_untyped(value: Self) -> Result<Val> {
222 Ok(Val::Str(value.to_string().into()))222 Ok(Val::Str(StrValue::Flat(value.to_string().into())))
223 }223 }
224224
225 fn from_untyped(value: Val) -> Result<Self> {225 fn from_untyped(value: Val) -> Result<Self> {
226 <Self as Typed>::TYPE.check(&value)?;226 <Self as Typed>::TYPE.check(&value)?;
227 match value {227 match value {
228 Val::Str(s) => Ok(s.chars().next().unwrap()),228 Val::Str(s) => Ok(s.into_flat().chars().next().unwrap()),
229 _ => unreachable!(),229 _ => unreachable!(),
230 }230 }
231 }231 }
480480
481 fn into_untyped(value: Self) -> Result<Val> {481 fn into_untyped(value: Self) -> Result<Val> {
482 match value {482 match value {
483 IndexableVal::Str(s) => Ok(Val::Str(s)),483 IndexableVal::Str(s) => Ok(Val::Str(StrValue::Flat(s))),
484 IndexableVal::Arr(a) => Ok(Val::Arr(a)),484 IndexableVal::Arr(a) => Ok(Val::Arr(a)),
485 }485 }
486 }486 }
modifiedcrates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth
150 Self::Any => Ok(()),150 Self::Any => Ok(()),
151 Self::Simple(t) => t.check(value),151 Self::Simple(t) => t.check(value),
152 Self::Char => match value {152 Self::Char => match value {
153 Val::Str(s) if s.len() == 1 || s.chars().count() == 1 => Ok(()),153 Val::Str(s) if s.len() == 1 || s.clone().into_flat().chars().count() == 1 => Ok(()),
154 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),154 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
155 },155 },
156 Self::BoundedNumber(from, to) => {156 Self::BoundedNumber(from, to) => {
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
1use std::{cell::RefCell, fmt::Debug, mem::replace};1use std::{
2 cell::RefCell,
3 fmt::{self, Debug, Display},
4 mem::replace,
5 rc::Rc,
6};
27
3use jrsonnet_gcmodule::{Cc, Trace};8use jrsonnet_gcmodule::{Cc, Trace};
117}122}
118123
119impl<T: Debug + Trace> Debug for Thunk<T> {124impl<T: Debug + Trace> Debug for Thunk<T> {
120 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 write!(f, "Lazy")126 write!(f, "Lazy")
122 }127 }
123}128}
187 }192 }
188}193}
194
195#[derive(Debug, Clone, Trace)]
196pub enum StrValue {
197 Flat(IStr),
198 Tree(Rc<(StrValue, StrValue, usize)>),
199}
200impl StrValue {
201 pub fn concat(a: StrValue, b: StrValue) -> Self {
202 if a.is_empty() {
203 b
204 } else if b.is_empty() {
205 a
206 } else {
207 let len = a.len() + b.len();
208 Self::Tree(Rc::new((a, b, len)))
209 }
210 }
211 pub fn into_flat(self) -> IStr {
212 match self {
213 StrValue::Flat(f) => f,
214 StrValue::Tree(_) => {
215 let mut buf = String::new();
216 self.into_flat_buf(&mut buf);
217 buf.into()
218 }
219 }
220 }
221 fn into_flat_buf(&self, out: &mut String) {
222 match self {
223 StrValue::Flat(f) => out.push_str(f),
224 StrValue::Tree(t) => {
225 t.0.into_flat_buf(out);
226 t.1.into_flat_buf(out);
227 }
228 }
229 }
230 pub fn len(&self) -> usize {
231 match self {
232 StrValue::Flat(v) => v.len(),
233 StrValue::Tree(t) => t.2,
234 }
235 }
236 pub fn is_empty(&self) -> bool {
237 match self {
238 Self::Flat(v) => v.is_empty(),
239 _ => false,
240 }
241 }
242}
243impl Display for StrValue {
244 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
245 match self {
246 StrValue::Flat(v) => write!(f, "{v}"),
247 StrValue::Tree(t) => {
248 write!(f, "{}", t.0)?;
249 write!(f, "{}", t.1)
250 }
251 }
252 }
253}
254impl PartialEq for StrValue {
255 fn eq(&self, other: &Self) -> bool {
256 let a = self.clone().into_flat();
257 let b = other.clone().into_flat();
258 a == b
259 }
260}
261impl Eq for StrValue {}
262impl PartialOrd for StrValue {
263 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
264 let a = self.clone().into_flat();
265 let b = other.clone().into_flat();
266 Some(a.cmp(&b))
267 }
268}
269impl Ord for StrValue {
270 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
271 self.partial_cmp(other)
272 .expect("partial_cmp always returns Some")
273 }
274}
189275
190/// Represents any valid Jsonnet value.276/// Represents any valid Jsonnet value.
191#[derive(Debug, Clone, Trace)]277#[derive(Debug, Clone, Trace)]
195 /// Represents a Jsonnet null value.281 /// Represents a Jsonnet null value.
196 Null,282 Null,
197 /// Represents a Jsonnet string.283 /// Represents a Jsonnet string.
198 Str(IStr),284 Str(StrValue),
199 /// Represents a Jsonnet number.285 /// Represents a Jsonnet number.
200 /// Should be finite, and not NaN286 /// Should be finite, and not NaN
201 /// This restriction isn't enforced by enum, as enum field can't be marked as private287 /// This restriction isn't enforced by enum, as enum field can't be marked as private
208 Func(FuncVal),294 Func(FuncVal),
209}295}
296
297static_assertions::assert_eq_size!(Val, [u8; 24]);
210298
211impl From<IndexableVal> for Val {299impl From<IndexableVal> for Val {
212 fn from(v: IndexableVal) -> Self {300 fn from(v: IndexableVal) -> Self {
213 match v {301 match v {
214 IndexableVal::Str(s) => Self::Str(s),302 IndexableVal::Str(s) => Self::Str(StrValue::Flat(s)),
215 IndexableVal::Arr(a) => Self::Arr(a),303 IndexableVal::Arr(a) => Self::Arr(a),
216 }304 }
217 }305 }
232 }320 }
233 pub fn as_str(&self) -> Option<IStr> {321 pub fn as_str(&self) -> Option<IStr> {
234 match self {322 match self {
235 Self::Str(s) => Some(s.clone()),323 Self::Str(s) => Some(s.clone().into_flat()),
236 _ => None,324 _ => None,
237 }325 }
238 }326 }
295 Self::Bool(true) => "true".into(),383 Self::Bool(true) => "true".into(),
296 Self::Bool(false) => "false".into(),384 Self::Bool(false) => "false".into(),
297 Self::Null => "null".into(),385 Self::Null => "null".into(),
298 Self::Str(s) => s.clone(),386 Self::Str(s) => s.clone().into_flat(),
299 _ => self.manifest(ToStringFormat).map(IStr::from)?,387 _ => self.manifest(ToStringFormat).map(IStr::from)?,
300 })388 })
301 }389 }
302390
303 pub fn into_indexable(self) -> Result<IndexableVal> {391 pub fn into_indexable(self) -> Result<IndexableVal> {
304 Ok(match self {392 Ok(match self {
305 Val::Str(s) => IndexableVal::Str(s),393 Val::Str(s) => IndexableVal::Str(s.into_flat()),
306 Val::Arr(arr) => IndexableVal::Arr(arr),394 Val::Arr(arr) => IndexableVal::Arr(arr),
307 _ => throw!(ValueIsNotIndexable(self.value_type())),395 _ => throw!(ValueIsNotIndexable(self.value_type())),
308 })396 })
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
37 func: NativeFn<((Either![String, Any],), Any)>,37 func: NativeFn<((Either![String, Any],), Any)>,
38 arr: IndexableVal,38 arr: IndexableVal,
39) -> Result<IndexableVal> {39) -> Result<IndexableVal> {
40 use std::fmt::Write;
40 match arr {41 match arr {
41 IndexableVal::Str(str) => {42 IndexableVal::Str(str) => {
42 let mut out = String::new();43 let mut out = String::new();
43 for c in str.chars() {44 for c in str.chars() {
44 match func(Either2::A(c.to_string()))?.0 {45 match func(Either2::A(c.to_string()))?.0 {
45 Val::Str(o) => out.push_str(&o),46 Val::Str(o) => write!(out, "{o}").unwrap(),
46 Val::Null => continue,47 Val::Null => continue,
47 _ => throw!("in std.join all items should be strings"),48 _ => throw!("in std.join all items should be strings"),
48 };49 };
101102
102#[builtin]103#[builtin]
103pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {104pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {
105 use std::fmt::Write;
104 Ok(match sep {106 Ok(match sep {
105 IndexableVal::Arr(joiner_items) => {107 IndexableVal::Arr(joiner_items) => {
106 let mut out = Vec::new();108 let mut out = Vec::new();
141 out += &sep;143 out += &sep;
142 }144 }
143 first = false;145 first = false;
144 out += &item;146 write!(out, "{item}").unwrap()
145 } else if matches!(item, Val::Null) {147 } else if matches!(item, Val::Null) {
146 continue;148 continue;
147 } else {149 } else {
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
320 }320 }
321 #[cfg(feature = "legacy-this-file")]321 #[cfg(feature = "legacy-this-file")]
322 fn initialize(&self, s: State, source: Source) -> Context {322 fn initialize(&self, s: State, source: Source) -> Context {
323 use jrsonnet_evaluator::val::StrValue;
324
323 let mut builder = ObjValueBuilder::new();325 let mut builder = ObjValueBuilder::new();
324 builder.with_super(self.stdlib_obj.clone());326 builder.with_super(self.stdlib_obj.clone());
325 builder327 builder
326 .member("thisFile".into())328 .member("thisFile".into())
327 .hide()329 .hide()
328 .value(Val::Str(match source.source_path().path() {330 .value(Val::Str(StrValue::Flat(
331 match source.source_path().path() {
329 Some(p) => self.settings().path_resolver.resolve(p).into(),332 Some(p) => self.settings().path_resolver.resolve(p).into(),
330 None => source.source_path().to_string().into(),333 None => source.source_path().to_string().into(),
331 }))334 },
335 )))
332 .expect("this object builder is empty");336 .expect("this object builder is empty");
333 let stdlib_with_this_file = builder.build();337 let stdlib_with_this_file = builder.build();
334338
modifiedcrates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth
118 }118 }
119 Val::Null => buf.push_str("null"),119 Val::Null => buf.push_str("null"),
120 Val::Str(s) => {120 Val::Str(s) => {
121 let s = s.clone().into_flat();
121 if s.is_empty() {122 if s.is_empty() {
122 buf.push_str("\"\"");123 buf.push_str("\"\"");
123 } else if let Some(s) = s.strip_suffix('\n') {124 } else if let Some(s) = s.strip_suffix('\n') {
128 buf.push_str(&options.padding);129 buf.push_str(&options.padding);
129 buf.push_str(line);130 buf.push_str(line);
130 }131 }
131 } else if !options.quote_keys && !yaml_needs_quotes(s) {132 } else if !options.quote_keys && !yaml_needs_quotes(&s) {
132 buf.push_str(s);133 buf.push_str(&s);
133 } else {134 } else {
134 escape_string_json_buf(s, buf);135 escape_string_json_buf(&s, buf);
135 }136 }
136 }137 }
137 Val::Num(n) => write!(buf, "{}", *n).unwrap(),138 Val::Num(n) => write!(buf, "{}", *n).unwrap(),
modifiedcrates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth
2 error::Result, function::builtin, typed::VecVal, val::Val, IStr, ObjValue,2 error::Result,
3 function::builtin,
4 typed::VecVal,
5 val::{StrValue, Val},
6 IStr, ObjValue,
3};7};
4use jrsonnet_gcmodule::Cc;8use jrsonnet_gcmodule::Cc;
18 );22 );
19 Ok(VecVal(Cc::new(23 Ok(VecVal(Cc::new(
20 out.into_iter().map(Val::Str).collect::<Vec<_>>(),24 out.into_iter()
25 .map(StrValue::Flat)
26 .map(Val::Str)
27 .collect::<Vec<_>>(),
21 )))28 )))
modifiedcrates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth
7 operator::evaluate_mod_op,7 operator::evaluate_mod_op,
8 stdlib::std_format,8 stdlib::std_format,
9 typed::{Any, Either, Either2},9 typed::{Any, Either, Either2},
10 val::{equals, primitive_equals},10 val::{equals, primitive_equals, StrValue},
11 IStr, Val,11 IStr, Val,
12};12};
1313
17 Ok(Any(evaluate_mod_op(17 Ok(Any(evaluate_mod_op(
18 &match a {18 &match a {
19 A(v) => Val::Num(v),19 A(v) => Val::Num(v),
20 B(s) => Val::Str(s),20 B(s) => Val::Str(StrValue::Flat(s)),
21 },21 },
22 &b.0,22 &b.0,
23 )?))23 )?))
3535
36#[builtin]36#[builtin]
37pub fn builtin_format(str: IStr, vals: Any) -> Result<String> {37pub fn builtin_format(str: IStr, vals: Any) -> Result<String> {
38 std_format(str, vals.0)38 std_format(&str, vals.0)
39}39}
4040
modifiedcrates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth
3 function::builtin,3 function::builtin,
4 throw,4 throw,
5 typed::{Either2, VecVal, M1},5 typed::{Either2, VecVal, M1},
6 val::ArrValue,6 val::{ArrValue, StrValue},
7 Either, IStr, Val,7 Either, IStr, Val,
8};8};
9use jrsonnet_gcmodule::Cc;9use jrsonnet_gcmodule::Cc;
34 Ok(VecVal(Cc::new(match maxsplits {34 Ok(VecVal(Cc::new(match maxsplits {
35 A(n) => str35 A(n) => str
36 .splitn(n + 1, &c as &str)36 .splitn(n + 1, &c as &str)
37 .map(|s| Val::Str(s.into()))37 .map(|s| Val::Str(StrValue::Flat(s.into())))
38 .collect(),38 .collect(),
39 B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),39 B(_) => str
40 .split(&c as &str)
41 .map(|s| Val::Str(StrValue::Flat(s.into())))
42 .collect(),
40 })))43 })))
41}44}