difftreelog
refactor split Typed into FromUntyped and IntoUntyped
in: master
20 files changed
bindings/jsonnet/src/native.rsdiffbeforeafterboth--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -6,7 +6,7 @@
use jrsonnet_evaluator::{
error::{Error, ErrorKind},
function::builtin::{NativeCallback, NativeCallbackHandler},
- typed::Typed,
+ typed::FromUntyped as _,
IStr, Val,
};
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -11,10 +11,6 @@
workspace = true
[features]
-default = [
- "exp-regex",
-]
-
experimental = [
"exp-preserve-order",
"exp-destruct",
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -5,11 +5,11 @@
rc::Rc,
};
-use jrsonnet_gcmodule::{cc_dyn, Cc, Trace};
+use jrsonnet_gcmodule::{cc_dyn, Cc};
use jrsonnet_interner::IBytes;
use jrsonnet_parser::{Expr, Spanned};
-use crate::{function::NativeFn, typed::Typed, Context, Result, Thunk, Val};
+use crate::{function::NativeFn, Context, Result, Thunk, Val};
mod spec;
pub use spec::{ArrayLike, *};
@@ -241,4 +241,3 @@
self.0.is_cheap()
}
}
-
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -8,8 +8,11 @@
use super::ArrValue;
use crate::function::NativeFn;
use crate::{
- error::ErrorKind::InfiniteRecursionDetected, evaluate, typed::Typed, val::ThunkValue, Context,
- Error, ObjValue, Result, Thunk, Val,
+ error::ErrorKind::InfiniteRecursionDetected,
+ evaluate,
+ typed::{IntoUntyped, Typed},
+ val::ThunkValue,
+ Context, Error, ObjValue, Result, Thunk, Val,
};
pub trait ArrayLike: Any + Trace + Debug {
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -20,7 +20,7 @@
function::{CallLocation, FuncDesc, FuncVal},
gc::WithCapacityExt as _,
in_frame,
- typed::Typed,
+ typed::{FromUntyped, IntoUntyped as _, Typed},
val::{CachedUnbound, IndexableVal, NumValue, StrValue, Thunk},
with_state, Context, Error, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result,
ResultExt, SupThis, Unbound, Val,
@@ -620,7 +620,7 @@
}
}
Slice(slice) => {
- fn parse_idx<T: Typed>(
+ fn parse_idx<T: Typed + FromUntyped>(
loc: CallLocation<'_>,
ctx: Context,
expr: Option<&Spanned<Expr>>,
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -8,7 +8,7 @@
error::ErrorKind::*,
evaluate,
stdlib::std_format,
- typed::Typed,
+ typed::IntoUntyped as _,
val::{equals, StrValue},
Context, Result, Val,
};
crates/jrsonnet-evaluator/src/function/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/native.rs
+++ b/crates/jrsonnet-evaluator/src/function/native.rs
@@ -3,7 +3,11 @@
use jrsonnet_gcmodule::Trace;
use super::PreparedFuncVal;
-use crate::{bail, function::FuncVal, typed::Typed, CallLocation, Result, Val};
+use crate::{
+ function::FuncVal,
+ typed::{FromUntyped, IntoUntyped, Typed},
+ CallLocation, Result, Val,
+};
use jrsonnet_types::{ComplexValType, ValType};
#[derive(Debug, Trace, Clone)]
@@ -12,8 +16,8 @@
($i:expr; $($gen:ident)*) => {
impl<$($gen,)* O> NativeFn<($($gen,)* O,)>
where
- $($gen: Typed,)*
- O: Typed,
+ $($gen: Typed + IntoUntyped,)*
+ O: Typed + FromUntyped,
{
#[allow(non_snake_case, clippy::too_many_arguments)]
pub fn call(
@@ -22,7 +26,7 @@
) -> Result<O> {
let val = self.0.call(
CallLocation::native(),
- &[$(Typed::into_lazy_untyped($gen),)*],
+ &[$(IntoUntyped::into_lazy_untyped($gen),)*],
&[],
)?;
O::from_untyped(val)
@@ -30,11 +34,9 @@
}
impl<$($gen,)* O> Typed for NativeFn<($($gen,)* O,)> {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
-
- fn into_untyped(_typed: Self) -> Result<Val> {
- bail!("can only convert functions from jsonnet to native")
- }
+ }
+ impl<$($gen,)* O> FromUntyped for NativeFn<($($gen,)* O,)> {
fn from_untyped(untyped: Val) -> Result<Self> {
let func = FuncVal::from_untyped(untyped)?;
Ok(Self(
crates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth1//! faster std.format impl2#![allow(clippy::too_many_arguments)]34use jrsonnet_gcmodule::Trace;5use jrsonnet_interner::IStr;6use jrsonnet_types::ValType;7use thiserror::Error;89use crate::{10 bail,11 error::{format_found, suggest_object_fields, ErrorKind::*},12 typed::Typed,13 Error, ObjValue, Result, Val,14};1516#[derive(Debug, Clone, Error, Trace)]17pub enum FormatError {18 #[error("truncated format code")]19 TruncatedFormatCode,20 #[error("unrecognized conversion type: {0}")]21 UnrecognizedConversionType(char),2223 #[error("not enough values")]24 NotEnoughValues,2526 #[error("cannot use * width with object")]27 CannotUseStarWidthWithObject,28 #[error("mapping keys required")]29 MappingKeysRequired,30 #[error("no such format field: {0}")]31 NoSuchFormatField(IStr),3233 #[error("expected subfield <{0}> to be an object, got {1} instead")]34 SubfieldDidntYieldAnObject(IStr, ValType),35 #[error("subfield not found: <[{full}]{current}>{}", format_found(.found, "subfield"))]36 SubfieldNotFound {37 current: IStr,38 full: IStr,39 found: Box<Vec<IStr>>,40 },41}4243impl From<FormatError> for Error {44 fn from(e: FormatError) -> Self {45 Self::new(Format(e))46 }47}4849use FormatError::*;5051type ParseResult<'t, T> = std::result::Result<(T, &'t str), FormatError>;5253pub fn try_parse_mapping_key(str: &str) -> ParseResult<'_, &str> {54 if str.is_empty() {55 return Err(TruncatedFormatCode);56 }57 let bytes = str.as_bytes();58 if bytes[0] == b'(' {59 let mut i = 1;60 while i < bytes.len() {61 if bytes[i] == b')' {62 return Ok((&str[1..i], &str[i + 1..]));63 }64 i += 1;65 }66 Err(TruncatedFormatCode)67 } else {68 Ok(("", str))69 }70}7172#[cfg(test)]73pub mod tests_key {74 use super::*;7576 #[test]77 fn parse_key() {78 assert_eq!(79 try_parse_mapping_key("(hello ) world").unwrap(),80 ("hello ", " world")81 );82 assert_eq!(try_parse_mapping_key("() world").unwrap(), ("", " world"));83 assert_eq!(try_parse_mapping_key(" world").unwrap(), ("", " world"));84 assert_eq!(85 try_parse_mapping_key(" () world").unwrap(),86 ("", " () world")87 );88 }8990 #[test]91 #[should_panic = "TruncatedFormatCode"]92 fn parse_key_missing_start() {93 try_parse_mapping_key("").unwrap();94 }9596 #[test]97 #[should_panic = "TruncatedFormatCode"]98 fn parse_key_missing_end() {99 try_parse_mapping_key("( ").unwrap();100 }101}102103#[allow(clippy::struct_excessive_bools)]104#[derive(Default, Debug)]105pub struct CFlags {106 pub alt: bool,107 pub zero: bool,108 pub left: bool,109 pub blank: bool,110 pub sign: bool,111}112113pub fn try_parse_cflags(str: &str) -> ParseResult<'_, CFlags> {114 if str.is_empty() {115 return Err(TruncatedFormatCode);116 }117 let bytes = str.as_bytes();118 let mut i = 0;119 let mut out = CFlags::default();120 loop {121 if bytes.len() == i {122 return Err(TruncatedFormatCode);123 }124 match bytes[i] {125 b'#' => out.alt = true,126 b'0' => out.zero = true,127 b'-' => out.left = true,128 b' ' => out.blank = true,129 b'+' => out.sign = true,130 _ => break,131 }132 i += 1;133 }134 Ok((out, &str[i..]))135}136137#[derive(Debug, PartialEq, Eq)]138pub enum Width {139 Star,140 Fixed(u16),141}142pub fn try_parse_field_width(str: &str) -> ParseResult<'_, Width> {143 if str.is_empty() {144 return Err(TruncatedFormatCode);145 }146 let bytes = str.as_bytes();147 if bytes[0] == b'*' {148 return Ok((Width::Star, &str[1..]));149 }150 let mut out: u16 = 0;151 let mut digits = 0;152 while let Some(digit) = (bytes[digits] as char).to_digit(10) {153 out *= 10;154 out += digit as u16;155 digits += 1;156 if digits == bytes.len() {157 return Err(TruncatedFormatCode);158 }159 }160 Ok((Width::Fixed(out), &str[digits..]))161}162163pub fn try_parse_precision(str: &str) -> ParseResult<'_, Option<Width>> {164 if str.is_empty() {165 return Err(TruncatedFormatCode);166 }167 let bytes = str.as_bytes();168 if bytes[0] == b'.' {169 try_parse_field_width(&str[1..]).map(|(r, s)| (Some(r), s))170 } else {171 Ok((None, str))172 }173}174175// Only skips176pub fn try_parse_length_modifier(str: &str) -> ParseResult<'_, ()> {177 if str.is_empty() {178 return Err(TruncatedFormatCode);179 }180 let bytes = str.as_bytes();181 let mut idx = 0;182 while bytes[idx] == b'h' || bytes[idx] == b'l' || bytes[idx] == b'L' {183 idx += 1;184 if bytes.len() == idx {185 return Err(TruncatedFormatCode);186 }187 }188 Ok(((), &str[idx..]))189}190191#[derive(Debug, PartialEq, Eq)]192pub enum ConvTypeV {193 Decimal,194 Octal,195 Hexadecimal,196 Scientific,197 Float,198 Shorter,199 Char,200 String,201 Percent,202}203pub struct ConvType {204 v: ConvTypeV,205 caps: bool,206}207208pub fn parse_conversion_type(str: &str) -> ParseResult<'_, ConvType> {209 if str.is_empty() {210 return Err(TruncatedFormatCode);211 }212213 let code = str.as_bytes()[0];214 let v: (ConvTypeV, bool) = match code {215 b'd' | b'i' | b'u' => (ConvTypeV::Decimal, false),216 b'o' => (ConvTypeV::Octal, false),217 b'x' => (ConvTypeV::Hexadecimal, false),218 b'X' => (ConvTypeV::Hexadecimal, true),219 b'e' => (ConvTypeV::Scientific, false),220 b'E' => (ConvTypeV::Scientific, true),221 b'f' => (ConvTypeV::Float, false),222 b'F' => (ConvTypeV::Float, true),223 b'g' => (ConvTypeV::Shorter, false),224 b'G' => (ConvTypeV::Shorter, true),225 b'c' => (ConvTypeV::Char, false),226 b's' => (ConvTypeV::String, false),227 b'%' => (ConvTypeV::Percent, false),228 c => return Err(UnrecognizedConversionType(c as char)),229 };230231 Ok((ConvType { v: v.0, caps: v.1 }, &str[1..]))232}233234#[derive(Debug)]235pub struct Code<'s> {236 mkey: &'s str,237 cflags: CFlags,238 width: Width,239 precision: Option<Width>,240 convtype: ConvTypeV,241 caps: bool,242}243pub fn parse_code(str: &str) -> ParseResult<'_, Code<'_>> {244 if str.is_empty() {245 return Err(TruncatedFormatCode);246 }247 let (mkey, str) = try_parse_mapping_key(str)?;248 let (cflags, str) = try_parse_cflags(str)?;249 let (width, str) = try_parse_field_width(str)?;250 let (precision, str) = try_parse_precision(str)?;251 let ((), str) = try_parse_length_modifier(str)?;252 let (convtype, str) = parse_conversion_type(str)?;253254 Ok((255 Code {256 mkey,257 cflags,258 width,259 precision,260 convtype: convtype.v,261 caps: convtype.caps,262 },263 str,264 ))265}266267#[derive(Debug)]268pub enum Element<'s> {269 String(&'s str),270 Code(Code<'s>),271}272pub fn parse_codes(mut str: &str) -> Result<Vec<Element<'_>>> {273 let mut bytes = str.as_bytes();274 let mut out = vec![];275 let mut offset = 0;276277 loop {278 while offset != bytes.len() && bytes[offset] != b'%' {279 offset += 1;280 }281 if offset != 0 {282 out.push(Element::String(&str[0..offset]));283 }284 if offset == bytes.len() {285 return Ok(out);286 }287 str = &str[offset + 1..];288 let code;289 (code, str) = parse_code(str)?;290 bytes = str.as_bytes();291 offset = 0;292293 out.push(Element::Code(code));294 }295}296297const NUMBERS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";298299#[inline]300#[allow(clippy::fn_params_excessive_bools)]301pub fn render_integer(302 out: &mut String,303 neg: bool,304 iv: f64,305 padding: u16,306 precision: u16,307 blank: bool,308 sign: bool,309 radix: i64,310 zero_prefix: &str,311 prefix_in_padding: bool,312 caps: bool,313) {314 debug_assert!(iv >= 0.0, "render_integer receives sign using arg");315 let iv = iv.floor() as i64;316 // Digit char indexes in reverse order, i.e317 // for radix = 16 and n = 12f: [15, 2, 1]318 let digits = if iv == 0 {319 vec![0u8]320 } else {321 let mut v = iv.abs();322 let mut nums = Vec::with_capacity(1);323 while v != 0 {324 nums.push((v % radix) as u8);325 v /= radix;326 }327 nums328 };329 #[allow(clippy::bool_to_int_with_if)]330 let zp = padding.saturating_sub(if neg || blank || sign { 1 } else { 0 });331332 let pref_len = zero_prefix.len() as u16;333 let zp2 = zp334 .saturating_sub(if prefix_in_padding { 0 } else { pref_len })335 .max(precision)336 .saturating_sub(if prefix_in_padding { pref_len } else { 0 } + digits.len() as u16);337338 if neg {339 out.push('-');340 } else if sign {341 out.push('+');342 } else if blank {343 out.push(' ');344 }345346 out.reserve(zp2 as usize);347 if iv != 0 {348 out.push_str(zero_prefix);349 }350 for _ in 0..zp2 {351 out.push('0');352 }353354 for digit in digits.into_iter().rev() {355 let ch = NUMBERS[digit as usize] as char;356 out.push(if caps { ch.to_ascii_uppercase() } else { ch });357 }358}359360pub fn render_decimal(361 out: &mut String,362 neg: bool,363 iv: f64,364 padding: u16,365 precision: u16,366 blank: bool,367 sign: bool,368) {369 render_integer(370 out, neg, iv, padding, precision, blank, sign, 10, "", false, false,371 );372}373#[allow(clippy::fn_params_excessive_bools)]374pub fn render_octal(375 out: &mut String,376 neg: bool,377 iv: f64,378 padding: u16,379 precision: u16,380 alt: bool,381 blank: bool,382 sign: bool,383) {384 render_integer(385 out,386 neg,387 iv,388 padding,389 precision,390 blank,391 sign,392 8,393 if alt && iv != 0.0 { "0" } else { "" },394 true,395 false,396 );397}398399#[allow(clippy::fn_params_excessive_bools)]400pub fn render_hexadecimal(401 out: &mut String,402 iv: f64,403 padding: u16,404 precision: u16,405 alt: bool,406 blank: bool,407 sign: bool,408 caps: bool,409) {410 render_integer(411 out,412 iv < 0.0,413 iv.abs(),414 padding,415 precision,416 blank,417 sign,418 16,419 match (alt, caps) {420 (true, true) => "0X",421 (true, false) => "0x",422 (false, _) => "",423 },424 false,425 caps,426 );427}428429#[allow(clippy::fn_params_excessive_bools)]430pub fn render_float(431 out: &mut String,432 n: f64,433 mut padding: u16,434 precision: u16,435 blank: bool,436 sign: bool,437 ensure_pt: bool,438 trailing: bool,439) {440 // Represent the rounded number as an integer * 1/10**prec.441 // Note that it can also be equal to 10**prec and we'll need to carry442 // over to the wholes. We operate on the absolute numbers, so that we443 // don't have trouble with the rounding direction.444 let denominator = 10.0f64.powi(i32::from(precision));445 let numerator = n.abs().mul_add(denominator, 0.5);446 let whole = (numerator / denominator).floor();447 let frac = numerator.floor() % denominator;448449 #[allow(clippy::bool_to_int_with_if)]450 let dot_size = if precision == 0 && !ensure_pt { 0 } else { 1 };451 padding = padding.saturating_sub(dot_size + precision);452 render_decimal(out, n < 0.0, whole, padding, 0, blank, sign);453 if precision == 0 {454 if ensure_pt {455 out.push('.');456 }457 return;458 }459 if trailing || frac > 0.0 {460 out.push('.');461 let mut frac_str = String::new();462 render_decimal(&mut frac_str, false, frac, precision, 0, false, false);463 let mut trim = frac_str.len();464 if !trailing {465 for b in frac_str.as_bytes().iter().rev() {466 if *b == b'0' {467 trim -= 1;468 } else {469 break;470 }471 }472 }473 out.push_str(&frac_str[..trim]);474 } else if ensure_pt {475 out.push('.');476 }477}478479#[allow(clippy::fn_params_excessive_bools)]480pub fn render_float_sci(481 out: &mut String,482 n: f64,483 mut padding: u16,484 precision: u16,485 blank: bool,486 sign: bool,487 ensure_pt: bool,488 trailing: bool,489 caps: bool,490) {491 let exponent = if n == 0.0 {492 0.0493 } else {494 n.abs().log10().floor()495 };496497 let mantissa = if exponent as i16 == -324 {498 n * 10.0 / 10.0_f64.powf(exponent + 1.0)499 } else {500 n / 10.0_f64.powf(exponent)501 };502 let mut exponent_str = String::new();503 render_decimal(504 &mut exponent_str,505 exponent < 0.0,506 exponent.abs(),507 3,508 0,509 false,510 true,511 );512513 // +1 for e514 padding = padding.saturating_sub(exponent_str.len() as u16 + 1);515516 render_float(517 out, mantissa, padding, precision, blank, sign, ensure_pt, trailing,518 );519 out.push(if caps { 'E' } else { 'e' });520 out.push_str(&exponent_str);521}522523#[allow(clippy::too_many_lines)]524pub fn format_code(525 out: &mut String,526 value: &Val,527 code: &Code<'_>,528 width: u16,529 precision: Option<u16>,530) -> Result<()> {531 let clfags = &code.cflags;532 let (fpprec, iprec) = precision.map_or((6, 0), |v| (v, v));533 let padding = if clfags.zero && !clfags.left {534 width535 } else {536 0537 };538539 // TODO: If left padded, can optimize by writing directly to out540 let mut tmp_out = String::new();541542 match code.convtype {543 ConvTypeV::String => tmp_out.push_str(&value.clone().to_string()?),544 ConvTypeV::Decimal => {545 let value = f64::from_untyped(value.clone())?;546 render_decimal(547 &mut tmp_out,548 value <= -1.0,549 value.abs(),550 padding,551 iprec,552 clfags.blank,553 clfags.sign,554 );555 }556 ConvTypeV::Octal => {557 let value = f64::from_untyped(value.clone())?;558 render_octal(559 &mut tmp_out,560 value <= -1.0,561 value.abs(),562 padding,563 iprec,564 clfags.alt,565 clfags.blank,566 clfags.sign,567 );568 }569 ConvTypeV::Hexadecimal => {570 let value = f64::from_untyped(value.clone())?;571 render_hexadecimal(572 &mut tmp_out,573 value,574 padding,575 iprec,576 clfags.alt,577 clfags.blank,578 clfags.sign,579 code.caps,580 );581 }582 ConvTypeV::Scientific => {583 let value = f64::from_untyped(value.clone())?;584 render_float_sci(585 &mut tmp_out,586 value,587 padding,588 fpprec,589 clfags.blank,590 clfags.sign,591 clfags.alt,592 true,593 code.caps,594 );595 }596 ConvTypeV::Float => {597 let value = f64::from_untyped(value.clone())?;598 render_float(599 &mut tmp_out,600 value,601 padding,602 fpprec,603 clfags.blank,604 clfags.sign,605 clfags.alt,606 true,607 );608 }609 ConvTypeV::Shorter => {610 let value = f64::from_untyped(value.clone())?;611 let exponent = if value == 0.0 {612 0.0613 } else {614 value.abs().log10().floor()615 };616 if exponent < -4.0 || exponent >= f64::from(fpprec) {617 render_float_sci(618 &mut tmp_out,619 value,620 padding,621 fpprec - 1,622 clfags.blank,623 clfags.sign,624 clfags.alt,625 clfags.alt,626 code.caps,627 );628 } else {629 let digits_before_pt = 1.max(exponent as u16 + 1);630 render_float(631 &mut tmp_out,632 value,633 padding,634 fpprec - digits_before_pt,635 clfags.blank,636 clfags.sign,637 clfags.alt,638 clfags.alt,639 );640 }641 }642 ConvTypeV::Char => match value.clone() {643 Val::Num(n) => {644 let n = n.get();645 tmp_out.push(646 std::char::from_u32(n as u32)647 .ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,648 );649 }650 Val::Str(s) => {651 let s = s.into_flat();652 if s.chars().count() != 1 {653 bail!("%c expected 1 char string, got {}", s.chars().count());654 }655 tmp_out.push_str(&s);656 }657 _ => {658 bail!(TypeMismatch(659 "%c requires number/string",660 vec![ValType::Num, ValType::Str],661 value.value_type(),662 ));663 }664 },665 ConvTypeV::Percent => tmp_out.push('%'),666 }667668 let padding = width.saturating_sub(tmp_out.len() as u16);669670 if !clfags.left {671 for _ in 0..padding {672 out.push(' ');673 }674 }675 out.push_str(&tmp_out);676 if clfags.left {677 for _ in 0..padding {678 out.push(' ');679 }680 }681682 Ok(())683}684685pub fn format_arr(str: &str, mut values: &[Val]) -> Result<String> {686 let codes = parse_codes(str)?;687 let mut out = String::new();688 let value_count = values.len();689690 for code in codes {691 match code {692 Element::String(s) => {693 out.push_str(s);694 }695 Element::Code(c) => {696 let width = match c.width {697 Width::Star => {698 if values.is_empty() {699 bail!(NotEnoughValues);700 }701 let value = &values[0];702 values = &values[1..];703 u16::from_untyped(value.clone())?704 }705 Width::Fixed(n) => n,706 };707 let precision = match c.precision {708 Some(Width::Star) => {709 if values.is_empty() {710 bail!(NotEnoughValues);711 }712 let value = &values[0];713 values = &values[1..];714 Some(u16::from_untyped(value.clone())?)715 }716 Some(Width::Fixed(n)) => Some(n),717 None => None,718 };719720 // %% should not consume a value721 let value = if c.convtype == ConvTypeV::Percent {722 &Val::Null723 } else {724 if values.is_empty() {725 bail!(NotEnoughValues);726 }727 let value = &values[0];728 values = &values[1..];729 value730 };731732 format_code(&mut out, value, &c, width, precision)?;733 }734 }735 }736737 if !values.is_empty() {738 bail!(739 "too many values to format, expected {value_count}, got {}",740 value_count + values.len()741 )742 }743744 Ok(out)745}746747fn get_dotted_field(obj: ObjValue, field: &str) -> Result<Val> {748 let mut current = Val::Obj(obj);749 let mut name_offset = 0;750 for component in field.split('.') {751 let end_offset = name_offset + component.len();752 current = if let Val::Obj(obj) = current {753 if let Some(value) = obj.get(component.into())? {754 value755 } else {756 let current = &field[name_offset..end_offset];757 let full = &field[..name_offset];758 let found = Box::new(suggest_object_fields(&obj, current.into()));759 bail!(SubfieldNotFound {760 current: current.into(),761 full: full.into(),762 found,763 })764 }765 } else {766 // No underflow may happen, initially we always start with an object767 let subfield = &field[..name_offset - 1];768 bail!(SubfieldDidntYieldAnObject(769 subfield.into(),770 current.value_type()771 ));772 };773 name_offset = end_offset + 1;774 }775 Ok(current)776}777778pub fn format_obj(str: &str, values: &ObjValue) -> Result<String> {779 let codes = parse_codes(str)?;780 let mut out = String::new();781782 for code in codes {783 match code {784 Element::String(s) => {785 out.push_str(s);786 }787 Element::Code(c) => {788 // TODO: Operate on ref789 let f: IStr = c.mkey.into();790 let width = match c.width {791 Width::Star => {792 bail!(CannotUseStarWidthWithObject);793 }794 Width::Fixed(n) => n,795 };796 let precision = match c.precision {797 Some(Width::Star) => {798 bail!(CannotUseStarWidthWithObject);799 }800 Some(Width::Fixed(n)) => Some(n),801 None => None,802 };803804 let value = if c.convtype == ConvTypeV::Percent {805 Val::Null806 } else {807 if f.is_empty() {808 bail!(MappingKeysRequired);809 }810 if let Some(v) = values.get(f.clone())? {811 v812 } else {813 get_dotted_field(values.clone(), &f)?814 }815 };816817 format_code(&mut out, &value, &c, width, precision)?;818 }819 }820 }821822 Ok(out)823}824825#[cfg(test)]826pub mod test_format {827 use super::*;828 use crate::val::NumValue;829830 #[test]831 fn parse() {832 assert_eq!(833 parse_codes(834 "How much error budget is left looking at our %.3f%% availability gurantees?"835 )836 .unwrap()837 .len(),838 4839 );840 }841842 fn num(v: f64) -> Val {843 Val::Num(NumValue::new(v).expect("finite"))844 }845846 #[test]847 fn octals() {848 assert_eq!(format_arr("%#o", &[num(8.0)]).unwrap(), "010");849 assert_eq!(format_arr("%#4o", &[num(8.0)]).unwrap(), " 010");850 assert_eq!(format_arr("%4o", &[num(8.0)]).unwrap(), " 10");851 assert_eq!(format_arr("%04o", &[num(8.0)]).unwrap(), "0010");852 assert_eq!(format_arr("%+4o", &[num(8.0)]).unwrap(), " +10");853 assert_eq!(format_arr("%+04o", &[num(8.0)]).unwrap(), "+010");854 assert_eq!(format_arr("%-4o", &[num(8.0)]).unwrap(), "10 ");855 assert_eq!(format_arr("%+-4o", &[num(8.0)]).unwrap(), "+10 ");856 assert_eq!(format_arr("%+-04o", &[num(8.0)]).unwrap(), "+10 ");857 }858859 #[test]860 fn percent_doesnt_consumes_values() {861 assert_eq!(862 format_arr(863 "How much error budget is left looking at our %.3f%% availability gurantees?",864 &[num(4.0)]865 )866 .unwrap(),867 "How much error budget is left looking at our 4.000% availability gurantees?"868 );869 }870}crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -1,6 +1,6 @@
use std::{collections::BTreeMap, marker::PhantomData, ops::Deref};
-use jrsonnet_gcmodule::{Cc, Trace};
+use jrsonnet_gcmodule::Trace;
use jrsonnet_interner::{IBytes, IStr};
pub use jrsonnet_macros::Typed;
use jrsonnet_types::{ComplexValType, ValType};
@@ -8,17 +8,17 @@
use crate::{
arr::{ArrValue, BytesArray},
bail,
- function::{FuncDesc, FuncVal},
+ function::FuncVal,
typed::CheckType,
val::{IndexableVal, NumValue, StrValue, ThunkMapper},
ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
};
#[derive(Trace)]
-struct FromUntyped<K: Trace>(PhantomData<fn() -> K>);
-impl<K> ThunkMapper<Val> for FromUntyped<K>
+struct ThunkFromUntyped<K: Trace>(PhantomData<fn() -> K>);
+impl<K> ThunkMapper<Val> for ThunkFromUntyped<K>
where
- K: Typed + Trace,
+ K: Typed + FromUntyped + Trace,
{
type Output = K;
@@ -26,12 +26,29 @@
K::from_untyped(from)
}
}
-impl<K: Trace> Default for FromUntyped<K> {
+impl<K: Trace> Default for ThunkFromUntyped<K> {
fn default() -> Self {
Self(PhantomData)
}
}
+#[derive(Trace)]
+struct ThunkIntoUntyped<K: Trace>(PhantomData<fn() -> K>);
+impl<K> ThunkMapper<K> for ThunkIntoUntyped<K>
+where
+ K: Typed + Trace + IntoUntyped,
+{
+ type Output = Val;
+ fn map(self, from: K) -> Result<Self::Output> {
+ K::into_untyped(from)
+ }
+}
+impl<K: Trace> Default for ThunkIntoUntyped<K> {
+ fn default() -> Self {
+ Self(PhantomData)
+ }
+}
+
pub trait TypedObj: Typed {
fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;
fn parse(obj: &ObjValue) -> Result<Self>;
@@ -44,31 +61,41 @@
pub trait Typed: Sized {
const TYPE: &'static ComplexValType;
+}
+pub trait IntoUntyped: Typed {
+ // Whatever caller should use `into_lazy_untyped` instead of `into_untyped`
+ fn provides_lazy() -> bool {
+ false
+ }
fn into_untyped(typed: Self) -> Result<Val>;
fn into_lazy_untyped(typed: Self) -> Thunk<Val> {
Thunk::from(Self::into_untyped(typed))
}
+}
+pub trait IntoUntypedResult: Typed {
+ /// Hack to make builtins be able to return non-result values, and make macros able to convert those values to result
+ /// This method returns identity in impl Typed for Result, and should not be overriden
+ #[doc(hidden)]
+ fn into_untyped_result(typed: Self) -> Result<Val>;
+}
+impl<T> IntoUntypedResult for T
+where
+ T: IntoUntyped,
+{
+ fn into_untyped_result(typed: Self) -> Result<Val> {
+ T::into_untyped(typed)
+ }
+}
+
+pub trait FromUntyped: Typed {
fn from_untyped(untyped: Val) -> Result<Self>;
fn from_lazy_untyped(lazy: Thunk<Val>) -> Result<Self> {
Self::from_untyped(lazy.evaluate()?)
}
- // Whatever caller should use `into_lazy_untyped` instead of `into_untyped`
- fn provides_lazy() -> bool {
- false
- }
-
// Whatever caller should use `from_lazy_untyped` instead of `from_untyped` when possible
fn wants_lazy() -> bool {
false
- }
-
- /// Hack to make builtins be able to return non-result values, and make macros able to convert those values to result
- /// This method returns identity in impl Typed for Result, and should not be overriden
- #[doc(hidden)]
- fn into_result(typed: Self) -> Result<Val> {
- let value = Self::into_untyped(typed)?;
- Ok(value)
}
}
@@ -77,38 +104,30 @@
T: Typed + Trace + Clone,
{
const TYPE: &'static ComplexValType = &ComplexValType::Lazy(T::TYPE);
+}
+impl<T> IntoUntyped for Thunk<T>
+where
+ T: Typed + IntoUntyped + Trace + Clone,
+{
fn into_untyped(typed: Self) -> Result<Val> {
T::into_untyped(typed.evaluate()?)
- }
-
- fn from_untyped(untyped: Val) -> Result<Self> {
- Self::from_lazy_untyped(Thunk::evaluated(untyped))
}
-
fn provides_lazy() -> bool {
true
}
fn into_lazy_untyped(inner: Self) -> Thunk<Val> {
- #[derive(Trace)]
- struct IntoUntyped<K: Trace>(PhantomData<fn() -> K>);
- impl<K> ThunkMapper<K> for IntoUntyped<K>
- where
- K: Typed + Trace,
- {
- type Output = Val;
+ inner.map(<ThunkIntoUntyped<T>>::default())
+ }
+}
- fn map(self, from: K) -> Result<Self::Output> {
- K::into_untyped(from)
- }
- }
- impl<K: Trace> Default for IntoUntyped<K> {
- fn default() -> Self {
- Self(PhantomData)
- }
- }
- inner.map(<IntoUntyped<T>>::default())
+impl<T> FromUntyped for Thunk<T>
+where
+ T: Typed + FromUntyped + Trace + Clone,
+{
+ fn from_untyped(untyped: Val) -> Result<Self> {
+ Self::from_lazy_untyped(Thunk::evaluated(untyped))
}
fn wants_lazy() -> bool {
@@ -116,7 +135,7 @@
}
fn from_lazy_untyped(inner: Thunk<Val>) -> Result<Self> {
- Ok(inner.map(<FromUntyped<T>>::default()))
+ Ok(inner.map(<ThunkFromUntyped<T>>::default()))
}
}
@@ -128,6 +147,8 @@
impl Typed for $ty {
const TYPE: &'static ComplexValType =
&ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));
+ }
+ impl FromUntyped for $ty {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -145,6 +166,8 @@
_ => unreachable!(),
}
}
+ }
+ impl IntoUntyped for $ty {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Num(value.into()))
}
@@ -183,7 +206,9 @@
Some(MIN as f64),
Some(MAX as f64),
);
+ }
+ impl<const MIN: $ty, const MAX: $ty> FromUntyped for $name<MIN, MAX> {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -201,7 +226,9 @@
_ => unreachable!(),
}
}
+ }
+ impl<const MIN: $ty, const MAX: $ty> IntoUntyped for $name<MIN, MAX> {
#[allow(clippy::cast_lossless)]
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::try_num(value.0)?)
@@ -220,11 +247,13 @@
impl Typed for f64 {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);
-
+}
+impl IntoUntyped for f64 {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::try_num(value)?)
}
-
+}
+impl FromUntyped for f64 {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -237,11 +266,13 @@
pub struct PositiveF64(pub f64);
impl Typed for PositiveF64 {
const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);
-
+}
+impl IntoUntyped for PositiveF64 {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::try_num(value.0)?)
}
-
+}
+impl FromUntyped for PositiveF64 {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -253,11 +284,13 @@
impl Typed for usize {
const TYPE: &'static ComplexValType =
&ComplexValType::BoundedNumber(Some(0.0), Some(MAX_SAFE_INTEGER));
-
+}
+impl IntoUntyped for usize {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::try_num(value)?)
}
-
+}
+impl FromUntyped for usize {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -276,11 +309,13 @@
impl Typed for IStr {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
-
+}
+impl IntoUntyped for IStr {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::string(value))
}
-
+}
+impl FromUntyped for IStr {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -292,11 +327,13 @@
impl Typed for String {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
-
+}
+impl IntoUntyped for String {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::string(value))
}
-
+}
+impl FromUntyped for String {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -308,11 +345,13 @@
impl Typed for StrValue {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
-
+}
+impl IntoUntyped for StrValue {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Str(value))
}
-
+}
+impl FromUntyped for StrValue {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -324,11 +363,13 @@
impl Typed for char {
const TYPE: &'static ComplexValType = &ComplexValType::Char;
-
+}
+impl IntoUntyped for char {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::string(value))
}
-
+}
+impl FromUntyped for char {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -338,12 +379,14 @@
}
}
+// TODO: View into vec using ArrayLike?
impl<T> Typed for Vec<T>
where
T: Typed,
{
const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);
-
+}
+impl<T: Typed + IntoUntyped> IntoUntyped for Vec<T> {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Arr(
value
@@ -352,7 +395,8 @@
.collect::<Result<ArrValue>>()?,
))
}
-
+}
+impl<T: Typed + FromUntyped> FromUntyped for Vec<T> {
fn from_untyped(value: Val) -> Result<Self> {
let Val::Arr(a) = value else {
<Self as Typed>::TYPE.check(&value)?;
@@ -369,9 +413,19 @@
}
}
-impl<K: Typed + Ord, V: Typed> Typed for BTreeMap<K, V> {
+// TODO: View into BTreeMap using ObjectCore?
+impl<K, V> Typed for BTreeMap<K, V>
+where
+ K: Typed + Ord,
+ V: Typed,
+{
const TYPE: &'static ComplexValType = &ComplexValType::AttrsOf(V::TYPE);
-
+}
+impl<K, V> IntoUntyped for BTreeMap<K, V>
+where
+ K: Typed + Ord + IntoUntyped,
+ V: Typed + IntoUntyped,
+{
fn into_untyped(typed: Self) -> Result<Val> {
let mut out = ObjValueBuilder::with_capacity(typed.len());
for (k, v) in typed {
@@ -383,7 +437,12 @@
}
Ok(Val::Obj(out.build()))
}
-
+}
+impl<K, V> FromUntyped for BTreeMap<K, V>
+where
+ K: FromUntyped + Ord,
+ V: FromUntyped,
+{
fn from_untyped(value: Val) -> Result<Self> {
Self::TYPE.check(&value)?;
let obj = value.as_obj().expect("typecheck should fail");
@@ -416,32 +475,27 @@
impl Typed for Val {
const TYPE: &'static ComplexValType = &ComplexValType::Any;
-
+}
+impl IntoUntyped for Val {
fn into_untyped(typed: Self) -> Result<Val> {
Ok(typed)
}
+}
+impl FromUntyped for Val {
fn from_untyped(untyped: Val) -> Result<Self> {
Ok(untyped)
}
}
-// Hack
#[doc(hidden)]
impl<T> Typed for Result<T>
where
T: Typed,
{
const TYPE: &'static ComplexValType = &ComplexValType::Any;
-
- fn into_untyped(_typed: Self) -> Result<Val> {
- panic!("do not use this conversion")
- }
-
- fn from_untyped(_untyped: Val) -> Result<Self> {
- panic!("do not use this conversion")
- }
-
- fn into_result(typed: Self) -> Result<Val> {
+}
+impl<T: IntoUntyped> IntoUntypedResult for Result<T> {
+ fn into_untyped_result(typed: Self) -> Result<Val> {
typed.map(T::into_untyped)?
}
}
@@ -450,11 +504,13 @@
impl Typed for IBytes {
const TYPE: &'static ComplexValType =
&ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));
-
+}
+impl IntoUntyped for IBytes {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Arr(ArrValue::bytes(value)))
}
-
+}
+impl FromUntyped for IBytes {
fn from_untyped(value: Val) -> Result<Self> {
let Val::Arr(a) = &value else {
<Self as Typed>::TYPE.check(&value)?;
@@ -477,11 +533,13 @@
pub struct M1;
impl Typed for M1 {
const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));
-
+}
+impl IntoUntyped for M1 {
fn into_untyped(_: Self) -> Result<Val> {
Ok(Val::Num(NumValue::new(-1.0).expect("finite")))
}
-
+}
+impl FromUntyped for M1 {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
Ok(Self)
@@ -499,13 +557,22 @@
$($id: Typed,)*
{
const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);
-
+ }
+ impl<$($id),*> IntoUntyped for $name<$($id),*>
+ where
+ $($id: Typed + IntoUntyped,)*
+ {
fn into_untyped(value: Self) -> Result<Val> {
match value {$(
$name::$id(v) => $id::into_untyped(v)
),*}
}
+ }
+ impl<$($id),*> FromUntyped for $name<$($id),*>
+ where
+ $($id: Typed + FromUntyped,)*
+ {
fn from_untyped(value: Val) -> Result<Self> {
$(
if $id::TYPE.check(&value).is_ok() {
@@ -544,11 +611,13 @@
impl Typed for ArrValue {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);
-
+}
+impl IntoUntyped for ArrValue {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Arr(value))
}
-
+}
+impl FromUntyped for ArrValue {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -560,32 +629,17 @@
impl Typed for FuncVal {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
-
+}
+impl IntoUntyped for FuncVal {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Func(value))
}
-
- fn from_untyped(value: Val) -> Result<Self> {
- <Self as Typed>::TYPE.check(&value)?;
- match value {
- Val::Func(a) => Ok(a),
- _ => unreachable!(),
- }
- }
}
-
-impl Typed for Cc<FuncDesc> {
- const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
-
- fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Func(FuncVal::Normal(value)))
- }
-
+impl FromUntyped for FuncVal {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
- Val::Func(FuncVal::Normal(desc)) => Ok(desc),
- Val::Func(_) => bail!("expected normal function, not builtin"),
+ Val::Func(a) => Ok(a),
_ => unreachable!(),
}
}
@@ -593,11 +647,13 @@
impl Typed for ObjValue {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);
-
+}
+impl IntoUntyped for ObjValue {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Obj(value))
}
-
+}
+impl FromUntyped for ObjValue {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -609,11 +665,13 @@
impl Typed for bool {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);
-
+}
+impl IntoUntyped for bool {
fn into_untyped(value: Self) -> Result<Val> {
Ok(Val::Bool(value))
}
-
+}
+impl FromUntyped for bool {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
@@ -622,19 +680,22 @@
}
}
}
+
impl Typed for IndexableVal {
const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[
&ComplexValType::Simple(ValType::Arr),
&ComplexValType::Simple(ValType::Str),
]);
-
+}
+impl IntoUntyped for IndexableVal {
fn into_untyped(value: Self) -> Result<Val> {
match value {
Self::Str(s) => Ok(Val::string(s)),
Self::Arr(a) => Ok(Val::Arr(a)),
}
}
-
+}
+impl FromUntyped for IndexableVal {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
value.into_indexable()
@@ -644,11 +705,13 @@
pub struct Null;
impl Typed for Null {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);
-
+}
+impl IntoUntyped for Null {
fn into_untyped(_: Self) -> Result<Val> {
Ok(Val::Null)
}
-
+}
+impl FromUntyped for Null {
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
Ok(Self)
@@ -661,11 +724,19 @@
{
const TYPE: &'static ComplexValType =
&ComplexValType::UnionRef(&[&ComplexValType::Simple(ValType::Null), T::TYPE]);
-
+}
+impl<T> IntoUntyped for Option<T>
+where
+ T: Typed + IntoUntyped,
+{
fn into_untyped(typed: Self) -> Result<Val> {
typed.map_or_else(|| Ok(Val::Null), |v| T::into_untyped(v))
}
-
+}
+impl<T> FromUntyped for Option<T>
+where
+ T: Typed + FromUntyped,
+{
fn from_untyped(untyped: Val) -> Result<Self> {
if matches!(untyped, Val::Null) {
Ok(None)
@@ -677,11 +748,13 @@
impl Typed for NumValue {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);
-
+}
+impl IntoUntyped for NumValue {
fn into_untyped(typed: Self) -> Result<Val> {
Ok(Val::Num(typed))
}
-
+}
+impl FromUntyped for NumValue {
fn from_untyped(untyped: Val) -> Result<Self> {
Self::TYPE.check(&untyped)?;
match untyped {
crates/jrsonnet-interner/src/names.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/names.rs
+++ b/crates/jrsonnet-interner/src/names.rs
@@ -0,0 +1 @@
+
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -8,15 +8,15 @@
parse_macro_input,
punctuated::Punctuated,
spanned::Spanned,
- token::{self, Comma},
+ token::Comma,
Attribute, DeriveInput, Error, Expr, ExprClosure, FnArg, GenericArgument, Ident, ItemFn,
LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type,
};
use self::typed::derive_typed_inner;
+mod names;
mod typed;
-mod names;
fn try_parse_attr_noargs<I>(attrs: &[Attribute], ident: I) -> Result<bool>
where
@@ -202,7 +202,7 @@
_ => {}
}
- let (optionality, ty) = if try_parse_attr_noargs(&mut arg.attrs, "default")? {
+ let (optionality, ty) = if try_parse_attr_noargs(&arg.attrs, "default")? {
remove_attr(&mut arg.attrs, "default");
(Optionality::TypeDefault, ty.clone())
} else if let Some(default) = parse_attr::<_, _>(&arg.attrs, "default")? {
@@ -322,7 +322,7 @@
let name = name.as_ref().map_or("<unnamed>", String::as_str);
let eval = quote! {jrsonnet_evaluator::in_description_frame(
|| format!("argument <{}> evaluation", #name),
- || <#ty>::from_untyped(value.evaluate()?),
+ || <#ty as FromUntyped>::from_untyped(value.evaluate()?),
)?};
let value = match optionality {
Optionality::Required => quote! {{
@@ -411,7 +411,7 @@
use ::jrsonnet_evaluator::{
State, Val,
function::{builtin::{Builtin, StaticBuiltin}, FunctionSignature, ParamParse, ParamName, ParamDefault, CallLocation},
- Result, Context, typed::Typed,
+ Result, Context, typed::{Typed, FromUntyped, IntoUntypedResult},
parser::Span, params, Thunk,
};
params!(
@@ -432,7 +432,7 @@
#[allow(unused_variables)]
fn call(&self, location: CallLocation<'_>, parsed: &[Option<Thunk<Val>>]) -> Result<Val> {
let result: #result = #name(#(#pass)*);
- <_ as Typed>::into_result(result)
+ <_ as IntoUntypedResult>::into_untyped_result(result)
}
fn as_any(&self) -> &dyn ::std::any::Any {
self
crates/jrsonnet-macros/src/names.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/names.rs
+++ b/crates/jrsonnet-macros/src/names.rs
@@ -1,6 +1,5 @@
use proc_macro2::TokenStream;
use quote::quote;
-use std::cell::RefCell;
#[derive(Default)]
pub struct Names {
crates/jrsonnet-macros/src/typed.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/typed.rs
+++ b/crates/jrsonnet-macros/src/typed.rs
@@ -178,7 +178,7 @@
None
};
- __value.map(<#ty as Typed>::from_untyped).transpose()?
+ __value.map(<#ty as FromUntyped>::from_untyped).transpose()?
},
}
}
@@ -219,7 +219,7 @@
return Err(ErrorKind::NoSuchField(__names[#error_text].clone(), vec![]).into());
};
- <#ty as Typed>::from_untyped(__value)?
+ <#ty as FromUntyped>::from_untyped(__value)?
},
}
}
@@ -258,14 +258,14 @@
out.field(__names[#name].clone())
#hide
#add
- .try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;
+ .try_thunk(<#ty as IntoUntyped>::into_lazy_untyped(value))?;
}
} else {
quote! {
out.field(__names[#name].clone())
#hide
#add
- .try_value(<#ty as Typed>::into_untyped(value)?)?;
+ .try_value(<#ty as IntoUntyped>::into_untyped(value)?)?;
}
};
if self.is_option {
@@ -313,18 +313,21 @@
const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[
#(#fields,)*
]);
+ }
+ impl #impl_generics FromUntyped for #ident #ty_generics #where_clause {
fn from_untyped(value: Val) -> JrResult<Self> {
let obj = value.as_obj().expect("shape is correct");
Self::parse(&obj)
}
+ }
+ impl #impl_generics IntoUntyped for #ident #ty_generics #where_clause {
fn into_untyped(value: Self) -> JrResult<Val> {
let mut out = ObjValueBuilder::with_capacity(#capacity);
value.serialize(&mut out)?;
Ok(Val::Obj(out.build()))
}
-
}
}
};
@@ -344,7 +347,7 @@
Ok(quote! {
const _: () = {
use ::jrsonnet_evaluator::{
- typed::{ComplexValType, Typed, TypedObj, CheckType},
+ typed::{ComplexValType, Typed, IntoUntyped, FromUntyped, TypedObj, CheckType},
Val, State,
error::{ErrorKind, Result as JrResult},
ObjValueBuilder, ObjValue, IStr,
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -4,7 +4,7 @@
bail,
function::{builtin, FuncVal, NativeFn},
runtime_error,
- typed::{BoundedI32, BoundedUsize, Either2, Typed},
+ typed::{BoundedI32, BoundedUsize, Either2, FromUntyped},
val::{equals, ArrValue, IndexableVal},
Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
};
@@ -24,7 +24,7 @@
}
func.evaluate_trivial().map_or_else(
// TODO: Different mapped array impl avoiding allocating unnecessary vals
- || Ok(ArrValue::range_exclusive(0, *sz).map(Typed::from_untyped(Val::Func(func))?)),
+ || Ok(ArrValue::range_exclusive(0, *sz).map(FromUntyped::from_untyped(Val::Func(func))?)),
|trivial| {
let mut out = Vec::with_capacity(*sz as usize);
for _ in 0..*sz {
crates/jrsonnet-stdlib/src/keyf.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/keyf.rs
+++ b/crates/jrsonnet-stdlib/src/keyf.rs
@@ -1,5 +1,5 @@
use jrsonnet_evaluator::function::{CallLocation, FuncVal, PreparedFuncVal};
-use jrsonnet_evaluator::typed::{ComplexValType, Typed, ValType};
+use jrsonnet_evaluator::typed::{ComplexValType, FromUntyped, Typed, ValType};
use jrsonnet_evaluator::{Error, Result, Thunk, Val};
#[derive(Default, Clone)]
@@ -31,11 +31,9 @@
impl Typed for KeyF {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);
+}
+impl FromUntyped for KeyF {
fn from_untyped(untyped: Val) -> Result<Self> {
FuncVal::from_untyped(untyped).map(Self::new)
- }
-
- fn into_untyped(_typed: Self) -> Result<Val> {
- unreachable!("unused, todo: port split of Typed trait from #193")
}
}
crates/jrsonnet-stdlib/src/manifest/ini.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/ini.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/ini.rs
@@ -2,7 +2,7 @@
use jrsonnet_evaluator::{
manifest::{ManifestFormat, ToStringFormat},
- typed::Typed,
+ typed::{FromUntyped, Typed},
ObjValue, Result, ResultExt, Val,
};
use jrsonnet_parser::IStr;
crates/jrsonnet-stdlib/src/manifest/xml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/xml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/xml.rs
@@ -1,7 +1,7 @@
use jrsonnet_evaluator::{
bail, in_description_frame,
manifest::{ManifestFormat, ToStringFormat},
- typed::{ComplexValType, Either2, Typed, ValType},
+ typed::{ComplexValType, Either2, FromUntyped, Typed, ValType},
val::ArrValue,
Either, ObjValue, Result, ResultExt, Val,
};
@@ -32,11 +32,8 @@
}
impl Typed for JSONMLValue {
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);
-
- fn into_untyped(_typed: Self) -> Result<Val> {
- unreachable!("not used, reserved for parseXML?")
- }
-
+}
+impl FromUntyped for JSONMLValue {
fn from_untyped(untyped: Val) -> Result<Self> {
let val = <Either![ArrValue, String]>::from_untyped(untyped)
.description("parsing JSONML value (an array or string)")?;
@@ -73,7 +70,7 @@
children: in_description_frame(
|| "parsing children".to_owned(),
|| {
- Typed::from_untyped(Val::Arr(arr.slice(
+ FromUntyped::from_untyped(Val::Arr(arr.slice(
Some(if has_attrs { 2 } else { 1 }),
None,
None,
crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -4,7 +4,7 @@
bail,
error::{ErrorKind::*, Result},
function::builtin,
- typed::{Either2, Typed, M1},
+ typed::{Either2, FromUntyped, M1},
val::{ArrValue, IndexableVal},
Either, IStr, Val,
};
tests/tests/builtin.rsdiffbeforeafterboth--- a/tests/tests/builtin.rs
+++ b/tests/tests/builtin.rs
@@ -5,7 +5,7 @@
function::{CallLocation, FuncVal, builtin, builtin::Builtin},
parser::Source,
trace::PathResolver,
- typed::Typed,
+ typed::FromUntyped,
};
use jrsonnet_gcmodule::Trace;
use jrsonnet_stdlib::ContextInitializer as StdContextInitializer;
tests/tests/typed_obj.rsdiffbeforeafterboth--- a/tests/tests/typed_obj.rs
+++ b/tests/tests/typed_obj.rs
@@ -2,7 +2,11 @@
use std::fmt::Debug;
-use jrsonnet_evaluator::{Result, State, trace::PathResolver, typed::Typed};
+use jrsonnet_evaluator::{
+ Result, State,
+ trace::PathResolver,
+ typed::{FromUntyped, IntoUntyped, Typed},
+};
use jrsonnet_stdlib::ContextInitializer;
#[derive(Clone, Typed, PartialEq, Debug)]
@@ -11,7 +15,9 @@
b: u16,
}
-fn test_roundtrip<T: Typed + PartialEq + Debug + Clone>(value: T) -> Result<()> {
+fn test_roundtrip<T: Typed + PartialEq + Debug + Clone + FromUntyped + IntoUntyped>(
+ value: T,
+) -> Result<()> {
let untyped = T::into_untyped(value.clone())?;
let value2 = T::from_untyped(untyped.clone())?;
ensure_eq!(value, value2);