difftreelog
style fix clippy warnings
in: master
17 files changed
crates/jrsonnet-evaluator/src/arr/spec.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/spec.rs
+++ b/crates/jrsonnet-evaluator/src/arr/spec.rs
@@ -372,7 +372,7 @@
pub fn new_inclusive(start: i32, end: i32) -> Self {
Self { start, end }
}
- fn range(&self) -> impl Iterator<Item = i32> + ExactSizeIterator + DoubleEndedIterator {
+ fn range(&self) -> impl ExactSizeIterator<Item = i32> + DoubleEndedIterator {
WithExactSize(
self.start..=self.end,
(self.end as usize)
@@ -461,7 +461,7 @@
ArrayThunk::Waiting(..) => {}
};
- let ArrayThunk::Waiting(_) =
+ let ArrayThunk::Waiting(()) =
replace(&mut self.cached.borrow_mut()[index], ArrayThunk::Pending)
else {
unreachable!()
@@ -508,7 +508,7 @@
match &self.cached.borrow()[index] {
ArrayThunk::Computed(c) => return Some(Thunk::evaluated(c.clone())),
ArrayThunk::Errored(e) => return Some(Thunk::errored(e.clone())),
- ArrayThunk::Waiting(_) | ArrayThunk::Pending => {}
+ ArrayThunk::Waiting(()) | ArrayThunk::Pending => {}
};
Some(Thunk::new(ArrayElement {
@@ -597,9 +597,7 @@
}
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
- let Some(key) = self.keys.get(index) else {
- return None;
- };
+ let key = self.keys.get(index)?;
Some(self.obj.get_lazy_or_bail(key.clone()))
}
@@ -649,9 +647,7 @@
}
fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {
- let Some(key) = self.keys.get(index) else {
- return None;
- };
+ let key = self.keys.get(index)?;
// Nothing can fail in the key part, yet value is still
// lazy-evaluated
Some(Thunk::evaluated(
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -89,7 +89,7 @@
specs: &[CompSpec],
callback: &mut impl FnMut(Context) -> Result<()>,
) -> Result<()> {
- match specs.get(0) {
+ match specs.first() {
None => callback(ctx)?,
Some(CompSpec::IfSpec(IfSpecData(cond))) => {
if bool::from_untyped(evaluate(ctx.clone(), cond)?)? {
crates/jrsonnet-evaluator/src/function/builtin.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/builtin.rs
+++ b/crates/jrsonnet-evaluator/src/function/builtin.rs
@@ -6,8 +6,8 @@
use super::{arglike::ArgsLike, parse::parse_builtin_call, CallLocation};
use crate::{gc::TraceBox, tb, Context, Result, Val};
-/// Can't have str | IStr, because constant BuiltinParam causes
-/// E0492: constant functions cannot refer to interior mutable data
+/// Can't have `str` | `IStr`, because constant `BuiltinParam` causes
+/// `E0492: constant functions cannot refer to interior mutable data`
#[derive(Clone, Trace)]
pub struct ParamName(Option<Cow<'static, str>>);
impl ParamName {
@@ -27,10 +27,9 @@
}
impl PartialEq<IStr> for ParamName {
fn eq(&self, other: &IStr) -> bool {
- match &self.0 {
- Some(s) => s.as_bytes() == other.as_bytes(),
- None => false,
- }
+ self.0
+ .as_ref()
+ .map_or(false, |s| s.as_bytes() == other.as_bytes())
}
}
@@ -87,7 +86,7 @@
params: params
.into_iter()
.map(|n| BuiltinParam {
- name: ParamName::new_dynamic(n.to_string()),
+ name: ParamName::new_dynamic(n),
has_default: false,
})
.collect(),
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -159,11 +159,11 @@
Val::Null => serializer.serialize_none(),
Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()),
Val::Num(n) => {
- if n.fract() != 0.0 {
- serializer.serialize_f64(*n)
- } else {
+ if n.fract() == 0.0 {
let n = *n as i64;
serializer.serialize_i64(n)
+ } else {
+ serializer.serialize_f64(*n)
}
}
#[cfg(feature = "exp-bigint")]
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -41,10 +41,12 @@
clippy::missing_const_for_fn,
// too many false-positives with .expect() calls
clippy::missing_panics_doc,
- // false positive for IStr type. There is an configuration option for
- // such cases, but it doesn't work:
- // https://github.com/rust-lang/rust-clippy/issues/9801
- clippy::mutable_key_type,
+ // false positive for IStr type. There is an configuration option for
+ // such cases, but it doesn't work:
+ // https://github.com/rust-lang/rust-clippy/issues/9801
+ clippy::mutable_key_type,
+ // false positives
+ clippy::redundant_pub_crate,
)]
// For jrsonnet-macros
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -175,6 +175,8 @@
manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;
Ok(out)
}
+
+#[allow(clippy::too_many_lines)]
fn manifest_json_ex_buf(
val: &Val,
buf: &mut String,
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -171,7 +171,7 @@
// .field("assertions_ran", &self.assertions_ran)
.field("this_entries", &self.this_entries)
// .field("value_cache", &self.value_cache)
- .finish()
+ .finish_non_exhaustive()
}
}
@@ -347,7 +347,7 @@
out.with_super(self);
let mut member = out.field(key);
if value.flags.add() {
- member = member.add()
+ member = member.add();
}
if let Some(loc) = value.location {
member = member.with_location(loc);
@@ -395,7 +395,7 @@
pub fn get(&self, key: IStr) -> Result<Option<Val>> {
self.run_assertions()?;
- self.get_for(key, self.0.this().unwrap_or(self.clone()))
+ self.get_for(key, self.0.this().unwrap_or_else(|| self.clone()))
}
pub fn get_for(&self, key: IStr, this: ObjValue) -> Result<Option<Val>> {
@@ -474,7 +474,7 @@
type Output = Val;
fn get(self: Box<Self>) -> Result<Self::Output> {
- Ok(self.obj.get_or_bail(self.key)?)
+ self.obj.get_or_bail(self.key)
}
}
@@ -495,7 +495,7 @@
SuperDepth::default(),
&mut |depth, index, name, visibility| {
let new_sort_key = FieldSortKey::new(depth, index);
- let entry = out.entry(name.clone());
+ let entry = out.entry(name);
let (visible, _) = entry.or_insert((true, new_sort_key));
match visibility {
Visibility::Normal => {}
@@ -634,7 +634,7 @@
SuperDepth::default(),
&mut |depth, index, name, visibility| {
let new_sort_key = FieldSortKey::new(depth, index);
- let entry = out.entry(name.clone());
+ let entry = out.entry(name);
let (visible, _) = entry.or_insert((true, new_sort_key));
match visibility {
Visibility::Normal => {}
crates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/format.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/format.rs
@@ -248,7 +248,7 @@
let (cflags, str) = try_parse_cflags(str)?;
let (width, str) = try_parse_field_width(str)?;
let (precision, str) = try_parse_precision(str)?;
- let (_, str) = try_parse_length_modifier(str)?;
+ let ((), str) = try_parse_length_modifier(str)?;
let (convtype, str) = parse_conversion_type(str)?;
Ok((
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -449,25 +449,21 @@
}
fn from_untyped(value: Val) -> Result<Self> {
- match &value {
- Val::Arr(a) => {
- if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {
- return Ok(bytes.0.as_slice().into());
- };
- <Self as Typed>::TYPE.check(&value)?;
- // Any::downcast_ref::<ByteArray>(&a);
- let mut out = Vec::with_capacity(a.len());
- for e in a.iter() {
- let r = e?;
- out.push(u8::from_untyped(r)?);
- }
- Ok(out.as_slice().into())
- }
- _ => {
- <Self as Typed>::TYPE.check(&value)?;
- unreachable!()
- }
+ let Val::Arr(a) = &value else {
+ <Self as Typed>::TYPE.check(&value)?;
+ unreachable!()
+ };
+ if let Some(bytes) = a.as_any().downcast_ref::<BytesArray>() {
+ return Ok(bytes.0.as_slice().into());
+ };
+ <Self as Typed>::TYPE.check(&value)?;
+ // Any::downcast_ref::<ByteArray>(&a);
+ let mut out = Vec::with_capacity(a.len());
+ for e in a.iter() {
+ let r = e?;
+ out.push(u8::from_untyped(r)?);
}
+ Ok(out.as_slice().into())
}
}
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -90,7 +90,7 @@
item: impl Fn() -> Result<()>,
) -> Result<()> {
State::push_description(error_reason, || match item() {
- Ok(_) => Ok(()),
+ Ok(()) => Ok(()),
Err(mut e) => {
if let ErrorKind::TypeError(e) = &mut e.error_mut() {
(e.1).0.push(path());
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -351,6 +351,8 @@
}
}
impl PartialEq for StrValue {
+ // False positive, into_flat returns not StrValue, but IStr, thus no infinite recursion here.
+ #[allow(clippy::unconditional_recursion)]
fn eq(&self, other: &Self) -> bool {
let a = self.clone().into_flat();
let b = other.clone().into_flat();
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -6,7 +6,7 @@
#![warn(clippy::pedantic, clippy::nursery)]
#![allow(clippy::missing_const_for_fn)]
use std::{
- borrow::{Borrow, Cow},
+ borrow::Cow,
cell::RefCell,
fmt::{self, Display},
hash::{BuildHasherDefault, Hash, Hasher},
@@ -14,7 +14,7 @@
str,
};
-use hashbrown::HashMap;
+use hashbrown::{hash_map::RawEntryMut, HashMap};
use jrsonnet_gcmodule::Trace;
use rustc_hash::FxHasher;
@@ -57,17 +57,6 @@
}
}
-impl Borrow<str> for IStr {
- fn borrow(&self) -> &str {
- self.as_str()
- }
-}
-impl Borrow<[u8]> for IStr {
- fn borrow(&self) -> &[u8] {
- self.as_bytes()
- }
-}
-
impl PartialEq for IStr {
fn eq(&self, other: &Self) -> bool {
// all IStr should be inlined into same pool
@@ -142,12 +131,6 @@
type Target = [u8];
fn deref(&self) -> &Self::Target {
- self.0.as_slice()
- }
-}
-
-impl Borrow<[u8]> for IBytes {
- fn borrow(&self) -> &[u8] {
self.0.as_slice()
}
}
@@ -285,9 +268,9 @@
let mut pool = pool.borrow_mut();
let entry = pool.raw_entry_mut().from_key(bytes);
match entry {
- hashbrown::hash_map::RawEntryMut::Occupied(i) => IBytes(i.get_key_value().0.clone()),
- hashbrown::hash_map::RawEntryMut::Vacant(e) => {
- let (k, _) = e.insert(Inner::new_bytes(bytes), ());
+ RawEntryMut::Occupied(i) => IBytes(i.get_key_value().0.clone()),
+ RawEntryMut::Vacant(e) => {
+ let (k, ()) = e.insert(Inner::new_bytes(bytes), ());
IBytes(k.clone())
}
}
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth1use proc_macro2::TokenStream;2use quote::quote;3use syn::{4 parenthesized,5 parse::{Parse, ParseStream},6 parse_macro_input,7 punctuated::Punctuated,8 spanned::Spanned,9 token::{self, Comma},10 Attribute, DeriveInput, Error, Expr, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,11 PathArguments, Result, ReturnType, Token, Type,12};1314fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>15where16 Ident: PartialEq<I>,17{18 let attrs = attrs19 .iter()20 .filter(|a| a.path().is_ident(&ident))21 .collect::<Vec<_>>();22 if attrs.len() > 1 {23 return Err(Error::new(24 attrs[1].span(),25 "this attribute may be specified only once",26 ));27 } else if attrs.is_empty() {28 return Ok(None);29 }30 let attr = attrs[0];31 let attr = attr.parse_args::<A>()?;3233 Ok(Some(attr))34}3536fn path_is(path: &Path, needed: &str) -> bool {37 path.leading_colon.is_none()38 && !path.segments.is_empty()39 && path.segments.iter().last().unwrap().ident == needed40}4142fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {43 match ty {44 Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {45 let args = &path.path.segments.iter().last().unwrap().arguments;46 Some(args)47 }48 _ => None,49 }50}5152fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {53 let Some(args) = type_is_path(ty, "Option") else {54 return Ok(None);55 };56 // It should have only on angle-bracketed param ("<String>"):57 let PathArguments::AngleBracketed(params) = args else {58 return Err(Error::new(args.span(), "missing option generic"));59 };60 let generic_arg = params.args.iter().next().unwrap();61 // This argument must be a type:62 let GenericArgument::Type(ty) = generic_arg else {63 return Err(Error::new(64 generic_arg.span(),65 "option generic should be a type",66 ));67 };68 Ok(Some(ty))69}7071struct Field {72 attrs: Vec<Attribute>,73 name: Ident,74 _colon: Token![:],75 ty: Type,76}77impl Parse for Field {78 fn parse(input: ParseStream) -> syn::Result<Self> {79 Ok(Self {80 attrs: input.call(Attribute::parse_outer)?,81 name: input.parse()?,82 _colon: input.parse()?,83 ty: input.parse()?,84 })85 }86}8788mod kw {89 syn::custom_keyword!(fields);90 syn::custom_keyword!(rename);91 syn::custom_keyword!(flatten);92 syn::custom_keyword!(add);93 syn::custom_keyword!(hide);94 syn::custom_keyword!(ok);95}9697struct EmptyAttr;98impl Parse for EmptyAttr {99 fn parse(_input: ParseStream) -> Result<Self> {100 Ok(Self)101 }102}103104struct BuiltinAttrs {105 fields: Vec<Field>,106}107impl Parse for BuiltinAttrs {108 fn parse(input: ParseStream) -> syn::Result<Self> {109 if input.is_empty() {110 return Ok(Self { fields: Vec::new() });111 }112 input.parse::<kw::fields>()?;113 let fields;114 parenthesized!(fields in input);115 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;116 Ok(Self {117 fields: p.into_iter().collect(),118 })119 }120}121122enum ArgInfo {123 Normal {124 ty: Box<Type>,125 is_option: bool,126 name: Option<String>,127 cfg_attrs: Vec<Attribute>,128 },129 Lazy {130 is_option: bool,131 name: Option<String>,132 },133 Context,134 Location,135 This,136}137138impl ArgInfo {139 fn parse(name: &str, arg: &FnArg) -> Result<Self> {140 let FnArg::Typed(arg) = arg else {141 unreachable!()142 };143 let ident = match &arg.pat as &Pat {144 Pat::Ident(i) => Some(i.ident.clone()),145 _ => None,146 };147 let ty = &arg.ty;148 if type_is_path(ty, "Context").is_some() {149 return Ok(Self::Context);150 } else if type_is_path(ty, "CallLocation").is_some() {151 return Ok(Self::Location);152 } else if type_is_path(ty, "Thunk").is_some() {153 return Ok(Self::Lazy {154 is_option: false,155 name: ident.map(|v| v.to_string()),156 });157 }158159 match ty as &Type {160 Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),161 _ => {}162 }163164 let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {165 if type_is_path(ty, "Thunk").is_some() {166 return Ok(Self::Lazy {167 is_option: true,168 name: ident.map(|v| v.to_string()),169 });170 }171172 (true, Box::new(ty.clone()))173 } else {174 (false, ty.clone())175 };176177 let cfg_attrs = arg178 .attrs179 .iter()180 .filter(|a| a.path().is_ident("cfg"))181 .cloned()182 .collect();183184 Ok(Self::Normal {185 ty,186 is_option,187 name: ident.map(|v| v.to_string()),188 cfg_attrs,189 })190 }191}192193#[proc_macro_attribute]194pub fn builtin(195 attr: proc_macro::TokenStream,196 item: proc_macro::TokenStream,197) -> proc_macro::TokenStream {198 let attr = parse_macro_input!(attr as BuiltinAttrs);199 let item_fn = item.clone();200 let item_fn: ItemFn = parse_macro_input!(item_fn);201202 match builtin_inner(attr, item_fn, item.into()) {203 Ok(v) => v.into(),204 Err(e) => e.into_compile_error().into(),205 }206}207208fn builtin_inner(209 attr: BuiltinAttrs,210 fun: ItemFn,211 item: proc_macro2::TokenStream,212) -> syn::Result<TokenStream> {213 let ReturnType::Type(_, result) = &fun.sig.output else {214 return Err(Error::new(215 fun.sig.span(),216 "builtin should return something",217 ));218 };219220 let name = fun.sig.ident.to_string();221 let args = fun222 .sig223 .inputs224 .iter()225 .map(|arg| ArgInfo::parse(&name, arg))226 .collect::<Result<Vec<_>>>()?;227228 let params_desc = args.iter().flat_map(|a| match a {229 ArgInfo::Normal {230 is_option,231 name,232 cfg_attrs,233 ..234 } => {235 let name = name236 .as_ref()237 .map(|n| quote! {ParamName::new_static(#n)})238 .unwrap_or_else(|| quote! {None});239 Some(quote! {240 #(#cfg_attrs)*241 BuiltinParam::new(#name, #is_option),242 })243 }244 ArgInfo::Lazy { is_option, name } => {245 let name = name246 .as_ref()247 .map(|n| quote! {ParamName::new_static(#n)})248 .unwrap_or_else(|| quote! {None});249 Some(quote! {250 BuiltinParam::new(#name, #is_option),251 })252 }253 ArgInfo::Context => None,254 ArgInfo::Location => None,255 ArgInfo::This => None,256 });257258 let mut id = 0usize;259 let pass = args260 .iter()261 .map(|a| match a {262 ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {263 let cid = id;264 id += 1;265 (quote! {#cid}, a)266 }267 ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {268 (quote! {compile_error!("should not use id")}, a)269 }270 })271 .map(|(id, a)| match a {272 ArgInfo::Normal {273 ty,274 is_option,275 name,276 cfg_attrs,277 } => {278 let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");279 let eval = quote! {jrsonnet_evaluator::State::push_description(280 || format!("argument <{}> evaluation", #name),281 || <#ty>::from_untyped(value.evaluate()?),282 )?};283 let value = if *is_option {284 quote! {if let Some(value) = &parsed[#id] {285 Some(#eval)286 } else {287 None288 },}289 } else {290 quote! {{291 let value = parsed[#id].as_ref().expect("args shape is checked");292 #eval293 },}294 };295 quote! {296 #(#cfg_attrs)*297 #value298 }299 }300 ArgInfo::Lazy { is_option, .. } => {301 if *is_option {302 quote! {if let Some(value) = &parsed[#id] {303 Some(value.clone())304 } else {305 None306 }}307 } else {308 quote! {309 parsed[#id].as_ref().expect("args shape is correct").clone(),310 }311 }312 }313 ArgInfo::Context => quote! {ctx.clone(),},314 ArgInfo::Location => quote! {location,},315 ArgInfo::This => quote! {self,},316 });317318 let fields = attr.fields.iter().map(|field| {319 let attrs = &field.attrs;320 let name = &field.name;321 let ty = &field.ty;322 quote! {323 #(#attrs)*324 pub #name: #ty,325 }326 });327328 let name = &fun.sig.ident;329 let vis = &fun.vis;330 let static_ext = if attr.fields.is_empty() {331 quote! {332 impl #name {333 pub const INST: &'static dyn StaticBuiltin = &#name {};334 }335 impl StaticBuiltin for #name {}336 }337 } else {338 quote! {}339 };340 let static_derive_copy = if attr.fields.is_empty() {341 quote! {, Copy}342 } else {343 quote! {}344 };345346 Ok(quote! {347 #item348349 #[doc(hidden)]350 #[allow(non_camel_case_types)]351 #[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]352 #vis struct #name {353 #(#fields)*354 }355 const _: () = {356 use ::jrsonnet_evaluator::{357 State, Val,358 function::{builtin::{Builtin, StaticBuiltin, BuiltinParam, ParamName}, CallLocation, ArgsLike, parse::parse_builtin_call},359 Result, Context, typed::Typed,360 parser::ExprLocation,361 };362 const PARAMS: &'static [BuiltinParam] = &[363 #(#params_desc)*364 ];365366 #static_ext367 impl Builtin for #name368 where369 Self: 'static370 {371 fn name(&self) -> &str {372 stringify!(#name)373 }374 fn params(&self) -> &[BuiltinParam] {375 PARAMS376 }377 fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {378 let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;379380 let result: #result = #name(#(#pass)*);381 <_ as Typed>::into_result(result)382 }383 fn as_any(&self) -> &dyn ::std::any::Any {384 self385 }386 }387 };388 })389}390391#[derive(Default)]392struct TypedAttr {393 rename: Option<String>,394 flatten: bool,395 /// flatten(ok) strategy for flattened optionals396 /// field would be None in case of any parsing error (as in serde)397 flatten_ok: bool,398 // Should it be `field+:` instead of `field:`399 add: bool,400 // Should it be `field::` instead of `field:`401 hide: bool,402}403impl Parse for TypedAttr {404 fn parse(input: ParseStream) -> syn::Result<Self> {405 let mut out = Self::default();406 loop {407 let lookahead = input.lookahead1();408 if lookahead.peek(kw::rename) {409 input.parse::<kw::rename>()?;410 input.parse::<Token![=]>()?;411 let name = input.parse::<LitStr>()?;412 if out.rename.is_some() {413 return Err(Error::new(414 name.span(),415 "rename attribute may only be specified once",416 ));417 }418 out.rename = Some(name.value());419 } else if lookahead.peek(kw::flatten) {420 input.parse::<kw::flatten>()?;421 out.flatten = true;422 if input.peek(token::Paren) {423 let content;424 parenthesized!(content in input);425 let lookahead = content.lookahead1();426 if lookahead.peek(kw::ok) {427 content.parse::<kw::ok>()?;428 out.flatten_ok = true;429 } else {430 return Err(lookahead.error());431 }432 }433 } else if lookahead.peek(kw::add) {434 input.parse::<kw::add>()?;435 out.add = true;436 } else if lookahead.peek(kw::hide) {437 input.parse::<kw::hide>()?;438 out.hide = true;439 } else if input.is_empty() {440 break;441 } else {442 return Err(lookahead.error());443 }444 if input.peek(Token![,]) {445 input.parse::<Token![,]>()?;446 } else {447 break;448 }449 }450 Ok(out)451 }452}453454struct TypedField {455 attr: TypedAttr,456 ident: Ident,457 ty: Type,458 is_option: bool,459}460impl TypedField {461 fn parse(field: &syn::Field) -> Result<Self> {462 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();463 let Some(ident) = field.ident.clone() else {464 return Err(Error::new(465 field.span(),466 "this field should appear in output object, but it has no visible name",467 ));468 };469 let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {470 (true, ty.clone())471 } else {472 (false, field.ty.clone())473 };474 if is_option && attr.flatten {475 if !attr.flatten_ok {476 return Err(Error::new(477 field.span(),478 "strategy should be set when flattening Option",479 ));480 }481 } else if attr.flatten_ok {482 return Err(Error::new(483 field.span(),484 "flatten(ok) is only useable on optional fields",485 ));486 }487488 Ok(Self {489 attr,490 ident,491 ty,492 is_option,493 })494 }495 /// None if this field is flattened in jsonnet output496 fn name(&self) -> Option<String> {497 if self.attr.flatten {498 return None;499 }500 Some(501 self.attr502 .rename503 .clone()504 .unwrap_or_else(|| self.ident.to_string()),505 )506 }507508 fn expand_field(&self) -> Option<TokenStream> {509 if self.is_option {510 return None;511 }512 let name = self.name()?;513 let ty = &self.ty;514 Some(quote! {515 (#name, <#ty as Typed>::TYPE)516 })517 }518 fn expand_parse(&self) -> TokenStream {519 let ident = &self.ident;520 let ty = &self.ty;521 if self.attr.flatten {522 // optional flatten is handled in same way as serde523 return if self.is_option {524 quote! {525 #ident: <#ty as TypedObj>::parse(&obj).ok(),526 }527 } else {528 quote! {529 #ident: <#ty as TypedObj>::parse(&obj)?,530 }531 };532 };533534 let name = self.name().unwrap();535 let value = if self.is_option {536 quote! {537 if let Some(value) = obj.get(#name.into())? {538 Some(<#ty as Typed>::from_untyped(value)?)539 } else {540 None541 }542 }543 } else {544 quote! {545 <#ty as Typed>::from_untyped(obj.get(#name.into())?.ok_or_else(|| ErrorKind::NoSuchField(#name.into(), vec![]))?)?546 }547 };548549 quote! {550 #ident: #value,551 }552 }553 fn expand_serialize(&self) -> Result<TokenStream> {554 let ident = &self.ident;555 let ty = &self.ty;556 Ok(if let Some(name) = self.name() {557 let hide = if self.attr.hide {558 quote! {.hide()}559 } else {560 quote! {}561 };562 let add = if self.attr.add {563 quote! {.add()}564 } else {565 quote! {}566 };567 if self.is_option {568 quote! {569 if let Some(value) = self.#ident {570 out.field(#name)571 #hide572 #add573 .try_value(<#ty as Typed>::into_untyped(value)?)?;574 }575 }576 } else {577 quote! {578 out.field(#name)579 #hide580 #add581 .try_value(<#ty as Typed>::into_untyped(self.#ident)?)?;582 }583 }584 } else if self.is_option {585 quote! {586 if let Some(value) = self.#ident {587 <#ty as TypedObj>::serialize(value, out)?;588 }589 }590 } else {591 quote! {592 <#ty as TypedObj>::serialize(self.#ident, out)?;593 }594 })595 }596}597598#[proc_macro_derive(Typed, attributes(typed))]599pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {600 let input = parse_macro_input!(item as DeriveInput);601602 match derive_typed_inner(input) {603 Ok(v) => v.into(),604 Err(e) => e.to_compile_error().into(),605 }606}607608fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {609 let syn::Data::Struct(data) = &input.data else {610 return Err(Error::new(input.span(), "only structs supported"));611 };612613 let ident = &input.ident;614 let fields = data615 .fields616 .iter()617 .map(TypedField::parse)618 .collect::<Result<Vec<_>>>()?;619620 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();621622 let typed = {623 let fields = fields624 .iter()625 .flat_map(TypedField::expand_field)626 .collect::<Vec<_>>();627 quote! {628 impl #impl_generics Typed for #ident #ty_generics #where_clause {629 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[630 #(#fields,)*631 ]);632633 fn from_untyped(value: Val) -> JrResult<Self> {634 let obj = value.as_obj().expect("shape is correct");635 Self::parse(&obj)636 }637638 fn into_untyped(value: Self) -> JrResult<Val> {639 let mut out = ObjValueBuilder::new();640 value.serialize(&mut out)?;641 Ok(Val::Obj(out.build()))642 }643644 }645 }646 };647648 let fields_parse = fields.iter().map(TypedField::expand_parse);649 let fields_serialize = fields650 .iter()651 .map(TypedField::expand_serialize)652 .collect::<Result<Vec<_>>>()?;653654 Ok(quote! {655 const _: () = {656 use ::jrsonnet_evaluator::{657 typed::{ComplexValType, Typed, TypedObj, CheckType},658 Val, State,659 error::{ErrorKind, Result as JrResult},660 ObjValueBuilder, ObjValue,661 };662663 #typed664665 impl #impl_generics TypedObj for #ident #ty_generics #where_clause {666 fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {667 #(#fields_serialize)*668669 Ok(())670 }671 fn parse(obj: &ObjValue) -> JrResult<Self> {672 Ok(Self {673 #(#fields_parse)*674 })675 }676 }677 };678 })679}680681struct FormatInput {682 formatting: LitStr,683 arguments: Vec<Expr>,684}685impl Parse for FormatInput {686 fn parse(input: ParseStream) -> Result<Self> {687 let formatting = input.parse()?;688 let mut arguments = Vec::new();689690 while input.peek(Token![,]) {691 input.parse::<Token![,]>()?;692 if input.is_empty() {693 // Trailing comma694 break;695 }696 let expr = input.parse()?;697 arguments.push(expr);698 }699700 if !input.is_empty() {701 return Err(syn::Error::new(input.span(), "unexpected trailing input"));702 }703704 Ok(Self {705 formatting,706 arguments,707 })708 }709}710fn is_format_str(i: &str) -> bool {711 let mut is_plain = true;712 // -1 = {713 // +1 = }714 let mut is_bracket = 0i8;715 for ele in i.chars() {716 match ele {717 '{' if is_bracket == -1 => {718 is_bracket = 0;719 }720 '}' if is_bracket == -1 => {721 is_plain = false;722 break;723 }724 '}' if is_bracket == 1 => {725 is_bracket = 0;726 }727 '{' if is_bracket == 1 => {728 is_plain = false;729 break;730 }731 '{' => {732 is_bracket = -1;733 }734 '}' => {735 is_bracket = 1;736 }737 _ if is_bracket != 0 => {738 is_plain = false;739 break;740 }741 _ => {}742 }743 }744 !is_plain || is_bracket != 0745}746impl FormatInput {747 fn expand(self) -> TokenStream {748 let format = self.formatting;749 if is_format_str(&format.value()) {750 let args = self.arguments;751 quote! {752 ::jrsonnet_evaluator::IStr::from(format!(#format #(, #args)*))753 }754 } else {755 if let Some(first) = self.arguments.first() {756 return syn::Error::new(757 first.span(),758 "string has no formatting codes, it should not have the arguments",759 )760 .into_compile_error();761 }762 quote! {763 ::jrsonnet_evaluator::IStr::from(#format)764 }765 }766 }767}768769/// IStr formatting helper770///771/// Using `format!("literal with no codes").into()` is slower than just `"literal with no codes".into()`772/// This macro looks for formatting codes in the input string, and uses773/// `format!()` only when necessary774#[proc_macro]775pub fn format_istr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {776 let input = parse_macro_input!(input as FormatInput);777 input.expand().into()778}crates/jrsonnet-stdlib/src/encoding.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/encoding.rs
+++ b/crates/jrsonnet-stdlib/src/encoding.rs
@@ -39,5 +39,5 @@
let bytes = STANDARD
.decode(str.as_bytes())
.map_err(|e| runtime_error!("invalid base64: {e}"))?;
- Ok(String::from_utf8(bytes).map_err(|_| runtime_error!("bad utf8"))?)
+ String::from_utf8(bytes).map_err(|_| runtime_error!("bad utf8"))
}
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -47,7 +47,7 @@
.ext_natives
.get(&x)
.cloned()
- .map_or(Val::Null, |v| Val::Func(v))
+ .map_or(Val::Null, Val::Func)
}
#[builtin(fields(
flake.lockdiffbeforeafterboth--- a/flake.lock
+++ b/flake.lock
@@ -5,11 +5,11 @@
"systems": "systems"
},
"locked": {
- "lastModified": 1694529238,
- "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
+ "lastModified": 1705309234,
+ "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
- "rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
+ "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
@@ -20,11 +20,11 @@
},
"nixpkgs": {
"locked": {
- "lastModified": 1701376520,
- "narHash": "sha256-U3iGiOZqgu7wvVzgfoQzGGFMqNsDj/q/6zPIjCy7ajg=",
+ "lastModified": 1705391267,
+ "narHash": "sha256-gGVm9QudiRtYTX8PN9cTTy7uuJcL4I2lRMoPx496kXk=",
"owner": "nixos",
"repo": "nixpkgs",
- "rev": "c74cc3c3db2ed5e68895953d75c397797d499133",
+ "rev": "41a9a7f170c740acb24f3390323877d11c69d5ee",
"type": "github"
},
"original": {
@@ -50,11 +50,11 @@
]
},
"locked": {
- "lastModified": 1701310566,
- "narHash": "sha256-CL9J3xUR2Ejni4LysrEGX0IdO+Y4BXCiH/By0lmF3eQ=",
+ "lastModified": 1705371439,
+ "narHash": "sha256-P1kulUXpYWkcrjiX3sV4j8ACJZh9XXSaaD+jDLBDLKo=",
"owner": "oxalica",
"repo": "rust-overlay",
- "rev": "6d3c6e185198b8bf7ad639f22404a75aa9a09bff",
+ "rev": "b21f3c0d5bf0f0179f5f0140e8e0cd099618bd04",
"type": "github"
},
"original": {
flake.nixdiffbeforeafterboth--- a/flake.nix
+++ b/flake.nix
@@ -25,14 +25,14 @@
lib = pkgs.lib;
rust =
(pkgs.rustChannelOf {
- date = "2023-10-28";
+ date = "2024-01-10";
channel = "nightly";
})
.default
.override {
extensions = ["rust-src" "miri" "rust-analyzer" "clippy"];
};
- in rec {
+ in {
packages = rec {
go-jsonnet = pkgs.callPackage ./nix/go-jsonnet.nix {};
sjsonnet = pkgs.callPackage ./nix/sjsonnet.nix {};