difftreelog
Merge pull request #146 from CertainLach/fix/tests
in: master
Fix failing CI for tests and lints
22 files changed
cmds/jrsonnet-fmt/src/comments.rsdiffbeforeafterboth--- a/cmds/jrsonnet-fmt/src/comments.rs
+++ b/cmds/jrsonnet-fmt/src/comments.rs
@@ -72,7 +72,7 @@
if matches!(loc, CommentLocation::ItemInline) {
p!(pi: str(" "));
}
- p!(pi: str("/* ") string(lines[0].trim().to_string()) str(" */"))
+ p!(pi: str("/* ") string(lines[0].trim().to_string()) str(" */") nl)
} else if !lines.is_empty() {
fn common_ws_prefix<'a>(a: &'a str, b: &str) -> &'a str {
let offset = a
cmds/jrsonnet-fmt/src/main.rsdiffbeforeafterboth--- a/cmds/jrsonnet-fmt/src/main.rs
+++ b/cmds/jrsonnet-fmt/src/main.rs
@@ -372,8 +372,8 @@
return p!(new: str("{ }"));
}
let mut pi = p!(new: str("{") >i nl);
- for mem in children.into_iter() {
- if mem.should_start_with_newline {
+ for (i, mem) in children.into_iter().enumerate() {
+ if mem.should_start_with_newline && i != 0 {
p!(pi: nl);
}
p!(pi: items(format_comments(&mem.before_trivia, CommentLocation::AboveItem)));
crates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-cli/src/manifest.rs
+++ b/crates/jrsonnet-cli/src/manifest.rs
@@ -6,7 +6,7 @@
};
use jrsonnet_stdlib::{TomlFormat, YamlFormat};
-#[derive(Clone, ValueEnum)]
+#[derive(Clone, Copy, ValueEnum)]
pub enum ManifestFormatName {
/// Expect string as output, and write them directly
String,
@@ -18,9 +18,11 @@
#[derive(Parser)]
#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]
pub struct ManifestOpts {
- /// Output format, wraps resulting value to corresponding std.manifest call.
- #[clap(long, short = 'f', default_value = "json")]
- format: ManifestFormatName,
+ /// Output format, wraps resulting value to corresponding std.manifest call
+ ///
+ /// [default: json, yaml when -y is used]
+ #[clap(long, short = 'f')]
+ format: Option<ManifestFormatName>,
/// Expect plain string as output.
/// Mutually exclusive with `--format`
#[clap(long, short = 'S', conflicts_with = "format")]
@@ -29,7 +31,9 @@
#[clap(long, short = 'y', conflicts_with = "string")]
yaml_stream: bool,
/// Number of spaces to pad output manifest with.
- /// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml/toml]
+ /// `0` for hard tabs, `-1` for single line output
+ ///
+ /// [default: 3 for json, 2 for yaml/toml]
#[clap(long)]
line_padding: Option<usize>,
/// Preserve order in object manifestification
@@ -44,7 +48,12 @@
} else {
#[cfg(feature = "exp-preserve-order")]
let preserve_order = self.preserve_order;
- match self.format {
+ let format = match self.format {
+ Some(v) => v,
+ None if self.yaml_stream => ManifestFormatName::Yaml,
+ None => ManifestFormatName::Json,
+ };
+ match format {
ManifestFormatName::String => Box::new(ToStringFormat),
ManifestFormatName::Json => Box::new(JsonFormat::cli(
self.line_padding.unwrap_or(3),
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 #[allow(unused_variable)]378 fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {379 let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;380381 let result: #result = #name(#(#pass)*);382 <_ as Typed>::into_result(result)383 }384 fn as_any(&self) -> &dyn ::std::any::Any {385 self386 }387 }388 };389 })390}391392#[derive(Default)]393struct TypedAttr {394 rename: Option<String>,395 flatten: bool,396 /// flatten(ok) strategy for flattened optionals397 /// field would be None in case of any parsing error (as in serde)398 flatten_ok: bool,399 // Should it be `field+:` instead of `field:`400 add: bool,401 // Should it be `field::` instead of `field:`402 hide: bool,403}404impl Parse for TypedAttr {405 fn parse(input: ParseStream) -> syn::Result<Self> {406 let mut out = Self::default();407 loop {408 let lookahead = input.lookahead1();409 if lookahead.peek(kw::rename) {410 input.parse::<kw::rename>()?;411 input.parse::<Token![=]>()?;412 let name = input.parse::<LitStr>()?;413 if out.rename.is_some() {414 return Err(Error::new(415 name.span(),416 "rename attribute may only be specified once",417 ));418 }419 out.rename = Some(name.value());420 } else if lookahead.peek(kw::flatten) {421 input.parse::<kw::flatten>()?;422 out.flatten = true;423 if input.peek(token::Paren) {424 let content;425 parenthesized!(content in input);426 let lookahead = content.lookahead1();427 if lookahead.peek(kw::ok) {428 content.parse::<kw::ok>()?;429 out.flatten_ok = true;430 } else {431 return Err(lookahead.error());432 }433 }434 } else if lookahead.peek(kw::add) {435 input.parse::<kw::add>()?;436 out.add = true;437 } else if lookahead.peek(kw::hide) {438 input.parse::<kw::hide>()?;439 out.hide = true;440 } else if input.is_empty() {441 break;442 } else {443 return Err(lookahead.error());444 }445 if input.peek(Token![,]) {446 input.parse::<Token![,]>()?;447 } else {448 break;449 }450 }451 Ok(out)452 }453}454455struct TypedField {456 attr: TypedAttr,457 ident: Ident,458 ty: Type,459 is_option: bool,460}461impl TypedField {462 fn parse(field: &syn::Field) -> Result<Self> {463 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();464 let Some(ident) = field.ident.clone() else {465 return Err(Error::new(466 field.span(),467 "this field should appear in output object, but it has no visible name",468 ));469 };470 let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {471 (true, ty.clone())472 } else {473 (false, field.ty.clone())474 };475 if is_option && attr.flatten {476 if !attr.flatten_ok {477 return Err(Error::new(478 field.span(),479 "strategy should be set when flattening Option",480 ));481 }482 } else if attr.flatten_ok {483 return Err(Error::new(484 field.span(),485 "flatten(ok) is only useable on optional fields",486 ));487 }488489 Ok(Self {490 attr,491 ident,492 ty,493 is_option,494 })495 }496 /// None if this field is flattened in jsonnet output497 fn name(&self) -> Option<String> {498 if self.attr.flatten {499 return None;500 }501 Some(502 self.attr503 .rename504 .clone()505 .unwrap_or_else(|| self.ident.to_string()),506 )507 }508509 fn expand_field(&self) -> Option<TokenStream> {510 if self.is_option {511 return None;512 }513 let name = self.name()?;514 let ty = &self.ty;515 Some(quote! {516 (#name, <#ty as Typed>::TYPE)517 })518 }519 fn expand_parse(&self) -> TokenStream {520 let ident = &self.ident;521 let ty = &self.ty;522 if self.attr.flatten {523 // optional flatten is handled in same way as serde524 return if self.is_option {525 quote! {526 #ident: <#ty as TypedObj>::parse(&obj).ok(),527 }528 } else {529 quote! {530 #ident: <#ty as TypedObj>::parse(&obj)?,531 }532 };533 };534535 let name = self.name().unwrap();536 let value = if self.is_option {537 quote! {538 if let Some(value) = obj.get(#name.into())? {539 Some(<#ty as Typed>::from_untyped(value)?)540 } else {541 None542 }543 }544 } else {545 quote! {546 <#ty as Typed>::from_untyped(obj.get(#name.into())?.ok_or_else(|| ErrorKind::NoSuchField(#name.into(), vec![]))?)?547 }548 };549550 quote! {551 #ident: #value,552 }553 }554 fn expand_serialize(&self) -> Result<TokenStream> {555 let ident = &self.ident;556 let ty = &self.ty;557 Ok(if let Some(name) = self.name() {558 let hide = if self.attr.hide {559 quote! {.hide()}560 } else {561 quote! {}562 };563 let add = if self.attr.add {564 quote! {.add()}565 } else {566 quote! {}567 };568 if self.is_option {569 quote! {570 if let Some(value) = self.#ident {571 out.field(#name)572 #hide573 #add574 .try_value(<#ty as Typed>::into_untyped(value)?)?;575 }576 }577 } else {578 quote! {579 out.field(#name)580 #hide581 #add582 .try_value(<#ty as Typed>::into_untyped(self.#ident)?)?;583 }584 }585 } else if self.is_option {586 quote! {587 if let Some(value) = self.#ident {588 <#ty as TypedObj>::serialize(value, out)?;589 }590 }591 } else {592 quote! {593 <#ty as TypedObj>::serialize(self.#ident, out)?;594 }595 })596 }597}598599#[proc_macro_derive(Typed, attributes(typed))]600pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {601 let input = parse_macro_input!(item as DeriveInput);602603 match derive_typed_inner(input) {604 Ok(v) => v.into(),605 Err(e) => e.to_compile_error().into(),606 }607}608609fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {610 let syn::Data::Struct(data) = &input.data else {611 return Err(Error::new(input.span(), "only structs supported"));612 };613614 let ident = &input.ident;615 let fields = data616 .fields617 .iter()618 .map(TypedField::parse)619 .collect::<Result<Vec<_>>>()?;620621 let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();622623 let typed = {624 let fields = fields625 .iter()626 .flat_map(TypedField::expand_field)627 .collect::<Vec<_>>();628 quote! {629 impl #impl_generics Typed for #ident #ty_generics #where_clause {630 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[631 #(#fields,)*632 ]);633634 fn from_untyped(value: Val) -> JrResult<Self> {635 let obj = value.as_obj().expect("shape is correct");636 Self::parse(&obj)637 }638639 fn into_untyped(value: Self) -> JrResult<Val> {640 let mut out = ObjValueBuilder::new();641 value.serialize(&mut out)?;642 Ok(Val::Obj(out.build()))643 }644645 }646 }647 };648649 let fields_parse = fields.iter().map(TypedField::expand_parse);650 let fields_serialize = fields651 .iter()652 .map(TypedField::expand_serialize)653 .collect::<Result<Vec<_>>>()?;654655 Ok(quote! {656 const _: () = {657 use ::jrsonnet_evaluator::{658 typed::{ComplexValType, Typed, TypedObj, CheckType},659 Val, State,660 error::{ErrorKind, Result as JrResult},661 ObjValueBuilder, ObjValue,662 };663664 #typed665666 impl #impl_generics TypedObj for #ident #ty_generics #where_clause {667 fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {668 #(#fields_serialize)*669670 Ok(())671 }672 fn parse(obj: &ObjValue) -> JrResult<Self> {673 Ok(Self {674 #(#fields_parse)*675 })676 }677 }678 };679 })680}681682struct FormatInput {683 formatting: LitStr,684 arguments: Vec<Expr>,685}686impl Parse for FormatInput {687 fn parse(input: ParseStream) -> Result<Self> {688 let formatting = input.parse()?;689 let mut arguments = Vec::new();690691 while input.peek(Token![,]) {692 input.parse::<Token![,]>()?;693 if input.is_empty() {694 // Trailing comma695 break;696 }697 let expr = input.parse()?;698 arguments.push(expr);699 }700701 if !input.is_empty() {702 return Err(syn::Error::new(input.span(), "unexpected trailing input"));703 }704705 Ok(Self {706 formatting,707 arguments,708 })709 }710}711fn is_format_str(i: &str) -> bool {712 let mut is_plain = true;713 // -1 = {714 // +1 = }715 let mut is_bracket = 0i8;716 for ele in i.chars() {717 match ele {718 '{' if is_bracket == -1 => {719 is_bracket = 0;720 }721 '}' if is_bracket == -1 => {722 is_plain = false;723 break;724 }725 '}' if is_bracket == 1 => {726 is_bracket = 0;727 }728 '{' if is_bracket == 1 => {729 is_plain = false;730 break;731 }732 '{' => {733 is_bracket = -1;734 }735 '}' => {736 is_bracket = 1;737 }738 _ if is_bracket != 0 => {739 is_plain = false;740 break;741 }742 _ => {}743 }744 }745 !is_plain || is_bracket != 0746}747impl FormatInput {748 fn expand(self) -> TokenStream {749 let format = self.formatting;750 if is_format_str(&format.value()) {751 let args = self.arguments;752 quote! {753 ::jrsonnet_evaluator::IStr::from(format!(#format #(, #args)*))754 }755 } else {756 if let Some(first) = self.arguments.first() {757 return syn::Error::new(758 first.span(),759 "string has no formatting codes, it should not have the arguments",760 )761 .into_compile_error();762 }763 quote! {764 ::jrsonnet_evaluator::IStr::from(#format)765 }766 }767 }768}769770/// IStr formatting helper771///772/// Using `format!("literal with no codes").into()` is slower than just `"literal with no codes".into()`773/// This macro looks for formatting codes in the input string, and uses774/// `format!()` only when necessary775#[proc_macro]776pub fn format_istr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {777 let input = parse_macro_input!(input as FormatInput);778 input.expand().into()779}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/manifest/yaml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/yaml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -134,6 +134,14 @@
buf.push_str(&options.padding);
buf.push_str(line);
}
+ } else if s.contains('\n') {
+ buf.push_str("|-");
+ for line in s.split('\n') {
+ buf.push('\n');
+ buf.push_str(cur_padding);
+ buf.push_str(&options.padding);
+ buf.push_str(line);
+ }
} else if !options.quote_keys && !yaml_needs_quotes(&s) {
buf.push_str(&s);
} else {
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 {};
tests/suite/std_param_names.jsonnetdiffbeforeafterboth--- a/tests/suite/std_param_names.jsonnet
+++ b/tests/suite/std_param_names.jsonnet
@@ -103,6 +103,7 @@
asin: ['x'],
acos: ['x'],
atan: ['x'],
+ atan2: ['y', 'x'],
type: ['x'],
filter: ['func', 'arr'],
objectHasEx: ['obj', 'fname', 'hidden'],