difftreelog
style fix clippy warnings
in: master
5 files changed
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- 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)
}
}
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- 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<Val> for Value {
+ type Error = LocError;
+
+ fn try_from(value: Val) -> Result<Self, Self::Error> {
+ <Self as TryFrom<&Val>>::try_from(&value)
+ }
+}
impl TryFrom<&Value> for Val {
type Error = LocError;
@@ -68,3 +75,10 @@
})
}
}
+impl TryFrom<Value> for Val {
+ type Error = LocError;
+
+ fn try_from(value: Value) -> Result<Self, Self::Error> {
+ <Self as TryFrom<&Value>>::try_from(&value)
+ }
+}
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth1use std::convert::{TryFrom, TryInto};23use gcmodule::Cc;4use jrsonnet_interner::IStr;5pub use jrsonnet_macros::Typed;6use jrsonnet_types::{ComplexValType, ValType};78use crate::{9 error::{Error::*, LocError, Result},10 throw,11 typed::CheckType,12 ArrValue, FuncDesc, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,13};1415pub trait TypedObj: Typed {16 fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;17 fn parse(obj: &ObjValue) -> Result<Self>;18 fn into_object(self) -> Result<ObjValue> {19 let mut builder = ObjValueBuilder::new();20 self.serialize(&mut builder)?;21 Ok(builder.build())22 }23}2425pub trait Typed: TryFrom<Val, Error = LocError> + TryInto<Val, Error = LocError> {26 const TYPE: &'static ComplexValType;27}2829macro_rules! impl_int {30 ($($ty:ty)*) => {$(31 impl Typed for $ty {32 const TYPE: &'static ComplexValType =33 &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));34 }35 impl TryFrom<Val> for $ty {36 type Error = LocError;3738 fn try_from(value: Val) -> Result<Self> {39 <Self as Typed>::TYPE.check(&value)?;40 match value {41 Val::Num(n) => {42 if n.trunc() != n {43 throw!(RuntimeError(44 format!(45 "cannot convert number with fractional part to {}",46 stringify!($ty)47 )48 .into()49 ))50 }51 Ok(n as Self)52 }53 _ => unreachable!(),54 }55 }56 }57 impl TryFrom<$ty> for Val {58 type Error = LocError;5960 fn try_from(value: $ty) -> Result<Self> {61 Ok(Self::Num(value as f64))62 }63 }64 )*};65}6667impl_int!(i8 u8 i16 u16 i32 u32);6869impl Typed for f64 {70 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);71}72impl TryFrom<Val> for f64 {73 type Error = LocError;7475 fn try_from(value: Val) -> Result<Self> {76 <Self as Typed>::TYPE.check(&value)?;77 match value {78 Val::Num(n) => Ok(n),79 _ => unreachable!(),80 }81 }82}83impl TryFrom<f64> for Val {84 type Error = LocError;8586 fn try_from(value: f64) -> Result<Self> {87 Ok(Self::Num(value))88 }89}9091pub struct PositiveF64(pub f64);92impl Typed for PositiveF64 {93 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);94}95impl TryFrom<Val> for PositiveF64 {96 type Error = LocError;9798 fn try_from(value: Val) -> Result<Self> {99 <Self as Typed>::TYPE.check(&value)?;100 match value {101 Val::Num(n) => Ok(Self(n)),102 _ => unreachable!(),103 }104 }105}106impl TryFrom<PositiveF64> for Val {107 type Error = LocError;108109 fn try_from(value: PositiveF64) -> Result<Self> {110 Ok(Self::Num(value.0))111 }112}113114impl Typed for usize {115 // It is possible to store 54 bits of precision in f64, but leaving u32::MAX here for compatibility116 const TYPE: &'static ComplexValType =117 &ComplexValType::BoundedNumber(Some(0.0), Some(4294967295.0));118}119impl TryFrom<Val> for usize {120 type Error = LocError;121122 fn try_from(value: Val) -> Result<Self> {123 <Self as Typed>::TYPE.check(&value)?;124 match value {125 Val::Num(n) => {126 if n.trunc() != n {127 throw!(RuntimeError(128 "cannot convert number with fractional part to usize".into()129 ))130 }131 Ok(n as Self)132 }133 _ => unreachable!(),134 }135 }136}137impl TryFrom<usize> for Val {138 type Error = LocError;139140 fn try_from(value: usize) -> Result<Self> {141 if value > u32::MAX as usize {142 throw!(RuntimeError("number is too large".into()))143 }144 Ok(Self::Num(value as f64))145 }146}147148impl Typed for IStr {149 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);150}151impl TryFrom<Val> for IStr {152 type Error = LocError;153154 fn try_from(value: Val) -> Result<Self> {155 <Self as Typed>::TYPE.check(&value)?;156 match value {157 Val::Str(s) => Ok(s),158 _ => unreachable!(),159 }160 }161}162impl TryFrom<IStr> for Val {163 type Error = LocError;164165 fn try_from(value: IStr) -> Result<Self> {166 Ok(Self::Str(value))167 }168}169170impl Typed for String {171 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);172}173impl TryFrom<Val> for String {174 type Error = LocError;175176 fn try_from(value: Val) -> Result<Self> {177 <Self as Typed>::TYPE.check(&value)?;178 match value {179 Val::Str(s) => Ok(s.to_string()),180 _ => unreachable!(),181 }182 }183}184impl TryFrom<String> for Val {185 type Error = LocError;186187 fn try_from(value: String) -> Result<Self> {188 Ok(Self::Str(value.into()))189 }190}191192impl Typed for char {193 const TYPE: &'static ComplexValType = &ComplexValType::Char;194}195impl TryFrom<Val> for char {196 type Error = LocError;197198 fn try_from(value: Val) -> Result<Self> {199 <Self as Typed>::TYPE.check(&value)?;200 match value {201 Val::Str(s) => Ok(s.chars().next().unwrap()),202 _ => unreachable!(),203 }204 }205}206impl TryFrom<char> for Val {207 type Error = LocError;208209 fn try_from(value: char) -> Result<Self> {210 Ok(Self::Str(value.to_string().into()))211 }212}213214impl<T> Typed for Vec<T>215where216 T: Typed,217 T: TryFrom<Val, Error = LocError>,218 T: TryInto<Val, Error = LocError>,219{220 const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);221}222impl<T> TryFrom<Val> for Vec<T>223where224 T: Typed,225 T: TryFrom<Val, Error = LocError>,226 T: TryInto<Val, Error = LocError>,227{228 type Error = LocError;229230 fn try_from(value: Val) -> Result<Self> {231 <Self as Typed>::TYPE.check(&value)?;232 match value {233 Val::Arr(a) => {234 let mut o = Self::with_capacity(a.len());235 for i in a.iter() {236 o.push(T::try_from(i?)?);237 }238 Ok(o)239 }240 _ => unreachable!(),241 }242 }243}244impl<T> TryFrom<Vec<T>> for Val245where246 T: Typed,247 T: TryFrom<Self, Error = LocError>,248 T: TryInto<Self, Error = LocError>,249{250 type Error = LocError;251252 fn try_from(value: Vec<T>) -> Result<Self> {253 let mut o = Vec::with_capacity(value.len());254 for i in value {255 o.push(i.try_into()?);256 }257 Ok(Self::Arr(o.into()))258 }259}260261/// To be used in Vec<Any>262/// Regular Val can't be used here, because it has wrong TryFrom::Error type263#[derive(Clone)]264pub struct Any(pub Val);265266impl Typed for Any {267 const TYPE: &'static ComplexValType = &ComplexValType::Any;268}269impl TryFrom<Val> for Any {270 type Error = LocError;271272 fn try_from(value: Val) -> Result<Self> {273 Ok(Self(value))274 }275}276impl TryFrom<Any> for Val {277 type Error = LocError;278279 fn try_from(value: Any) -> Result<Self> {280 Ok(value.0)281 }282}283284/// Specialization, provides faster TryFrom<VecVal> for Val285pub struct VecVal(pub Vec<Val>);286287impl Typed for VecVal {288 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);289}290impl TryFrom<Val> for VecVal {291 type Error = LocError;292293 fn try_from(value: Val) -> Result<Self> {294 <Self as Typed>::TYPE.check(&value)?;295 match value {296 Val::Arr(a) => Ok(Self(a.evaluated()?.to_vec())),297 _ => unreachable!(),298 }299 }300}301impl TryFrom<VecVal> for Val {302 type Error = LocError;303304 fn try_from(value: VecVal) -> Result<Self> {305 Ok(Self::Arr(value.0.into()))306 }307}308309pub struct M1;310impl Typed for M1 {311 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));312}313impl TryFrom<Val> for M1 {314 type Error = LocError;315316 fn try_from(value: Val) -> Result<Self> {317 <Self as Typed>::TYPE.check(&value)?;318 Ok(Self)319 }320}321impl TryFrom<M1> for Val {322 type Error = LocError;323324 fn try_from(_: M1) -> Result<Self> {325 Ok(Self::Num(-1.0))326 }327}328329macro_rules! decl_either {330 ($($name: ident, $($id: ident)*);*) => {$(331 pub enum $name<$($id),*> {332 $($id($id)),*333 }334 impl<$($id),*> Typed for $name<$($id),*>335 where336 $($id: Typed,)*337 {338 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);339 }340 impl<$($id),*> TryFrom<Val> for $name<$($id),*>341 where342 $($id: Typed,)*343 {344 type Error = LocError;345346 fn try_from(value: Val) -> Result<Self> {347 $(348 if $id::TYPE.check(&value).is_ok() {349 $id::try_from(value).map(Self::$id)350 } else351 )* {352 <Self as Typed>::TYPE.check(&value)?;353 unreachable!()354 }355 }356 }357 impl<$($id),*> TryFrom<$name<$($id),*>> for Val358 where359 $($id: Typed,)*360 {361 type Error = LocError;362 fn try_from(value: $name<$($id),*>) -> Result<Self> {363 match value {$(364 $name::$id(v) => v.try_into()365 ),*}366 }367 }368 )*}369}370decl_either!(371 Either1, A;372 Either2, A B;373 Either3, A B C;374 Either4, A B C D;375 Either5, A B C D E;376 Either6, A B C D E F;377 Either7, A B C D E F G378);379#[macro_export]380macro_rules! Either {381 ($a:ty) => {Either1<$a>};382 ($a:ty, $b:ty) => {Either2<$a, $b>};383 ($a:ty, $b:ty, $c:ty) => {Either3<$a, $b, $c>};384 ($a:ty, $b:ty, $c:ty, $d:ty) => {Either4<$a, $b, $c, $d>};385 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {Either5<$a, $b, $c, $d, $e>};386 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {Either6<$a, $b, $c, $d, $e, $f>};387 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {Either7<$a, $b, $c, $d, $e, $f, $g>};388}389390pub type MyType = Either![u32, f64, String];391392impl Typed for ArrValue {393 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);394}395impl TryFrom<Val> for ArrValue {396 type Error = LocError;397398 fn try_from(value: Val) -> Result<Self> {399 <Self as Typed>::TYPE.check(&value)?;400 match value {401 Val::Arr(a) => Ok(a),402 _ => unreachable!(),403 }404 }405}406impl TryFrom<ArrValue> for Val {407 type Error = LocError;408409 fn try_from(value: ArrValue) -> Result<Self> {410 Ok(Self::Arr(value))411 }412}413414impl Typed for FuncVal {415 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);416}417impl TryFrom<Val> for FuncVal {418 type Error = LocError;419420 fn try_from(value: Val) -> Result<Self> {421 <Self as Typed>::TYPE.check(&value)?;422 match value {423 Val::Func(a) => Ok(a),424 _ => unreachable!(),425 }426 }427}428impl TryFrom<FuncVal> for Val {429 type Error = LocError;430431 fn try_from(value: FuncVal) -> Result<Self> {432 Ok(Self::Func(value))433 }434}435436impl Typed for Cc<FuncDesc> {437 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);438}439impl TryFrom<Val> for Cc<FuncDesc> {440 type Error = LocError;441442 fn try_from(value: Val) -> Result<Self, Self::Error> {443 <Self as Typed>::TYPE.check(&value)?;444 match value {445 Val::Func(FuncVal::Normal(desc)) => Ok(desc.clone()),446 Val::Func(_) => throw!(RuntimeError("expected normal function, not builtin".into())),447 _ => unreachable!(),448 }449 }450}451impl TryFrom<Cc<FuncDesc>> for Val {452 type Error = LocError;453454 fn try_from(value: Cc<FuncDesc>) -> Result<Self, Self::Error> {455 Ok(Self::Func(FuncVal::Normal(value)))456 }457}458459impl Typed for ObjValue {460 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);461}462impl TryFrom<Val> for ObjValue {463 type Error = LocError;464465 fn try_from(value: Val) -> Result<Self> {466 <Self as Typed>::TYPE.check(&value)?;467 match value {468 Val::Obj(a) => Ok(a),469 _ => unreachable!(),470 }471 }472}473impl TryFrom<ObjValue> for Val {474 type Error = LocError;475476 fn try_from(value: ObjValue) -> Result<Self> {477 Ok(Self::Obj(value))478 }479}480481impl Typed for bool {482 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);483}484impl TryFrom<Val> for bool {485 type Error = LocError;486487 fn try_from(value: Val) -> Result<Self> {488 <Self as Typed>::TYPE.check(&value)?;489 match value {490 Val::Bool(a) => Ok(a),491 _ => unreachable!(),492 }493 }494}495impl TryFrom<bool> for Val {496 type Error = LocError;497498 fn try_from(value: bool) -> Result<Self> {499 Ok(Self::Bool(value))500 }501}502503impl Typed for IndexableVal {504 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[505 &ComplexValType::Simple(ValType::Arr),506 &ComplexValType::Simple(ValType::Str),507 ]);508}509impl TryFrom<Val> for IndexableVal {510 type Error = LocError;511512 fn try_from(value: Val) -> Result<Self> {513 <Self as Typed>::TYPE.check(&value)?;514 value.into_indexable()515 }516}517impl TryFrom<IndexableVal> for Val {518 type Error = LocError;519520 fn try_from(value: IndexableVal) -> Result<Self> {521 match value {522 IndexableVal::Str(s) => Ok(Self::Str(s)),523 IndexableVal::Arr(a) => Ok(Self::Arr(a)),524 }525 }526}527528pub struct Null;529impl Typed for Null {530 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);531}532impl TryFrom<Val> for Null {533 type Error = LocError;534535 fn try_from(value: Val) -> Result<Self> {536 <Self as Typed>::TYPE.check(&value)?;537 Ok(Self)538 }539}540impl TryFrom<Null> for Val {541 type Error = LocError;542543 fn try_from(_: Null) -> Result<Self> {544 Ok(Self::Null)545 }546}crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- 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<bool> {
+ pub const fn as_bool(&self) -> Option<bool> {
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<f64> {
+ pub const fn as_num(&self) -> Option<f64> {
match self {
Val::Num(n) => Some(*n),
_ => None,
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- 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<Type>,
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::<Result<Vec<_>>>()?;
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<Self> {
- let attr =
- parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_else(Default::default);
+ let attr = parse_attr::<TypedAttr, _>(&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)?;
}
}
}