--- a/crates/jrsonnet-evaluator/src/function.rs +++ b/crates/jrsonnet-evaluator/src/function.rs @@ -15,12 +15,12 @@ #[derive(Clone, Copy)] pub struct CallLocation<'l>(pub Option<&'l ExprLocation>); impl<'l> CallLocation<'l> { - pub fn new(loc: &'l ExprLocation) -> Self { + pub const fn new(loc: &'l ExprLocation) -> Self { Self(Some(loc)) } } impl CallLocation<'static> { - pub fn native() -> Self { + pub const fn native() -> Self { Self(None) } } --- a/crates/jrsonnet-evaluator/src/integrations/serde.rs +++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs @@ -20,7 +20,7 @@ Val::Arr(a) => { let mut out = Vec::with_capacity(a.len()); for item in a.iter() { - out.push((&item?).try_into()?); + out.push(item?.try_into()?); } Self::Array(out) } @@ -29,8 +29,8 @@ for key in o.fields() { out.insert( (&key as &str).into(), - (&o.get(key)? - .expect("key is present in fields, so value should exist")) + o.get(key)? + .expect("key is present in fields, so value should exist") .try_into()?, ); } @@ -40,6 +40,13 @@ }) } } +impl TryFrom for Value { + type Error = LocError; + + fn try_from(value: Val) -> Result { + >::try_from(&value) + } +} impl TryFrom<&Value> for Val { type Error = LocError; @@ -68,3 +75,10 @@ }) } } +impl TryFrom for Val { + type Error = LocError; + + fn try_from(value: Value) -> Result { + >::try_from(&value) + } +} --- a/crates/jrsonnet-evaluator/src/typed/conversions.rs +++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs @@ -442,7 +442,7 @@ fn try_from(value: Val) -> Result { ::TYPE.check(&value)?; match value { - Val::Func(FuncVal::Normal(desc)) => Ok(desc.clone()), + Val::Func(FuncVal::Normal(desc)) => Ok(desc), Val::Func(_) => throw!(RuntimeError("expected normal function, not builtin".into())), _ => unreachable!(), } --- a/crates/jrsonnet-evaluator/src/val.rs +++ b/crates/jrsonnet-evaluator/src/val.rs @@ -126,15 +126,6 @@ } } -impl PartialEq for FuncVal { - fn eq(&self, other: &Self) -> bool { - match (self, other) { - (Self::Normal(a), Self::Normal(b)) => a == b, - (Self::StaticBuiltin(an), Self::StaticBuiltin(bn)) => std::ptr::eq(*an, *bn), - (..) => false, - } - } -} impl FuncVal { pub fn args_len(&self) -> usize { match self { @@ -353,13 +344,13 @@ } impl Val { - pub fn as_bool(&self) -> Option { + pub const fn as_bool(&self) -> Option { match self { Val::Bool(v) => Some(*v), _ => None, } } - pub fn as_null(&self) -> Option<()> { + pub const fn as_null(&self) -> Option<()> { match self { Val::Null => Some(()), _ => None, @@ -371,7 +362,7 @@ _ => None, } } - pub fn as_num(&self) -> Option { + pub const fn as_num(&self) -> Option { match self { Val::Num(n) => Some(*n), _ => None, --- a/crates/jrsonnet-macros/src/lib.rs +++ b/crates/jrsonnet-macros/src/lib.rs @@ -35,7 +35,7 @@ fn path_is(path: &Path, needed: &str) -> bool { path.leading_colon.is_none() - && path.segments.len() >= 1 + && !path.segments.is_empty() && path.segments.iter().last().unwrap().ident == needed } @@ -119,7 +119,7 @@ enum ArgInfo { Normal { - ty: Type, + ty: Box, is_option: bool, name: String, // ident: Ident, @@ -147,27 +147,27 @@ )) } }; - let ty = &typed.ty as &Type; - if type_is_path(&ty, "CallLocation").is_some() { + let ty = &typed.ty; + if type_is_path(ty, "CallLocation").is_some() { return Ok(Self::Location); - } else if type_is_path(&ty, "Self").is_some() { + } else if type_is_path(ty, "Self").is_some() { return Ok(Self::This); - } else if type_is_path(&ty, "LazyVal").is_some() { + } else if type_is_path(ty, "LazyVal").is_some() { return Ok(Self::Lazy { is_option: false, name: ident.to_string(), }); } - let (is_option, ty) = if let Some(ty) = extract_type_from_option(&ty)? { - if type_is_path(&ty, "LazyVal").is_some() { + let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? { + if type_is_path(ty, "LazyVal").is_some() { return Ok(Self::Lazy { is_option: true, name: ident.to_string(), }); } - (true, ty.clone()) + (true, Box::new(ty.clone())) } else { (false, ty.clone()) }; @@ -210,7 +210,7 @@ .sig .inputs .iter() - .map(|a| ArgInfo::parse(a)) + .map(ArgInfo::parse) .collect::>>()?; let params_desc = args.iter().flat_map(|a| match a { @@ -380,8 +380,7 @@ struct TypedField<'f>(&'f syn::Field, TypedAttr); impl<'f> TypedField<'f> { fn try_new(field: &'f syn::Field) -> Result { - let attr = - parse_attr::(&field.attrs, "typed")?.unwrap_or_else(Default::default); + let attr = parse_attr::(&field.attrs, "typed")?.unwrap_or_default(); if field.ident.is_none() { return Err(Error::new( field.span(), @@ -468,17 +467,15 @@ out.member(#name.into()).value(self.#ident.try_into()?); } } + } else if self.is_option() { + quote! { + if let Some(value) = self.#ident { + value.serialize(out)?; + } + } } else { - if self.is_option() { - quote! { - if let Some(value) = self.#ident { - value.serialize(out)?; - } - } - } else { - quote! { - self.#ident.serialize(out)?; - } + quote! { + self.#ident.serialize(out)?; } } }