difftreelog
refactor extended strings
in: master
16 files changed
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -17,7 +17,7 @@
function::{CallLocation, FuncDesc, FuncVal},
tb, throw,
typed::Typed,
- val::{CachedUnbound, IndexableVal, Thunk, ThunkValue},
+ val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},
Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,
Unbound, Val,
};
@@ -36,7 +36,7 @@
}
}
Some(match &*expr.0 {
- Expr::Str(s) => Val::Str(s.clone()),
+ Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),
Expr::Num(n) => Val::Num(*n),
Expr::Literal(LiteralType::False) => Val::Bool(false),
Expr::Literal(LiteralType::True) => Val::Bool(true),
@@ -135,7 +135,7 @@
let fctx = Pending::new();
let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());
let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![
- Thunk::evaluated(Val::Str(field.clone())),
+ Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),
Thunk::new(tb!(ObjectFieldThunk {
field: field.clone(),
obj: obj.clone(),
@@ -436,7 +436,7 @@
Literal(LiteralType::False) => Val::Bool(false),
Literal(LiteralType::Null) => Val::Null,
Parened(e) => evaluate(ctx, e)?,
- Str(v) => Val::Str(v.clone()),
+ Str(v) => Val::Str(StrValue::Flat(v.clone())),
Num(v) => Val::new_checked_num(*v)?,
BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
@@ -457,14 +457,14 @@
ctx.super_obj()
.clone()
.expect("no super found")
- .get_for(name, ctx.this().clone().expect("no this found"))?
+ .get_for(name.into_flat(), ctx.this().clone().expect("no this found"))?
.expect("value not found")
}
Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {
(Val::Obj(v), Val::Str(key)) => State::push(
CallLocation::new(loc),
|| format!("field <{key}> access"),
- || match v.get(key.clone()) {
+ || match v.get(key.clone().into_flat()) {
Ok(Some(v)) => Ok(v),
#[cfg(not(feature = "friendly-errors"))]
Ok(None) => throw!(NoSuchField(key.clone(), vec![])),
@@ -476,7 +476,10 @@
#[cfg(feature = "exp-preserve-order")]
false,
) {
- let conf = strsim::jaro_winkler(&field as &str, &key as &str);
+ let conf = strsim::jaro_winkler(
+ &field as &str,
+ &key.clone().into_flat() as &str,
+ );
if conf < 0.8 {
continue;
}
@@ -485,7 +488,7 @@
heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));
throw!(NoSuchField(
- key.clone(),
+ key.clone().into_flat(),
heap.into_iter().map(|(_, v)| v).collect()
))
}
@@ -505,7 +508,7 @@
v.get(n as usize)?
.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?
}
- (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),
+ (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n.into_flat())),
(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(
ValType::Arr,
ValType::Num,
@@ -514,16 +517,18 @@
(Val::Str(s), Val::Num(n)) => Val::Str({
let v: IStr = s
+ .clone()
+ .into_flat()
.chars()
.skip(n as usize)
.take(1)
.collect::<String>()
.into();
if v.is_empty() {
- let size = s.chars().count();
+ let size = s.into_flat().chars().count();
throw!(StringBoundsError(n as usize, size))
}
- v
+ StrValue::Flat(v)
}),
(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(
ValType::Str,
@@ -654,7 +659,7 @@
|| format!("import {:?}", path.clone()),
|| s.import_resolved(resolved_path),
)?,
- ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),
+ ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),
ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),
_ => unreachable!(),
}
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -3,8 +3,14 @@
use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
use crate::{
- arr::ArrValue, error::ErrorKind::*, evaluate, stdlib::std_format, throw, typed::Typed,
- val::equals, Context, Result, Val,
+ arr::ArrValue,
+ error::ErrorKind::*,
+ evaluate,
+ stdlib::std_format,
+ throw,
+ typed::Typed,
+ val::{equals, StrValue},
+ Context, Result, Val,
};
pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
@@ -25,15 +31,21 @@
Ok(match (a, b) {
(Str(a), Str(b)) if a.is_empty() => Val::Str(b.clone()),
(Str(a), Str(b)) if b.is_empty() => Val::Str(a.clone()),
- (Str(v1), Str(v2)) => Str(((**v1).to_owned() + v2).into()),
+ (Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),
// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
- (Num(a), Str(b)) => Str(format!("{a}{b}").into()),
- (Str(a), Num(b)) => Str(format!("{a}{b}").into()),
+ (Num(a), Str(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
+ (Str(a), Num(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
- (Str(a), o) | (o, Str(a)) if a.is_empty() => Val::Str(o.clone().to_string()?),
- (Str(a), o) => Str(format!("{a}{}", o.clone().to_string()?).into()),
- (o, Str(a)) => Str(format!("{}{a}", o.clone().to_string()?).into()),
+ (Str(a), o) | (o, Str(a)) if a.is_empty() => {
+ Val::Str(StrValue::Flat(o.clone().to_string()?))
+ }
+ (Str(a), o) => Str(StrValue::Flat(
+ format!("{a}{}", o.clone().to_string()?).into(),
+ )),
+ (o, Str(a)) => Str(StrValue::Flat(
+ format!("{}{a}", o.clone().to_string()?).into(),
+ )),
(Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),
(Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),
@@ -56,7 +68,9 @@
}
Ok(Num(a % b))
}
- (Str(str), vals) => String::into_untyped(std_format(str.clone(), vals.clone())?),
+ (Str(str), vals) => {
+ String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)
+ }
(a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(
BinaryOpType::Mod,
a.value_type(),
@@ -120,10 +134,10 @@
(a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),
(a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),
- (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
+ (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),
(a, Mod, b) => evaluate_mod_op(a, b)?,
- (Str(v1), Mul, Num(v2)) => Str(v1.repeat(*v2 as usize).into()),
+ (Str(v1), Mul, Num(v2)) => Str(StrValue::Flat(v1.to_string().repeat(*v2 as usize).into())),
// Bool X Bool
(Bool(a), And, Bool(b)) => Bool(*a && *b),
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -4,7 +4,13 @@
use jrsonnet_parser::{ArgsDesc, LocExpr};
use crate::{
- error::Result, evaluate, gc::GcHashMap, tb, typed::Typed, val::ThunkValue, Context, Thunk, Val,
+ error::Result,
+ evaluate,
+ gc::GcHashMap,
+ tb,
+ typed::Typed,
+ val::{StrValue, ThunkValue},
+ Context, Thunk, Val,
};
/// Marker for arguments, which can be evaluated with context set to None
@@ -59,7 +65,7 @@
impl ArgLike for TlaArg {
fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
match self {
- TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(s.clone()))),
+ TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(StrValue::Flat(s.clone())))),
TlaArg::Code(code) => Ok(if tailstrict {
Thunk::evaluated(evaluate(ctx, code)?)
} else {
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -7,7 +7,7 @@
Deserialize, Serialize,
};
-use crate::{arr::ArrValue, error::Result, ObjValueBuilder, State, Val};
+use crate::{arr::ArrValue, error::Result, val::StrValue, ObjValueBuilder, State, Val};
impl<'de> Deserialize<'de> for Val {
fn deserialize<D>(deserializer: D) -> Result<Val, D::Error>
@@ -49,7 +49,7 @@
where
E: serde::de::Error,
{
- Ok(Val::Str(v.into()))
+ Ok(Val::Str(StrValue::Flat(v.into())))
}
// visit_num! {
@@ -152,7 +152,7 @@
match self {
Val::Bool(v) => serializer.serialize_bool(*v),
Val::Null => serializer.serialize_none(),
- Val::Str(s) => serializer.serialize_str(s),
+ Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()),
Val::Num(n) => serializer.serialize_f64(*n),
Val::Arr(arr) => {
let mut seq = serializer.serialize_seq(Some(arr.len()))?;
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -147,7 +147,7 @@
}
}
Val::Null => buf.push_str("null"),
- Val::Str(s) => escape_string_json_buf(s, buf),
+ Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),
Val::Num(n) => write!(buf, "{n}").unwrap(),
Val::Arr(items) => {
buf.push('[');
@@ -256,7 +256,7 @@
let Val::Str(s) = val else {
throw!("output should be string for string manifest format, got {}", val.value_type())
};
- out.write_str(&s).unwrap();
+ write!(out, "{s}").unwrap();
Ok(())
}
}
crates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -589,6 +589,7 @@
.ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,
),
Val::Str(s) => {
+ let s = s.into_flat();
if s.chars().count() != 1 {
throw!("%c expected 1 char string, got {}", s.chars().count(),);
}
crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -2,13 +2,12 @@
#![allow(clippy::unnecessary_wraps)]
use format::{format_arr, format_obj};
-use jrsonnet_interner::IStr;
use crate::{error::Result, function::CallLocation, State, Val};
pub mod format;
-pub fn std_format(str: IStr, vals: Val) -> Result<String> {
+pub fn std_format(str: &str, vals: Val) -> Result<String> {
State::push(
CallLocation::native(),
|| format!("std.format of {str}"),
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth1use std::ops::Deref;23use jrsonnet_gcmodule::Cc;4use jrsonnet_interner::{IBytes, IStr};5pub use jrsonnet_macros::Typed;6use jrsonnet_types::{ComplexValType, ValType};78use crate::{9 arr::ArrValue,10 error::Result,11 function::{native::NativeDesc, FuncDesc, FuncVal},12 throw,13 typed::CheckType,14 val::IndexableVal,15 ObjValue, ObjValueBuilder, Val,16};1718pub trait TypedObj: Typed {19 fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;20 fn parse(obj: &ObjValue) -> Result<Self>;21 fn into_object(self) -> Result<ObjValue> {22 let mut builder = ObjValueBuilder::new();23 self.serialize(&mut builder)?;24 Ok(builder.build())25 }26}2728pub trait Typed: Sized {29 const TYPE: &'static ComplexValType;30 fn into_untyped(typed: Self) -> Result<Val>;31 fn from_untyped(untyped: Val) -> Result<Self>;32}3334const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS + 1)) - 1) as f64;3536macro_rules! impl_int {37 ($($ty:ty)*) => {$(38 impl Typed for $ty {39 const TYPE: &'static ComplexValType =40 &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));41 fn from_untyped(value: Val) -> Result<Self> {42 <Self as Typed>::TYPE.check(&value)?;43 match value {44 Val::Num(n) => {45 #[allow(clippy::float_cmp)]46 if n.trunc() != n {47 throw!(48 "cannot convert number with fractional part to {}",49 stringify!($ty)50 )51 }52 Ok(n as Self)53 }54 _ => unreachable!(),55 }56 }57 fn into_untyped(value: Self) -> Result<Val> {58 Ok(Val::Num(value as f64))59 }60 }61 )*};62}6364impl_int!(i8 u8 i16 u16 i32 u32);6566macro_rules! impl_bounded_int {67 ($($name:ident = $ty:ty)*) => {$(68 #[derive(Clone, Copy)]69 pub struct $name<const MIN: $ty, const MAX: $ty>($ty);70 impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {71 pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {72 if value >= MIN && value <= MAX {73 Some(Self(value))74 } else {75 None76 }77 }78 pub const fn value(self) -> $ty {79 self.080 }81 }82 impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {83 type Target = $ty;84 fn deref(&self) -> &Self::Target {85 &self.086 }87 }8889 impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {90 const TYPE: &'static ComplexValType =91 &ComplexValType::BoundedNumber(92 Some(MIN as f64),93 Some(MAX as f64),94 );9596 fn from_untyped(value: Val) -> Result<Self> {97 <Self as Typed>::TYPE.check(&value)?;98 match value {99 Val::Num(n) => {100 #[allow(clippy::float_cmp)]101 if n.trunc() != n {102 throw!(103 "cannot convert number with fractional part to {}",104 stringify!($ty)105 )106 }107 Ok(Self(n as $ty))108 }109 _ => unreachable!(),110 }111 }112113 fn into_untyped(value: Self) -> Result<Val> {114 Ok(Val::Num(value.0 as f64))115 }116 }117 )*};118}119120impl_bounded_int!(121 BoundedI8 = i8122 BoundedI16 = i16123 BoundedI32 = i32124 BoundedI64 = i64125 BoundedUsize = usize126);127128impl Typed for f64 {129 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);130131 fn into_untyped(value: Self) -> Result<Val> {132 Ok(Val::Num(value))133 }134135 fn from_untyped(value: Val) -> Result<Self> {136 <Self as Typed>::TYPE.check(&value)?;137 match value {138 Val::Num(n) => Ok(n),139 _ => unreachable!(),140 }141 }142}143144pub struct PositiveF64(pub f64);145impl Typed for PositiveF64 {146 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);147148 fn into_untyped(value: Self) -> Result<Val> {149 Ok(Val::Num(value.0))150 }151152 fn from_untyped(value: Val) -> Result<Self> {153 <Self as Typed>::TYPE.check(&value)?;154 match value {155 Val::Num(n) => Ok(Self(n)),156 _ => unreachable!(),157 }158 }159}160impl Typed for usize {161 const TYPE: &'static ComplexValType =162 &ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));163164 fn into_untyped(value: Self) -> Result<Val> {165 if value > MAX_SAFE_INTEGER as Self {166 throw!("number is too large")167 }168 Ok(Val::Num(value as f64))169 }170171 fn from_untyped(value: Val) -> Result<Self> {172 <Self as Typed>::TYPE.check(&value)?;173 match value {174 Val::Num(n) => {175 #[allow(clippy::float_cmp)]176 if n.trunc() != n {177 throw!("cannot convert number with fractional part to usize")178 }179 Ok(n as Self)180 }181 _ => unreachable!(),182 }183 }184}185186impl Typed for IStr {187 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);188189 fn into_untyped(value: Self) -> Result<Val> {190 Ok(Val::Str(value))191 }192193 fn from_untyped(value: Val) -> Result<Self> {194 <Self as Typed>::TYPE.check(&value)?;195 match value {196 Val::Str(s) => Ok(s),197 _ => unreachable!(),198 }199 }200}201202impl Typed for String {203 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);204205 fn into_untyped(value: Self) -> Result<Val> {206 Ok(Val::Str(value.into()))207 }208209 fn from_untyped(value: Val) -> Result<Self> {210 <Self as Typed>::TYPE.check(&value)?;211 match value {212 Val::Str(s) => Ok(s.to_string()),213 _ => unreachable!(),214 }215 }216}217218impl Typed for char {219 const TYPE: &'static ComplexValType = &ComplexValType::Char;220221 fn into_untyped(value: Self) -> Result<Val> {222 Ok(Val::Str(value.to_string().into()))223 }224225 fn from_untyped(value: Val) -> Result<Self> {226 <Self as Typed>::TYPE.check(&value)?;227 match value {228 Val::Str(s) => Ok(s.chars().next().unwrap()),229 _ => unreachable!(),230 }231 }232}233234impl<T> Typed for Vec<T>235where236 T: Typed,237{238 const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);239240 fn into_untyped(value: Self) -> Result<Val> {241 let mut o = Vec::with_capacity(value.len());242 for i in value {243 o.push(T::into_untyped(i)?);244 }245 Ok(Val::Arr(o.into()))246 }247248 fn from_untyped(value: Val) -> Result<Self> {249 <Self as Typed>::TYPE.check(&value)?;250 match value {251 Val::Arr(a) => {252 let mut o = Self::with_capacity(a.len());253 for i in a.iter() {254 o.push(T::from_untyped(i?)?);255 }256 Ok(o)257 }258 _ => unreachable!(),259 }260 }261}262263/// To be used in Vec<Any>264/// Regular Val can't be used here, because it has wrong `TryFrom::Error` type265#[derive(Clone)]266pub struct Any(pub Val);267268impl Typed for Any {269 const TYPE: &'static ComplexValType = &ComplexValType::Any;270271 fn into_untyped(value: Self) -> Result<Val> {272 Ok(value.0)273 }274275 fn from_untyped(value: Val) -> Result<Self> {276 Ok(Self(value))277 }278}279280/// Specialization, provides faster `TryFrom<VecVal>` for Val281pub struct VecVal(pub Cc<Vec<Val>>);282283impl Typed for VecVal {284 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);285286 fn into_untyped(value: Self) -> Result<Val> {287 Ok(Val::Arr(ArrValue::eager(value.0)))288 }289290 fn from_untyped(value: Val) -> Result<Self> {291 <Self as Typed>::TYPE.check(&value)?;292 match value {293 Val::Arr(a) => Ok(Self(a.evaluatedcc()?)),294 _ => unreachable!(),295 }296 }297}298299/// Specialization300impl Typed for IBytes {301 const TYPE: &'static ComplexValType =302 &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));303304 fn into_untyped(value: Self) -> Result<Val> {305 Ok(Val::Arr(ArrValue::bytes(value)))306 }307308 fn from_untyped(value: Val) -> Result<Self> {309 if let Val::Arr(ArrValue::Bytes(bytes)) = value {310 return Ok(bytes.0);311 }312 <Self as Typed>::TYPE.check(&value)?;313 match value {314 Val::Arr(a) => {315 let mut out = Vec::with_capacity(a.len());316 for e in a.iter() {317 let r = e?;318 out.push(u8::from_untyped(r)?);319 }320 Ok(out.as_slice().into())321 }322 _ => unreachable!(),323 }324 }325}326327pub struct M1;328impl Typed for M1 {329 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));330331 fn into_untyped(_: Self) -> Result<Val> {332 Ok(Val::Num(-1.0))333 }334335 fn from_untyped(value: Val) -> Result<Self> {336 <Self as Typed>::TYPE.check(&value)?;337 Ok(Self)338 }339}340341macro_rules! decl_either {342 ($($name: ident, $($id: ident)*);*) => {$(343 #[derive(Clone)]344 pub enum $name<$($id),*> {345 $($id($id)),*346 }347 impl<$($id),*> Typed for $name<$($id),*>348 where349 $($id: Typed,)*350 {351 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);352353 fn into_untyped(value: Self) -> Result<Val> {354 match value {$(355 $name::$id(v) => $id::into_untyped(v)356 ),*}357 }358359 fn from_untyped(value: Val) -> Result<Self> {360 $(361 if $id::TYPE.check(&value).is_ok() {362 $id::from_untyped(value).map(Self::$id)363 } else364 )* {365 <Self as Typed>::TYPE.check(&value)?;366 unreachable!()367 }368 }369 }370 )*}371}372decl_either!(373 Either1, A;374 Either2, A B;375 Either3, A B C;376 Either4, A B C D;377 Either5, A B C D E;378 Either6, A B C D E F;379 Either7, A B C D E F G380);381#[macro_export]382macro_rules! Either {383 ($a:ty) => {Either1<$a>};384 ($a:ty, $b:ty) => {Either2<$a, $b>};385 ($a:ty, $b:ty, $c:ty) => {Either3<$a, $b, $c>};386 ($a:ty, $b:ty, $c:ty, $d:ty) => {Either4<$a, $b, $c, $d>};387 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {Either5<$a, $b, $c, $d, $e>};388 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {Either6<$a, $b, $c, $d, $e, $f>};389 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {Either7<$a, $b, $c, $d, $e, $f, $g>};390}391pub use Either;392393pub type MyType = Either![u32, f64, String];394395impl Typed for ArrValue {396 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);397398 fn into_untyped(value: Self) -> Result<Val> {399 Ok(Val::Arr(value))400 }401402 fn from_untyped(value: Val) -> Result<Self> {403 <Self as Typed>::TYPE.check(&value)?;404 match value {405 Val::Arr(a) => Ok(a),406 _ => unreachable!(),407 }408 }409}410411impl Typed for FuncVal {412 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);413414 fn into_untyped(value: Self) -> Result<Val> {415 Ok(Val::Func(value))416 }417418 fn from_untyped(value: Val) -> Result<Self> {419 <Self as Typed>::TYPE.check(&value)?;420 match value {421 Val::Func(a) => Ok(a),422 _ => unreachable!(),423 }424 }425}426427impl Typed for Cc<FuncDesc> {428 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);429430 fn into_untyped(value: Self) -> Result<Val> {431 Ok(Val::Func(FuncVal::Normal(value)))432 }433434 fn from_untyped(value: Val) -> Result<Self> {435 <Self as Typed>::TYPE.check(&value)?;436 match value {437 Val::Func(FuncVal::Normal(desc)) => Ok(desc),438 Val::Func(_) => throw!("expected normal function, not builtin"),439 _ => unreachable!(),440 }441 }442}443444impl Typed for ObjValue {445 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);446447 fn into_untyped(value: Self) -> Result<Val> {448 Ok(Val::Obj(value))449 }450451 fn from_untyped(value: Val) -> Result<Self> {452 <Self as Typed>::TYPE.check(&value)?;453 match value {454 Val::Obj(a) => Ok(a),455 _ => unreachable!(),456 }457 }458}459460impl Typed for bool {461 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);462463 fn into_untyped(value: Self) -> Result<Val> {464 Ok(Val::Bool(value))465 }466467 fn from_untyped(value: Val) -> Result<Self> {468 <Self as Typed>::TYPE.check(&value)?;469 match value {470 Val::Bool(a) => Ok(a),471 _ => unreachable!(),472 }473 }474}475impl Typed for IndexableVal {476 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[477 &ComplexValType::Simple(ValType::Arr),478 &ComplexValType::Simple(ValType::Str),479 ]);480481 fn into_untyped(value: Self) -> Result<Val> {482 match value {483 IndexableVal::Str(s) => Ok(Val::Str(s)),484 IndexableVal::Arr(a) => Ok(Val::Arr(a)),485 }486 }487488 fn from_untyped(value: Val) -> Result<Self> {489 <Self as Typed>::TYPE.check(&value)?;490 value.into_indexable()491 }492}493494pub struct Null;495impl Typed for Null {496 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);497498 fn into_untyped(_: Self) -> Result<Val> {499 Ok(Val::Null)500 }501502 fn from_untyped(value: Val) -> Result<Self> {503 <Self as Typed>::TYPE.check(&value)?;504 Ok(Self)505 }506}507508pub struct NativeFn<D: NativeDesc>(D::Value);509impl<D: NativeDesc> Deref for NativeFn<D> {510 type Target = D::Value;511512 fn deref(&self) -> &Self::Target {513 &self.0514 }515}516impl<D: NativeDesc> Typed for NativeFn<D> {517 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);518519 fn into_untyped(_typed: Self) -> Result<Val> {520 throw!("can only convert functions from jsonnet to native")521 }522523 fn from_untyped(untyped: Val) -> Result<Self> {524 Ok(Self(525 untyped526 .as_func()527 .expect("shape is checked")528 .into_native::<D>(),529 ))530 }531}1use std::ops::Deref;23use jrsonnet_gcmodule::Cc;4use jrsonnet_interner::{IBytes, IStr};5pub use jrsonnet_macros::Typed;6use jrsonnet_types::{ComplexValType, ValType};78use crate::{9 arr::ArrValue,10 error::Result,11 function::{native::NativeDesc, FuncDesc, FuncVal},12 throw,13 typed::CheckType,14 val::{IndexableVal, StrValue},15 ObjValue, ObjValueBuilder, Val,16};1718pub trait TypedObj: Typed {19 fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;20 fn parse(obj: &ObjValue) -> Result<Self>;21 fn into_object(self) -> Result<ObjValue> {22 let mut builder = ObjValueBuilder::new();23 self.serialize(&mut builder)?;24 Ok(builder.build())25 }26}2728pub trait Typed: Sized {29 const TYPE: &'static ComplexValType;30 fn into_untyped(typed: Self) -> Result<Val>;31 fn from_untyped(untyped: Val) -> Result<Self>;32}3334const MAX_SAFE_INTEGER: f64 = ((1u64 << (f64::MANTISSA_DIGITS + 1)) - 1) as f64;3536macro_rules! impl_int {37 ($($ty:ty)*) => {$(38 impl Typed for $ty {39 const TYPE: &'static ComplexValType =40 &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));41 fn from_untyped(value: Val) -> Result<Self> {42 <Self as Typed>::TYPE.check(&value)?;43 match value {44 Val::Num(n) => {45 #[allow(clippy::float_cmp)]46 if n.trunc() != n {47 throw!(48 "cannot convert number with fractional part to {}",49 stringify!($ty)50 )51 }52 Ok(n as Self)53 }54 _ => unreachable!(),55 }56 }57 fn into_untyped(value: Self) -> Result<Val> {58 Ok(Val::Num(value as f64))59 }60 }61 )*};62}6364impl_int!(i8 u8 i16 u16 i32 u32);6566macro_rules! impl_bounded_int {67 ($($name:ident = $ty:ty)*) => {$(68 #[derive(Clone, Copy)]69 pub struct $name<const MIN: $ty, const MAX: $ty>($ty);70 impl<const MIN: $ty, const MAX: $ty> $name<MIN, MAX> {71 pub const fn new(value: $ty) -> Option<$name<MIN, MAX>> {72 if value >= MIN && value <= MAX {73 Some(Self(value))74 } else {75 None76 }77 }78 pub const fn value(self) -> $ty {79 self.080 }81 }82 impl<const MIN: $ty, const MAX: $ty> Deref for $name<MIN, MAX> {83 type Target = $ty;84 fn deref(&self) -> &Self::Target {85 &self.086 }87 }8889 impl<const MIN: $ty, const MAX: $ty> Typed for $name<MIN, MAX> {90 const TYPE: &'static ComplexValType =91 &ComplexValType::BoundedNumber(92 Some(MIN as f64),93 Some(MAX as f64),94 );9596 fn from_untyped(value: Val) -> Result<Self> {97 <Self as Typed>::TYPE.check(&value)?;98 match value {99 Val::Num(n) => {100 #[allow(clippy::float_cmp)]101 if n.trunc() != n {102 throw!(103 "cannot convert number with fractional part to {}",104 stringify!($ty)105 )106 }107 Ok(Self(n as $ty))108 }109 _ => unreachable!(),110 }111 }112113 fn into_untyped(value: Self) -> Result<Val> {114 Ok(Val::Num(value.0 as f64))115 }116 }117 )*};118}119120impl_bounded_int!(121 BoundedI8 = i8122 BoundedI16 = i16123 BoundedI32 = i32124 BoundedI64 = i64125 BoundedUsize = usize126);127128impl Typed for f64 {129 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);130131 fn into_untyped(value: Self) -> Result<Val> {132 Ok(Val::Num(value))133 }134135 fn from_untyped(value: Val) -> Result<Self> {136 <Self as Typed>::TYPE.check(&value)?;137 match value {138 Val::Num(n) => Ok(n),139 _ => unreachable!(),140 }141 }142}143144pub struct PositiveF64(pub f64);145impl Typed for PositiveF64 {146 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);147148 fn into_untyped(value: Self) -> Result<Val> {149 Ok(Val::Num(value.0))150 }151152 fn from_untyped(value: Val) -> Result<Self> {153 <Self as Typed>::TYPE.check(&value)?;154 match value {155 Val::Num(n) => Ok(Self(n)),156 _ => unreachable!(),157 }158 }159}160impl Typed for usize {161 const TYPE: &'static ComplexValType =162 &ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));163164 fn into_untyped(value: Self) -> Result<Val> {165 if value > MAX_SAFE_INTEGER as Self {166 throw!("number is too large")167 }168 Ok(Val::Num(value as f64))169 }170171 fn from_untyped(value: Val) -> Result<Self> {172 <Self as Typed>::TYPE.check(&value)?;173 match value {174 Val::Num(n) => {175 #[allow(clippy::float_cmp)]176 if n.trunc() != n {177 throw!("cannot convert number with fractional part to usize")178 }179 Ok(n as Self)180 }181 _ => unreachable!(),182 }183 }184}185186impl Typed for IStr {187 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);188189 fn into_untyped(value: Self) -> Result<Val> {190 Ok(Val::Str(StrValue::Flat(value)))191 }192193 fn from_untyped(value: Val) -> Result<Self> {194 <Self as Typed>::TYPE.check(&value)?;195 match value {196 Val::Str(s) => Ok(s.into_flat()),197 _ => unreachable!(),198 }199 }200}201202impl Typed for String {203 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);204205 fn into_untyped(value: Self) -> Result<Val> {206 Ok(Val::Str(StrValue::Flat(value.into())))207 }208209 fn from_untyped(value: Val) -> Result<Self> {210 <Self as Typed>::TYPE.check(&value)?;211 match value {212 Val::Str(s) => Ok(s.to_string()),213 _ => unreachable!(),214 }215 }216}217218impl Typed for char {219 const TYPE: &'static ComplexValType = &ComplexValType::Char;220221 fn into_untyped(value: Self) -> Result<Val> {222 Ok(Val::Str(StrValue::Flat(value.to_string().into())))223 }224225 fn from_untyped(value: Val) -> Result<Self> {226 <Self as Typed>::TYPE.check(&value)?;227 match value {228 Val::Str(s) => Ok(s.into_flat().chars().next().unwrap()),229 _ => unreachable!(),230 }231 }232}233234impl<T> Typed for Vec<T>235where236 T: Typed,237{238 const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);239240 fn into_untyped(value: Self) -> Result<Val> {241 let mut o = Vec::with_capacity(value.len());242 for i in value {243 o.push(T::into_untyped(i)?);244 }245 Ok(Val::Arr(o.into()))246 }247248 fn from_untyped(value: Val) -> Result<Self> {249 <Self as Typed>::TYPE.check(&value)?;250 match value {251 Val::Arr(a) => {252 let mut o = Self::with_capacity(a.len());253 for i in a.iter() {254 o.push(T::from_untyped(i?)?);255 }256 Ok(o)257 }258 _ => unreachable!(),259 }260 }261}262263/// To be used in Vec<Any>264/// Regular Val can't be used here, because it has wrong `TryFrom::Error` type265#[derive(Clone)]266pub struct Any(pub Val);267268impl Typed for Any {269 const TYPE: &'static ComplexValType = &ComplexValType::Any;270271 fn into_untyped(value: Self) -> Result<Val> {272 Ok(value.0)273 }274275 fn from_untyped(value: Val) -> Result<Self> {276 Ok(Self(value))277 }278}279280/// Specialization, provides faster `TryFrom<VecVal>` for Val281pub struct VecVal(pub Cc<Vec<Val>>);282283impl Typed for VecVal {284 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);285286 fn into_untyped(value: Self) -> Result<Val> {287 Ok(Val::Arr(ArrValue::eager(value.0)))288 }289290 fn from_untyped(value: Val) -> Result<Self> {291 <Self as Typed>::TYPE.check(&value)?;292 match value {293 Val::Arr(a) => Ok(Self(a.evaluatedcc()?)),294 _ => unreachable!(),295 }296 }297}298299/// Specialization300impl Typed for IBytes {301 const TYPE: &'static ComplexValType =302 &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));303304 fn into_untyped(value: Self) -> Result<Val> {305 Ok(Val::Arr(ArrValue::bytes(value)))306 }307308 fn from_untyped(value: Val) -> Result<Self> {309 if let Val::Arr(ArrValue::Bytes(bytes)) = value {310 return Ok(bytes.0);311 }312 <Self as Typed>::TYPE.check(&value)?;313 match value {314 Val::Arr(a) => {315 let mut out = Vec::with_capacity(a.len());316 for e in a.iter() {317 let r = e?;318 out.push(u8::from_untyped(r)?);319 }320 Ok(out.as_slice().into())321 }322 _ => unreachable!(),323 }324 }325}326327pub struct M1;328impl Typed for M1 {329 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));330331 fn into_untyped(_: Self) -> Result<Val> {332 Ok(Val::Num(-1.0))333 }334335 fn from_untyped(value: Val) -> Result<Self> {336 <Self as Typed>::TYPE.check(&value)?;337 Ok(Self)338 }339}340341macro_rules! decl_either {342 ($($name: ident, $($id: ident)*);*) => {$(343 #[derive(Clone)]344 pub enum $name<$($id),*> {345 $($id($id)),*346 }347 impl<$($id),*> Typed for $name<$($id),*>348 where349 $($id: Typed,)*350 {351 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);352353 fn into_untyped(value: Self) -> Result<Val> {354 match value {$(355 $name::$id(v) => $id::into_untyped(v)356 ),*}357 }358359 fn from_untyped(value: Val) -> Result<Self> {360 $(361 if $id::TYPE.check(&value).is_ok() {362 $id::from_untyped(value).map(Self::$id)363 } else364 )* {365 <Self as Typed>::TYPE.check(&value)?;366 unreachable!()367 }368 }369 }370 )*}371}372decl_either!(373 Either1, A;374 Either2, A B;375 Either3, A B C;376 Either4, A B C D;377 Either5, A B C D E;378 Either6, A B C D E F;379 Either7, A B C D E F G380);381#[macro_export]382macro_rules! Either {383 ($a:ty) => {Either1<$a>};384 ($a:ty, $b:ty) => {Either2<$a, $b>};385 ($a:ty, $b:ty, $c:ty) => {Either3<$a, $b, $c>};386 ($a:ty, $b:ty, $c:ty, $d:ty) => {Either4<$a, $b, $c, $d>};387 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {Either5<$a, $b, $c, $d, $e>};388 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {Either6<$a, $b, $c, $d, $e, $f>};389 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {Either7<$a, $b, $c, $d, $e, $f, $g>};390}391pub use Either;392393pub type MyType = Either![u32, f64, String];394395impl Typed for ArrValue {396 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);397398 fn into_untyped(value: Self) -> Result<Val> {399 Ok(Val::Arr(value))400 }401402 fn from_untyped(value: Val) -> Result<Self> {403 <Self as Typed>::TYPE.check(&value)?;404 match value {405 Val::Arr(a) => Ok(a),406 _ => unreachable!(),407 }408 }409}410411impl Typed for FuncVal {412 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);413414 fn into_untyped(value: Self) -> Result<Val> {415 Ok(Val::Func(value))416 }417418 fn from_untyped(value: Val) -> Result<Self> {419 <Self as Typed>::TYPE.check(&value)?;420 match value {421 Val::Func(a) => Ok(a),422 _ => unreachable!(),423 }424 }425}426427impl Typed for Cc<FuncDesc> {428 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);429430 fn into_untyped(value: Self) -> Result<Val> {431 Ok(Val::Func(FuncVal::Normal(value)))432 }433434 fn from_untyped(value: Val) -> Result<Self> {435 <Self as Typed>::TYPE.check(&value)?;436 match value {437 Val::Func(FuncVal::Normal(desc)) => Ok(desc),438 Val::Func(_) => throw!("expected normal function, not builtin"),439 _ => unreachable!(),440 }441 }442}443444impl Typed for ObjValue {445 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);446447 fn into_untyped(value: Self) -> Result<Val> {448 Ok(Val::Obj(value))449 }450451 fn from_untyped(value: Val) -> Result<Self> {452 <Self as Typed>::TYPE.check(&value)?;453 match value {454 Val::Obj(a) => Ok(a),455 _ => unreachable!(),456 }457 }458}459460impl Typed for bool {461 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);462463 fn into_untyped(value: Self) -> Result<Val> {464 Ok(Val::Bool(value))465 }466467 fn from_untyped(value: Val) -> Result<Self> {468 <Self as Typed>::TYPE.check(&value)?;469 match value {470 Val::Bool(a) => Ok(a),471 _ => unreachable!(),472 }473 }474}475impl Typed for IndexableVal {476 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[477 &ComplexValType::Simple(ValType::Arr),478 &ComplexValType::Simple(ValType::Str),479 ]);480481 fn into_untyped(value: Self) -> Result<Val> {482 match value {483 IndexableVal::Str(s) => Ok(Val::Str(StrValue::Flat(s))),484 IndexableVal::Arr(a) => Ok(Val::Arr(a)),485 }486 }487488 fn from_untyped(value: Val) -> Result<Self> {489 <Self as Typed>::TYPE.check(&value)?;490 value.into_indexable()491 }492}493494pub struct Null;495impl Typed for Null {496 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);497498 fn into_untyped(_: Self) -> Result<Val> {499 Ok(Val::Null)500 }501502 fn from_untyped(value: Val) -> Result<Self> {503 <Self as Typed>::TYPE.check(&value)?;504 Ok(Self)505 }506}507508pub struct NativeFn<D: NativeDesc>(D::Value);509impl<D: NativeDesc> Deref for NativeFn<D> {510 type Target = D::Value;511512 fn deref(&self) -> &Self::Target {513 &self.0514 }515}516impl<D: NativeDesc> Typed for NativeFn<D> {517 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);518519 fn into_untyped(_typed: Self) -> Result<Val> {520 throw!("can only convert functions from jsonnet to native")521 }522523 fn from_untyped(untyped: Val) -> Result<Self> {524 Ok(Self(525 untyped526 .as_func()527 .expect("shape is checked")528 .into_native::<D>(),529 ))530 }531}crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -150,7 +150,7 @@
Self::Any => Ok(()),
Self::Simple(t) => t.check(value),
Self::Char => match value {
- Val::Str(s) if s.len() == 1 || s.chars().count() == 1 => Ok(()),
+ Val::Str(s) if s.len() == 1 || s.clone().into_flat().chars().count() == 1 => Ok(()),
v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
},
Self::BoundedNumber(from, to) => {
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,4 +1,9 @@
-use std::{cell::RefCell, fmt::Debug, mem::replace};
+use std::{
+ cell::RefCell,
+ fmt::{self, Debug, Display},
+ mem::replace,
+ rc::Rc,
+};
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
@@ -117,7 +122,7 @@
}
impl<T: Debug + Trace> Debug for Thunk<T> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Lazy")
}
}
@@ -187,6 +192,87 @@
}
}
+#[derive(Debug, Clone, Trace)]
+pub enum StrValue {
+ Flat(IStr),
+ Tree(Rc<(StrValue, StrValue, usize)>),
+}
+impl StrValue {
+ pub fn concat(a: StrValue, b: StrValue) -> Self {
+ if a.is_empty() {
+ b
+ } else if b.is_empty() {
+ a
+ } else {
+ let len = a.len() + b.len();
+ Self::Tree(Rc::new((a, b, len)))
+ }
+ }
+ pub fn into_flat(self) -> IStr {
+ match self {
+ StrValue::Flat(f) => f,
+ StrValue::Tree(_) => {
+ let mut buf = String::new();
+ self.into_flat_buf(&mut buf);
+ buf.into()
+ }
+ }
+ }
+ fn into_flat_buf(&self, out: &mut String) {
+ match self {
+ StrValue::Flat(f) => out.push_str(f),
+ StrValue::Tree(t) => {
+ t.0.into_flat_buf(out);
+ t.1.into_flat_buf(out);
+ }
+ }
+ }
+ pub fn len(&self) -> usize {
+ match self {
+ StrValue::Flat(v) => v.len(),
+ StrValue::Tree(t) => t.2,
+ }
+ }
+ pub fn is_empty(&self) -> bool {
+ match self {
+ Self::Flat(v) => v.is_empty(),
+ _ => false,
+ }
+ }
+}
+impl Display for StrValue {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ StrValue::Flat(v) => write!(f, "{v}"),
+ StrValue::Tree(t) => {
+ write!(f, "{}", t.0)?;
+ write!(f, "{}", t.1)
+ }
+ }
+ }
+}
+impl PartialEq for StrValue {
+ fn eq(&self, other: &Self) -> bool {
+ let a = self.clone().into_flat();
+ let b = other.clone().into_flat();
+ a == b
+ }
+}
+impl Eq for StrValue {}
+impl PartialOrd for StrValue {
+ fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
+ let a = self.clone().into_flat();
+ let b = other.clone().into_flat();
+ Some(a.cmp(&b))
+ }
+}
+impl Ord for StrValue {
+ fn cmp(&self, other: &Self) -> std::cmp::Ordering {
+ self.partial_cmp(other)
+ .expect("partial_cmp always returns Some")
+ }
+}
+
/// Represents any valid Jsonnet value.
#[derive(Debug, Clone, Trace)]
pub enum Val {
@@ -195,7 +281,7 @@
/// Represents a Jsonnet null value.
Null,
/// Represents a Jsonnet string.
- Str(IStr),
+ Str(StrValue),
/// Represents a Jsonnet number.
/// Should be finite, and not NaN
/// This restriction isn't enforced by enum, as enum field can't be marked as private
@@ -208,10 +294,12 @@
Func(FuncVal),
}
+static_assertions::assert_eq_size!(Val, [u8; 24]);
+
impl From<IndexableVal> for Val {
fn from(v: IndexableVal) -> Self {
match v {
- IndexableVal::Str(s) => Self::Str(s),
+ IndexableVal::Str(s) => Self::Str(StrValue::Flat(s)),
IndexableVal::Arr(a) => Self::Arr(a),
}
}
@@ -232,7 +320,7 @@
}
pub fn as_str(&self) -> Option<IStr> {
match self {
- Self::Str(s) => Some(s.clone()),
+ Self::Str(s) => Some(s.clone().into_flat()),
_ => None,
}
}
@@ -295,14 +383,14 @@
Self::Bool(true) => "true".into(),
Self::Bool(false) => "false".into(),
Self::Null => "null".into(),
- Self::Str(s) => s.clone(),
+ Self::Str(s) => s.clone().into_flat(),
_ => self.manifest(ToStringFormat).map(IStr::from)?,
})
}
pub fn into_indexable(self) -> Result<IndexableVal> {
Ok(match self {
- Val::Str(s) => IndexableVal::Str(s),
+ Val::Str(s) => IndexableVal::Str(s.into_flat()),
Val::Arr(arr) => IndexableVal::Arr(arr),
_ => throw!(ValueIsNotIndexable(self.value_type())),
})
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -37,12 +37,13 @@
func: NativeFn<((Either![String, Any],), Any)>,
arr: IndexableVal,
) -> Result<IndexableVal> {
+ use std::fmt::Write;
match arr {
IndexableVal::Str(str) => {
let mut out = String::new();
for c in str.chars() {
match func(Either2::A(c.to_string()))?.0 {
- Val::Str(o) => out.push_str(&o),
+ Val::Str(o) => write!(out, "{o}").unwrap(),
Val::Null => continue,
_ => throw!("in std.join all items should be strings"),
};
@@ -101,6 +102,7 @@
#[builtin]
pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {
+ use std::fmt::Write;
Ok(match sep {
IndexableVal::Arr(joiner_items) => {
let mut out = Vec::new();
@@ -141,7 +143,7 @@
out += &sep;
}
first = false;
- out += &item;
+ write!(out, "{item}").unwrap()
} else if matches!(item, Val::Null) {
continue;
} else {
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -320,15 +320,19 @@
}
#[cfg(feature = "legacy-this-file")]
fn initialize(&self, s: State, source: Source) -> Context {
+ use jrsonnet_evaluator::val::StrValue;
+
let mut builder = ObjValueBuilder::new();
builder.with_super(self.stdlib_obj.clone());
builder
.member("thisFile".into())
.hide()
- .value(Val::Str(match source.source_path().path() {
- Some(p) => self.settings().path_resolver.resolve(p).into(),
- None => source.source_path().to_string().into(),
- }))
+ .value(Val::Str(StrValue::Flat(
+ match source.source_path().path() {
+ Some(p) => self.settings().path_resolver.resolve(p).into(),
+ None => source.source_path().to_string().into(),
+ },
+ )))
.expect("this object builder is empty");
let stdlib_with_this_file = builder.build();
crates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/yaml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -118,6 +118,7 @@
}
Val::Null => buf.push_str("null"),
Val::Str(s) => {
+ let s = s.clone().into_flat();
if s.is_empty() {
buf.push_str("\"\"");
} else if let Some(s) = s.strip_suffix('\n') {
@@ -128,10 +129,10 @@
buf.push_str(&options.padding);
buf.push_str(line);
}
- } else if !options.quote_keys && !yaml_needs_quotes(s) {
- buf.push_str(s);
+ } else if !options.quote_keys && !yaml_needs_quotes(&s) {
+ buf.push_str(&s);
} else {
- escape_string_json_buf(s, buf);
+ escape_string_json_buf(&s, buf);
}
}
Val::Num(n) => write!(buf, "{}", *n).unwrap(),
crates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -1,5 +1,9 @@
use jrsonnet_evaluator::{
- error::Result, function::builtin, typed::VecVal, val::Val, IStr, ObjValue,
+ error::Result,
+ function::builtin,
+ typed::VecVal,
+ val::{StrValue, Val},
+ IStr, ObjValue,
};
use jrsonnet_gcmodule::Cc;
@@ -17,7 +21,10 @@
preserve_order,
);
Ok(VecVal(Cc::new(
- out.into_iter().map(Val::Str).collect::<Vec<_>>(),
+ out.into_iter()
+ .map(StrValue::Flat)
+ .map(Val::Str)
+ .collect::<Vec<_>>(),
)))
}
crates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/operator.rs
+++ b/crates/jrsonnet-stdlib/src/operator.rs
@@ -7,7 +7,7 @@
operator::evaluate_mod_op,
stdlib::std_format,
typed::{Any, Either, Either2},
- val::{equals, primitive_equals},
+ val::{equals, primitive_equals, StrValue},
IStr, Val,
};
@@ -17,7 +17,7 @@
Ok(Any(evaluate_mod_op(
&match a {
A(v) => Val::Num(v),
- B(s) => Val::Str(s),
+ B(s) => Val::Str(StrValue::Flat(s)),
},
&b.0,
)?))
@@ -35,5 +35,5 @@
#[builtin]
pub fn builtin_format(str: IStr, vals: Any) -> Result<String> {
- std_format(str, vals.0)
+ std_format(&str, vals.0)
}
crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -3,7 +3,7 @@
function::builtin,
throw,
typed::{Either2, VecVal, M1},
- val::ArrValue,
+ val::{ArrValue, StrValue},
Either, IStr, Val,
};
use jrsonnet_gcmodule::Cc;
@@ -34,9 +34,12 @@
Ok(VecVal(Cc::new(match maxsplits {
A(n) => str
.splitn(n + 1, &c as &str)
- .map(|s| Val::Str(s.into()))
+ .map(|s| Val::Str(StrValue::Flat(s.into())))
+ .collect(),
+ B(_) => str
+ .split(&c as &str)
+ .map(|s| Val::Str(StrValue::Flat(s.into())))
.collect(),
- B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),
})))
}