git.delta.rocks / jrsonnet / refs/commits / 609e5ef85d49

difftreelog

fix fully qualified struct paths in Either!

Yaroslav Bolyukin2023-08-25parent: #6e6d01b.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -511,13 +511,13 @@
 );
 #[macro_export]
 macro_rules! Either {
-	($a:ty) => {Either1<$a>};
-	($a:ty, $b:ty) => {Either2<$a, $b>};
-	($a:ty, $b:ty, $c:ty) => {Either3<$a, $b, $c>};
-	($a:ty, $b:ty, $c:ty, $d:ty) => {Either4<$a, $b, $c, $d>};
-	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {Either5<$a, $b, $c, $d, $e>};
-	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {Either6<$a, $b, $c, $d, $e, $f>};
-	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {Either7<$a, $b, $c, $d, $e, $f, $g>};
+	($a:ty) => {$crate::typed::Either1<$a>};
+	($a:ty, $b:ty) => {$crate::typed::Either2<$a, $b>};
+	($a:ty, $b:ty, $c:ty) => {$crate::typed::Either3<$a, $b, $c>};
+	($a:ty, $b:ty, $c:ty, $d:ty) => {$crate::typed::Either4<$a, $b, $c, $d>};
+	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {$crate::typed::Either5<$a, $b, $c, $d, $e>};
+	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {$crate::typed::Either6<$a, $b, $c, $d, $e, $f>};
+	($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {$crate::typed::Either7<$a, $b, $c, $d, $e, $f, $g>};
 }
 pub use Either;
 
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/misc.rs
1use std::{cell::RefCell, rc::Rc};23use jrsonnet_evaluator::{4	bail,5	error::{ErrorKind::*, Result},6	function::{builtin, ArgLike, CallLocation, FuncVal},7	manifest::JsonFormat,8	typed::{Either2, Either4},9	val::{equals, ArrValue},10	Context, Either, IStr, ObjValue, Thunk, Val,11};1213use crate::{extvar_source, Settings};1415#[builtin]16pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {17	use Either4::*;18	match x {19		A(x) => x.chars().count(),20		B(x) => x.len(),21		C(x) => x.len(),22		D(f) => f.params_len(),23	}24}2526#[builtin(fields(27	settings: Rc<RefCell<Settings>>,28))]29pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {30	let ctx = ctx.state().create_default_context(extvar_source(&x, ""));31	this.settings32		.borrow()33		.ext_vars34		.get(&x)35		.cloned()36		.ok_or_else(|| UndefinedExternalVariable(x))?37		.evaluate_arg(ctx, true)?38		.evaluate()39}4041#[builtin(fields(42	settings: Rc<RefCell<Settings>>,43))]44pub fn builtin_native(this: &builtin_native, x: IStr) -> Val {45	this.settings46		.borrow()47		.ext_natives48		.get(&x)49		.cloned()50		.map_or(Val::Null, |v| Val::Func(v))51}5253#[builtin(fields(54	settings: Rc<RefCell<Settings>>,55))]56pub fn builtin_trace(57	this: &builtin_trace,58	loc: CallLocation,59	str: Val,60	rest: Option<Thunk<Val>>,61) -> Result<Val> {62	this.settings.borrow().trace_printer.print_trace(63		loc,64		match &str {65			Val::Str(s) => s.clone().into_flat(),66			Val::Func(f) => format!("{f:?}").into(),67			v => v68				.manifest(JsonFormat::std_to_json(69					String::from("  "),70					"\n",71					": ",72					#[cfg(feature = "exp-preserve-order")]73					true,74				))?75				.into(),76		},77	);78	if let Some(rest) = rest {79		rest.evaluate()80	} else {81		Ok(str)82	}83}8485#[allow(clippy::comparison_chain)]86#[builtin]87pub fn builtin_starts_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {88	Ok(match (a, b) {89		(Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),90		(Either2::B(a), Either2::B(b)) => {91			if b.len() > a.len() {92				return Ok(false);93			} else if b.len() == a.len() {94				return equals(&Val::Arr(a), &Val::Arr(b));95			} else {96				for (a, b) in a.iter().take(b.len()).zip(b.iter()) {97					let a = a?;98					let b = b?;99					if !equals(&a, &b)? {100						return Ok(false);101					}102				}103				true104			}105		}106		_ => bail!("both arguments should be of the same type"),107	})108}109110#[allow(clippy::comparison_chain)]111#[builtin]112pub fn builtin_ends_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {113	Ok(match (a, b) {114		(Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),115		(Either2::B(a), Either2::B(b)) => {116			if b.len() > a.len() {117				return Ok(false);118			} else if b.len() == a.len() {119				return equals(&Val::Arr(a), &Val::Arr(b));120			} else {121				let a_len = a.len();122				for (a, b) in a.iter().skip(a_len - b.len()).zip(b.iter()) {123					let a = a?;124					let b = b?;125					if !equals(&a, &b)? {126						return Ok(false);127					}128				}129				true130			}131		}132		_ => bail!("both arguments should be of the same type"),133	})134}
after · crates/jrsonnet-stdlib/src/misc.rs
1use std::{cell::RefCell, rc::Rc};23use jrsonnet_evaluator::{4	bail,5	error::{ErrorKind::*, Result},6	function::{builtin, ArgLike, CallLocation, FuncVal},7	manifest::JsonFormat,8	typed::{Either2, Either4},9	val::{equals, ArrValue},10	Context, Either, IStr, ObjValue, Thunk, Val,11};1213use crate::{extvar_source, Settings};1415#[builtin]16pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {17	use Either4::*;18	match x {19		A(x) => x.chars().count(),20		B(x) => x.len(),21		C(x) => x.len(),22		D(f) => f.params_len(),23	}24}2526#[builtin(fields(27	settings: Rc<RefCell<Settings>>,28))]29pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {30	let ctx = ctx.state().create_default_context(extvar_source(&x, ""));31	this.settings32		.borrow()33		.ext_vars34		.get(&x)35		.cloned()36		.ok_or_else(|| UndefinedExternalVariable(x))?37		.evaluate_arg(ctx, true)?38		.evaluate()39}4041#[builtin(fields(42	settings: Rc<RefCell<Settings>>,43))]44pub fn builtin_native(this: &builtin_native, x: IStr) -> Val {45	this.settings46		.borrow()47		.ext_natives48		.get(&x)49		.cloned()50		.map_or(Val::Null, |v| Val::Func(v))51}5253#[builtin(fields(54	settings: Rc<RefCell<Settings>>,55))]56pub fn builtin_trace(57	this: &builtin_trace,58	loc: CallLocation,59	str: Val,60	rest: Option<Thunk<Val>>,61) -> Result<Val> {62	this.settings.borrow().trace_printer.print_trace(63		loc,64		match &str {65			Val::Str(s) => s.clone().into_flat(),66			Val::Func(f) => format!("{f:?}").into(),67			v => v.manifest(JsonFormat::debug())?.into(),68		},69	);70	if let Some(rest) = rest {71		rest.evaluate()72	} else {73		Ok(str)74	}75}7677#[allow(clippy::comparison_chain)]78#[builtin]79pub fn builtin_starts_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {80	Ok(match (a, b) {81		(Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),82		(Either2::B(a), Either2::B(b)) => {83			if b.len() > a.len() {84				return Ok(false);85			} else if b.len() == a.len() {86				return equals(&Val::Arr(a), &Val::Arr(b));87			} else {88				for (a, b) in a.iter().take(b.len()).zip(b.iter()) {89					let a = a?;90					let b = b?;91					if !equals(&a, &b)? {92						return Ok(false);93					}94				}95				true96			}97		}98		_ => bail!("both arguments should be of the same type"),99	})100}101102#[allow(clippy::comparison_chain)]103#[builtin]104pub fn builtin_ends_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {105	Ok(match (a, b) {106		(Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),107		(Either2::B(a), Either2::B(b)) => {108			if b.len() > a.len() {109				return Ok(false);110			} else if b.len() == a.len() {111				return equals(&Val::Arr(a), &Val::Arr(b));112			} else {113				let a_len = a.len();114				for (a, b) in a.iter().skip(a_len - b.len()).zip(b.iter()) {115					let a = a?;116					let b = b?;117					if !equals(&a, &b)? {118						return Ok(false);119					}120				}121				true122			}123		}124		_ => bail!("both arguments should be of the same type"),125	})126}