difftreelog
feat macro name interning
in: master
9 files changed
cmds/jrsonnet/Cargo.tomldiffbeforeafterboth--- a/cmds/jrsonnet/Cargo.toml
+++ b/cmds/jrsonnet/Cargo.toml
@@ -11,6 +11,10 @@
workspace = true
[features]
+default = [
+ "exp-regex",
+]
+
experimental = [
"exp-preserve-order",
"exp-destruct",
@@ -18,7 +22,6 @@
"exp-object-iteration",
"exp-bigint",
"exp-apply",
- "exp-regex",
]
# Use mimalloc as allocator
mimalloc = ["mimallocator"]
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/arr/mod.rs
+++ b/crates/jrsonnet-evaluator/src/arr/mod.rs
@@ -5,11 +5,11 @@
rc::Rc,
};
-use jrsonnet_gcmodule::{cc_dyn, Cc};
+use jrsonnet_gcmodule::{cc_dyn, Cc, Trace};
use jrsonnet_interner::IBytes;
use jrsonnet_parser::{Expr, Spanned};
-use crate::{function::NativeFn, Context, Result, Thunk, Val};
+use crate::{function::NativeFn, typed::Typed, Context, Result, Thunk, Val};
mod spec;
pub use spec::{ArrayLike, *};
@@ -241,3 +241,4 @@
self.0.is_cheap()
}
}
+
crates/jrsonnet-evaluator/src/obj/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj/mod.rs
+++ b/crates/jrsonnet-evaluator/src/obj/mod.rs
@@ -12,11 +12,12 @@
use educe::Educe;
use jrsonnet_gcmodule::{cc_dyn, Acyclic, Cc, Trace, Weak};
use jrsonnet_interner::IStr;
-use jrsonnet_parser::{Span, Visibility};
+use jrsonnet_parser::Span;
use rustc_hash::{FxHashMap, FxHashSet};
mod oop;
+pub use jrsonnet_parser::Visibility;
pub use oop::ObjValueBuilder;
use crate::{
@@ -30,7 +31,7 @@
};
#[cfg(not(feature = "exp-preserve-order"))]
-mod ordering {
+pub mod ordering {
#![allow(
// This module works as stub for preserve-order feature
clippy::unused_self,
@@ -41,6 +42,9 @@
#[derive(Clone, Copy, Default, Debug, Trace)]
pub struct FieldIndex(());
impl FieldIndex {
+ pub fn absolute(_v: u32) -> Self {
+ Self(())
+ }
pub const fn next(self) -> Self {
Self(())
}
@@ -54,7 +58,7 @@
}
#[cfg(feature = "exp-preserve-order")]
-mod ordering {
+pub mod ordering {
use std::cmp::Reverse;
use jrsonnet_gcmodule::Trace;
@@ -62,6 +66,9 @@
#[derive(Clone, Copy, Default, Debug, Trace, PartialEq, Eq, PartialOrd, Ord)]
pub struct FieldIndex(u32);
impl FieldIndex {
+ pub fn absolute(v: u32) -> Self {
+ Self(v)
+ }
pub fn next(self) -> Self {
Self(self.0 + 1)
}
@@ -149,7 +156,7 @@
Pending,
}
-type EnumFieldsHandler<'a> =
+pub type EnumFieldsHandler<'a> =
dyn FnMut(SuperDepth, FieldIndex, IStr, EnumFields) -> ControlFlow<()> + 'a;
pub enum EnumFields {
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth1#![deny(2 unsafe_op_in_unsafe_fn,3 clippy::missing_safety_doc,4 clippy::undocumented_unsafe_blocks5)]6#![warn(clippy::pedantic, clippy::nursery)]7#![allow(clippy::missing_const_for_fn)]8use std::{9 borrow::Cow,10 cell::RefCell,11 fmt::{self, Display},12 hash::{Hash, Hasher},13 ops::Deref,14 str,15};1617use hashbrown::{hash_map::RawEntryMut, HashMap};18use jrsonnet_gcmodule::{Acyclic, Trace};19use rustc_hash::FxBuildHasher;2021mod inner;22use inner::Inner;2324/// Interned string25///26/// Provides O(1) comparsions and hashing, cheap copy, and cheap conversion to [`IBytes`]27#[derive(Clone, PartialOrd, Ord, Eq)]28pub struct IStr(Inner);29impl Trace for IStr {30 fn is_type_tracked() -> bool {31 false32 }33}3435/// SAFETY:36///37/// `IStr` is acyclic38unsafe impl Acyclic for IStr {}3940impl IStr {41 #[must_use]42 pub fn empty() -> Self {43 "".into()44 }45 #[must_use]46 pub fn as_str(&self) -> &str {47 self as &str48 }4950 #[must_use]51 pub fn cast_bytes(self) -> IBytes {52 IBytes(self.0.clone())53 }54}5556impl Deref for IStr {57 type Target = str;5859 fn deref(&self) -> &Self::Target {60 // SAFETY: Inner::check_utf8 is called on IStr construction, data is utf-861 unsafe { self.0.as_str_unchecked() }62 }63}6465impl PartialEq for IStr {66 fn eq(&self, other: &Self) -> bool {67 // all IStr should be inlined into same pool68 Inner::ptr_eq(&self.0, &other.0)69 }70}7172impl PartialEq<str> for IStr {73 fn eq(&self, other: &str) -> bool {74 self as &str == other75 }76}7778impl Hash for IStr {79 fn hash<H: Hasher>(&self, state: &mut H) {80 // IStr is always obtained from pool, where no string have duplicate, thus every unique string has unique address81 state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);82 }83}8485impl Drop for IStr {86 fn drop(&mut self) {87 maybe_unpool(&self.0);88 }89}9091impl fmt::Debug for IStr {92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {93 fmt::Debug::fmt(self as &str, f)94 }95}9697impl Display for IStr {98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {99 fmt::Display::fmt(self as &str, f)100 }101}102103/// Interned byte array104#[derive(Clone, PartialOrd, Ord, Eq)]105pub struct IBytes(Inner);106impl Trace for IBytes {107 fn is_type_tracked() -> bool {108 false109 }110}111112impl IBytes {113 #[must_use]114 pub fn cast_str(self) -> Option<IStr> {115 if Inner::check_utf8(&self.0) {116 Some(IStr(self.0.clone()))117 } else {118 None119 }120 }121 /// # Safety122 /// data should be valid utf8123 unsafe fn cast_str_unchecked(self) -> IStr {124 // SAFETY: data is utf8125 unsafe { Inner::assume_utf8(&self.0) };126 IStr(self.0.clone())127 }128129 #[must_use]130 pub fn as_slice(&self) -> &[u8] {131 self.0.as_slice()132 }133}134135impl Deref for IBytes {136 type Target = [u8];137138 fn deref(&self) -> &Self::Target {139 self.0.as_slice()140 }141}142143impl PartialEq for IBytes {144 fn eq(&self, other: &Self) -> bool {145 // all IStr should be inlined into same pool146 Inner::ptr_eq(&self.0, &other.0)147 }148}149150impl Hash for IBytes {151 fn hash<H: Hasher>(&self, state: &mut H) {152 // IBytes is always obtained from pool, where no string have duplicate, thus every unique string has unique address153 state.write_usize(Inner::as_ptr(&self.0).cast::<()>() as usize);154 }155}156157impl Drop for IBytes {158 fn drop(&mut self) {159 maybe_unpool(&self.0);160 }161}162163fn maybe_unpool(inner: &Inner) {164 #[cold]165 #[inline(never)]166 fn unpool(inner: &Inner) {167 // May fail on program termination168 let _ = POOL.try_with(|pool| {169 let mut pool = pool.borrow_mut();170171 if pool.remove(inner).is_none() {172 // On some platforms (i.e i686-windows), try_with will not fail after TLS173 // destructor is called, but instead re-initialize the TLS with the empty pool.174 // Allow non-pooled Drop in this case.175 // https://github.com/CertainLach/jrsonnet/issues/98#issuecomment-1591624016176 //177 // However, if pool is not empty, most likely this is issue #113, and then I don't178 // have any explainations for now.179 assert!(pool.is_empty(), "received an unpooled string not during the program termination, please write any info regarding this crash to https://github.com/CertainLach/jrsonnet/issues/113, thanks!");180 }181 });182 }183 // First reference - current object, second - POOL184 if Inner::strong_count(inner) <= 2 {185 unpool(inner);186 }187}188189impl fmt::Debug for IBytes {190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {191 fmt::Debug::fmt(self as &[u8], f)192 }193}194195impl<'c> From<Cow<'c, str>> for IStr {196 fn from(v: Cow<'c, str>) -> Self {197 intern_str(&v)198 }199}200impl From<&str> for IStr {201 fn from(v: &str) -> Self {202 intern_str(v)203 }204}205impl From<String> for IStr {206 fn from(s: String) -> Self {207 s.as_str().into()208 }209}210impl From<&String> for IStr {211 fn from(s: &String) -> Self {212 s.as_str().into()213 }214}215impl From<char> for IStr {216 fn from(value: char) -> Self {217 let mut buf = [0; 5];218 Self::from(&*value.encode_utf8(&mut buf))219 }220}221impl From<&[u8]> for IBytes {222 fn from(v: &[u8]) -> Self {223 intern_bytes(v)224 }225}226227type PoolMap = HashMap<Inner, (), FxBuildHasher>;228229thread_local! {230 static POOL: RefCell<PoolMap> = RefCell::new(HashMap::with_capacity_and_hasher(200, FxBuildHasher));231}232233/// Utils for embedding jrsonnet in non-rust.234///235/// Jrsonnet golang bindings require that it is possible to move jsonnet236/// VM between OS threads, and this is not possible due to usage of237/// `thread_local`. Instead, there is two methods added, one should be238/// called at the end of current thread work, and one that should be239/// used when using other thread.240pub mod interop {241 use std::mem;242243 use crate::{PoolMap, POOL};244245 /// Type-erased interned string pool246 pub enum PoolState {}247248 /// Dump current interned string pool, to be restored by249 /// `reenter_thread`250 pub fn exit_thread() -> *mut PoolState {251 Box::into_raw(Box::new(POOL.with_borrow_mut(mem::take))).cast()252 }253254 /// Reenter thread, using state dumped by `exit_thread`.255 ///256 /// # Safety257 ///258 /// `state` should be acquired from `exit_thread`, it is not allowed259 /// to reuse state to reenter multiple threads.260 pub unsafe fn reenter_thread(state: *mut PoolState) {261 let ptr: *mut PoolMap = state.cast();262 // SAFETY: ptr is an unique state per method safety requirements.263 let ptr: Box<PoolMap> = unsafe { Box::from_raw(ptr) };264 let ptr: PoolMap = *ptr;265 POOL.with_borrow_mut(|pool| {266 let _ = mem::replace(pool, ptr);267 });268 }269}270271#[must_use]272pub fn intern_bytes(bytes: &[u8]) -> IBytes {273 POOL.with(|pool| {274 let mut pool = pool.borrow_mut();275 let entry = pool.raw_entry_mut().from_key(bytes);276 match entry {277 RawEntryMut::Occupied(i) => IBytes(i.get_key_value().0.clone()),278 RawEntryMut::Vacant(e) => {279 let (k, ()) = e.insert(Inner::new_bytes(bytes), ());280 IBytes(k.clone())281 }282 }283 })284}285286#[must_use]287pub fn intern_str(str: &str) -> IStr {288 // SAFETY: Rust strings always utf8289 unsafe { intern_bytes(str.as_bytes()).cast_str_unchecked() }290}291292#[cfg(test)]293mod tests {294 use crate::IStr;295296 #[test]297 fn simple() {298 let a = IStr::from("a");299 let b = IStr::from("a");300301 assert_eq!(a.as_ptr(), b.as_ptr());302 }303}crates/jrsonnet-interner/src/names.rsdiffbeforeafterbothno changes
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -13,6 +13,11 @@
LitStr, Meta, Pat, Path, PathArguments, Result, ReturnType, Token, Type,
};
+use self::typed::derive_typed_inner;
+
+mod typed;
+mod names;
+
fn try_parse_attr_noargs<I>(attrs: &[Attribute], ident: I) -> Result<bool>
where
Ident: PartialEq<I>,
@@ -435,278 +440,6 @@
}
};
})
-}
-
-#[derive(Default)]
-#[allow(clippy::struct_excessive_bools)]
-struct TypedAttr {
- rename: Option<String>,
- aliases: Vec<String>,
- flatten: bool,
- /// flatten(ok) strategy for flattened optionals
- /// field would be None in case of any parsing error (as in serde)
- flatten_ok: bool,
- // Should it be `field+:` instead of `field:`
- add: bool,
- // Should it be `field::` instead of `field:`
- hide: bool,
-}
-impl Parse for TypedAttr {
- fn parse(input: ParseStream) -> syn::Result<Self> {
- let mut out = Self::default();
- loop {
- let lookahead = input.lookahead1();
- if lookahead.peek(kw::rename) {
- input.parse::<kw::rename>()?;
- input.parse::<Token![=]>()?;
- let name = input.parse::<LitStr>()?;
- if out.rename.is_some() {
- return Err(Error::new(
- name.span(),
- "rename attribute may only be specified once",
- ));
- }
- out.rename = Some(name.value());
- } else if lookahead.peek(kw::alias) {
- input.parse::<kw::alias>()?;
- input.parse::<Token![=]>()?;
- let alias = input.parse::<LitStr>()?;
- out.aliases.push(alias.value());
- } else if lookahead.peek(kw::flatten) {
- input.parse::<kw::flatten>()?;
- out.flatten = true;
- if input.peek(token::Paren) {
- let content;
- parenthesized!(content in input);
- let lookahead = content.lookahead1();
- if lookahead.peek(kw::ok) {
- content.parse::<kw::ok>()?;
- out.flatten_ok = true;
- } else {
- return Err(lookahead.error());
- }
- }
- } else if lookahead.peek(kw::add) {
- input.parse::<kw::add>()?;
- out.add = true;
- } else if lookahead.peek(kw::hide) {
- input.parse::<kw::hide>()?;
- out.hide = true;
- } else if input.is_empty() {
- break;
- } else {
- return Err(lookahead.error());
- }
- if input.peek(Token![,]) {
- input.parse::<Token![,]>()?;
- } else {
- break;
- }
- }
- Ok(out)
- }
-}
-
-struct TypedField {
- attr: TypedAttr,
- ident: Ident,
- ty: Type,
- is_option: bool,
- is_lazy: bool,
-}
-impl TypedField {
- fn parse(field: &syn::Field) -> Result<Self> {
- let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();
- let Some(ident) = field.ident.clone() else {
- return Err(Error::new(
- field.span(),
- "this field should appear in output object, but it has no visible name",
- ));
- };
- let (is_option, ty) = extract_type_from_option(&field.ty)?
- .map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));
- if is_option && attr.flatten {
- if !attr.flatten_ok {
- return Err(Error::new(
- field.span(),
- "strategy should be set when flattening Option",
- ));
- }
- } else if attr.flatten_ok {
- return Err(Error::new(
- field.span(),
- "flatten(ok) is only useable on optional fields",
- ));
- }
-
- let is_lazy = type_is_path(&ty, "Thunk").is_some();
-
- Ok(Self {
- attr,
- ident,
- ty,
- is_option,
- is_lazy,
- })
- }
- /// None if this field is flattened in jsonnet output
- fn name(&self) -> Option<String> {
- if self.attr.flatten {
- return None;
- }
- Some(
- self.attr
- .rename
- .clone()
- .unwrap_or_else(|| self.ident.to_string()),
- )
- }
-
- fn expand_field(&self) -> Option<TokenStream> {
- if self.is_option {
- return None;
- }
- let name = self.name()?;
- let ty = &self.ty;
- Some(quote! {
- (#name, <#ty as Typed>::TYPE)
- })
- }
-
- fn expand_parse(&self) -> TokenStream {
- if self.is_option {
- self.expand_parse_optional()
- } else {
- self.expand_parse_mandatory()
- }
- }
-
- fn expand_parse_optional(&self) -> TokenStream {
- let ident = &self.ident;
- let ty = &self.ty;
-
- // optional flatten is handled in same way as serde
- if self.attr.flatten {
- return quote! {
- #ident: <#ty as TypedObj>::parse(&obj).ok(),
- };
- }
-
- let name = self.name().unwrap();
- let aliases = &self.attr.aliases;
-
- quote! {
- #ident: {
- let __value = if let Some(__v) = obj.get(#name.into())? {
- Some(__v)
- } #(else if let Some(__v) = obj.get(#aliases.into())? {
- Some(__v)
- })* else {
- None
- };
-
- __value.map(<#ty as Typed>::from_untyped).transpose()?
- },
- }
- }
-
- fn expand_parse_mandatory(&self) -> TokenStream {
- let ident = &self.ident;
- let ty = &self.ty;
-
- // optional flatten is handled in same way as serde
- if self.attr.flatten {
- return quote! {
- #ident: <#ty as TypedObj>::parse(&obj)?,
- };
- }
-
- let name = self.name().unwrap();
- let aliases = &self.attr.aliases;
-
- let error_text = if aliases.is_empty() {
- // clippy does not understand name variable usage in quote! macro
- #[allow(clippy::redundant_clone)]
- name.clone()
- } else {
- format!("{name} (alias {})", aliases.join(", "))
- };
-
- quote! {
- #ident: {
- let __value = if let Some(__v) = obj.get(#name.into())? {
- __v
- } #(else if let Some(__v) = obj.get(#aliases.into())? {
- __v
- })* else {
- return Err(ErrorKind::NoSuchField(#error_text.into(), vec![]).into());
- };
-
- <#ty as Typed>::from_untyped(__value)?
- },
- }
- }
-
- fn expand_serialize(&self) -> TokenStream {
- let ident = &self.ident;
- let ty = &self.ty;
- self.name().map_or_else(
- || {
- if self.is_option {
- quote! {
- if let Some(value) = self.#ident {
- <#ty as TypedObj>::serialize(value, out)?;
- }
- }
- } else {
- quote! {
- <#ty as TypedObj>::serialize(self.#ident, out)?;
- }
- }
- },
- |name| {
- let hide = if self.attr.hide {
- quote! {.hide()}
- } else {
- quote! {}
- };
- let add = if self.attr.add {
- quote! {.add()}
- } else {
- quote! {}
- };
- let value = if self.is_lazy {
- quote! {
- out.field(#name)
- #hide
- #add
- .try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;
- }
- } else {
- quote! {
- out.field(#name)
- #hide
- #add
- .try_value(<#ty as Typed>::into_untyped(value)?)?;
- }
- };
- if self.is_option {
- quote! {
- if let Some(value) = self.#ident {
- #value
- }
- }
- } else {
- quote! {
- {
- let value = self.#ident;
- #value
- }
- }
- }
- },
- )
- }
}
#[proc_macro_derive(Typed, attributes(typed))]
@@ -717,79 +450,6 @@
Ok(v) => v.into(),
Err(e) => e.to_compile_error().into(),
}
-}
-
-fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {
- let syn::Data::Struct(data) = &input.data else {
- return Err(Error::new(input.span(), "only structs supported"));
- };
-
- let ident = &input.ident;
- let fields = data
- .fields
- .iter()
- .map(TypedField::parse)
- .collect::<Result<Vec<_>>>()?;
-
- let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
-
- let typed = {
- let fields = fields
- .iter()
- .filter_map(TypedField::expand_field)
- .collect::<Vec<_>>();
- quote! {
- impl #impl_generics Typed for #ident #ty_generics #where_clause {
- const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[
- #(#fields,)*
- ]);
-
- fn from_untyped(value: Val) -> JrResult<Self> {
- let obj = value.as_obj().expect("shape is correct");
- Self::parse(&obj)
- }
-
- fn into_untyped(value: Self) -> JrResult<Val> {
- let mut out = ObjValueBuilder::new();
- value.serialize(&mut out)?;
- Ok(Val::Obj(out.build()))
- }
-
- }
- }
- };
-
- let fields_parse = fields.iter().map(TypedField::expand_parse);
- let fields_serialize = fields
- .iter()
- .map(TypedField::expand_serialize)
- .collect::<Vec<_>>();
-
- Ok(quote! {
- const _: () = {
- use ::jrsonnet_evaluator::{
- typed::{ComplexValType, Typed, TypedObj, CheckType},
- Val, State,
- error::{ErrorKind, Result as JrResult},
- ObjValueBuilder, ObjValue,
- };
-
- #typed
-
- impl #impl_generics TypedObj for #ident #ty_generics #where_clause {
- fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {
- #(#fields_serialize)*
-
- Ok(())
- }
- fn parse(obj: &ObjValue) -> JrResult<Self> {
- Ok(Self {
- #(#fields_parse)*
- })
- }
- }
- };
- })
}
struct FormatInput {
crates/jrsonnet-macros/src/names.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-macros/src/names.rs
@@ -0,0 +1,32 @@
+use proc_macro2::TokenStream;
+use quote::quote;
+use std::cell::RefCell;
+
+#[derive(Default)]
+pub struct Names {
+ names: Vec<String>,
+}
+
+impl Names {
+ pub fn intern(&mut self, s: impl AsRef<str>) -> usize {
+ let s = s.as_ref();
+ if let Some(pos) = self.names.iter().position(|v| v == s) {
+ return pos;
+ }
+ let pos = self.names.len();
+ self.names.push(s.to_owned());
+ pos
+ }
+
+ pub fn expand(&self) -> TokenStream {
+ let len = self.names.len();
+ let name = self.names.iter();
+ quote! {
+ thread_local! {
+ static NAMES: [::jrsonnet_evaluator::IStr; #len] = [
+ #(::jrsonnet_evaluator::IStr::from(#name),)*
+ ];
+ }
+ }
+ }
+}
crates/jrsonnet-macros/src/typed.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-macros/src/typed.rs
@@ -0,0 +1,373 @@
+use crate::names::Names;
+use crate::{extract_type_from_option, kw, parse_attr, type_is_path};
+use proc_macro2::TokenStream;
+use quote::quote;
+use syn::parse::{Parse, ParseStream};
+use syn::spanned::Spanned as _;
+use syn::{parenthesized, token, DeriveInput, Error, Ident, LitStr, Result, Token, Type};
+
+#[derive(Default)]
+#[allow(clippy::struct_excessive_bools)]
+struct TypedAttr {
+ rename: Option<String>,
+ aliases: Vec<String>,
+ flatten: bool,
+ /// flatten(ok) strategy for flattened optionals
+ /// field would be None in case of any parsing error (as in serde)
+ flatten_ok: bool,
+ // Should it be `field+:` instead of `field:`
+ add: bool,
+ // Should it be `field::` instead of `field:`
+ hide: bool,
+}
+impl Parse for TypedAttr {
+ fn parse(input: ParseStream) -> syn::Result<Self> {
+ let mut out = Self::default();
+ loop {
+ let lookahead = input.lookahead1();
+ if lookahead.peek(kw::rename) {
+ input.parse::<kw::rename>()?;
+ input.parse::<Token![=]>()?;
+ let name = input.parse::<LitStr>()?;
+ if out.rename.is_some() {
+ return Err(Error::new(
+ name.span(),
+ "rename attribute may only be specified once",
+ ));
+ }
+ out.rename = Some(name.value());
+ } else if lookahead.peek(kw::alias) {
+ input.parse::<kw::alias>()?;
+ input.parse::<Token![=]>()?;
+ let alias = input.parse::<LitStr>()?;
+ out.aliases.push(alias.value());
+ } else if lookahead.peek(kw::flatten) {
+ input.parse::<kw::flatten>()?;
+ out.flatten = true;
+ if input.peek(token::Paren) {
+ let content;
+ parenthesized!(content in input);
+ let lookahead = content.lookahead1();
+ if lookahead.peek(kw::ok) {
+ content.parse::<kw::ok>()?;
+ out.flatten_ok = true;
+ } else {
+ return Err(lookahead.error());
+ }
+ }
+ } else if lookahead.peek(kw::add) {
+ input.parse::<kw::add>()?;
+ out.add = true;
+ } else if lookahead.peek(kw::hide) {
+ input.parse::<kw::hide>()?;
+ out.hide = true;
+ } else if input.is_empty() {
+ break;
+ } else {
+ return Err(lookahead.error());
+ }
+ if input.peek(Token![,]) {
+ input.parse::<Token![,]>()?;
+ } else {
+ break;
+ }
+ }
+ Ok(out)
+ }
+}
+struct TypedField {
+ attr: TypedAttr,
+ ident: Ident,
+ ty: Type,
+ is_option: bool,
+ is_lazy: bool,
+}
+impl TypedField {
+ fn parse(field: &syn::Field) -> Result<Self> {
+ let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();
+ let Some(ident) = field.ident.clone() else {
+ return Err(Error::new(
+ field.span(),
+ "this field should appear in output object, but it has no visible name",
+ ));
+ };
+ let (is_option, ty) = extract_type_from_option(&field.ty)?
+ .map_or_else(|| (false, field.ty.clone()), |ty| (true, ty.clone()));
+ if is_option && attr.flatten {
+ if !attr.flatten_ok {
+ return Err(Error::new(
+ field.span(),
+ "strategy should be set when flattening Option",
+ ));
+ }
+ } else if attr.flatten_ok {
+ return Err(Error::new(
+ field.span(),
+ "flatten(ok) is only useable on optional fields",
+ ));
+ }
+
+ let is_lazy = type_is_path(&ty, "Thunk").is_some();
+
+ Ok(Self {
+ attr,
+ ident,
+ ty,
+ is_option,
+ is_lazy,
+ })
+ }
+ /// None if this field is flattened in jsonnet output
+ fn name(&self) -> Option<String> {
+ if self.attr.flatten {
+ return None;
+ }
+ Some(
+ self.attr
+ .rename
+ .clone()
+ .unwrap_or_else(|| self.ident.to_string()),
+ )
+ }
+
+ fn expand_field(&self) -> Option<TokenStream> {
+ if self.is_option {
+ return None;
+ }
+ let name = self.name()?;
+ let ty = &self.ty;
+ Some(quote! {
+ (#name, <#ty as Typed>::TYPE)
+ })
+ }
+
+ fn expand_parse(&self, names: &mut Names) -> TokenStream {
+ if self.is_option {
+ self.expand_parse_optional(names)
+ } else {
+ self.expand_parse_mandatory(names)
+ }
+ }
+
+ fn expand_parse_optional(&self, names: &mut Names) -> TokenStream {
+ let ident = &self.ident;
+ let ty = &self.ty;
+
+ // optional flatten is handled in same way as serde
+ if self.attr.flatten {
+ return quote! {
+ #ident: <#ty as TypedObj>::parse(&obj).ok(),
+ };
+ }
+
+ let name = names.intern(self.name().unwrap());
+ let aliases = self
+ .attr
+ .aliases
+ .iter()
+ .map(|name| names.intern(name))
+ .collect::<Vec<_>>();
+
+ quote! {
+ #ident: {
+ let __value = if let Some(__v) = obj.get(__names[#name].clone())? {
+ Some(__v)
+ } #(else if let Some(__v) = obj.get(__names[#aliases].clone())? {
+ Some(__v)
+ })* else {
+ None
+ };
+
+ __value.map(<#ty as Typed>::from_untyped).transpose()?
+ },
+ }
+ }
+
+ fn expand_parse_mandatory(&self, names: &mut Names) -> TokenStream {
+ let ident = &self.ident;
+ let ty = &self.ty;
+
+ // optional flatten is handled in same way as serde
+ if self.attr.flatten {
+ return quote! {
+ #ident: <#ty as TypedObj>::parse(&obj)?,
+ };
+ }
+
+ let name = self.name().unwrap();
+ let aliases = &self.attr.aliases;
+
+ let error_text = if aliases.is_empty() {
+ // clippy does not understand name variable usage in quote! macro
+ #[allow(clippy::redundant_clone)]
+ name.clone()
+ } else {
+ format!("{name} (alias {})", aliases.join(", "))
+ };
+
+ let error_text = names.intern(error_text);
+ let name = names.intern(name);
+ let aliases = aliases.iter().map(|alias| names.intern(alias));
+
+ quote! {
+ #ident: {
+ let __value = if let Some(__v) = obj.get(__names[#name].clone())? {
+ __v
+ } #(else if let Some(__v) = obj.get(__names[#aliases].clone())? {
+ __v
+ })* else {
+ return Err(ErrorKind::NoSuchField(__names[#error_text].clone(), vec![]).into());
+ };
+
+ <#ty as Typed>::from_untyped(__value)?
+ },
+ }
+ }
+
+ fn expand_serialize(&self, names: &mut Names) -> TokenStream {
+ let ident = &self.ident;
+ let ty = &self.ty;
+ self.name().map_or_else(
+ || {
+ if self.is_option {
+ quote! {
+ if let Some(value) = self.#ident {
+ <#ty as TypedObj>::serialize(value, out)?;
+ }
+ }
+ } else {
+ quote! {
+ <#ty as TypedObj>::serialize(self.#ident, out)?;
+ }
+ }
+ },
+ |name| {
+ let name = names.intern(name);
+ let hide = if self.attr.hide {
+ quote! {.hide()}
+ } else {
+ quote! {}
+ };
+ let add = if self.attr.add {
+ quote! {.add()}
+ } else {
+ quote! {}
+ };
+ let value = if self.is_lazy {
+ quote! {
+ out.field(__names[#name].clone())
+ #hide
+ #add
+ .try_thunk(<#ty as Typed>::into_lazy_untyped(value))?;
+ }
+ } else {
+ quote! {
+ out.field(__names[#name].clone())
+ #hide
+ #add
+ .try_value(<#ty as Typed>::into_untyped(value)?)?;
+ }
+ };
+ if self.is_option {
+ quote! {
+ if let Some(value) = self.#ident {
+ #value
+ }
+ }
+ } else {
+ quote! {
+ {
+ let value = self.#ident;
+ #value
+ }
+ }
+ }
+ },
+ )
+ }
+}
+
+pub fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {
+ let syn::Data::Struct(data) = &input.data else {
+ return Err(Error::new(input.span(), "only structs supported"));
+ };
+
+ let ident = &input.ident;
+ let fields = data
+ .fields
+ .iter()
+ .map(TypedField::parse)
+ .collect::<Result<Vec<_>>>()?;
+
+ let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
+
+ let capacity = fields.len();
+
+ let typed = {
+ let fields = fields
+ .iter()
+ .filter_map(TypedField::expand_field)
+ .collect::<Vec<_>>();
+ quote! {
+ impl #impl_generics Typed for #ident #ty_generics #where_clause {
+ const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&[
+ #(#fields,)*
+ ]);
+
+ fn from_untyped(value: Val) -> JrResult<Self> {
+ let obj = value.as_obj().expect("shape is correct");
+ Self::parse(&obj)
+ }
+
+ fn into_untyped(value: Self) -> JrResult<Val> {
+ let mut out = ObjValueBuilder::with_capacity(#capacity);
+ value.serialize(&mut out)?;
+ Ok(Val::Obj(out.build()))
+ }
+
+ }
+ }
+ };
+
+ let mut names = Names::default();
+
+ let fields_parse = fields
+ .iter()
+ .map(|f| f.expand_parse(&mut names))
+ .collect::<Vec<_>>();
+ let fields_serialize = fields
+ .iter()
+ .map(|f| f.expand_serialize(&mut names))
+ .collect::<Vec<_>>();
+
+ let names_expanded = names.expand();
+ Ok(quote! {
+ const _: () = {
+ use ::jrsonnet_evaluator::{
+ typed::{ComplexValType, Typed, TypedObj, CheckType},
+ Val, State,
+ error::{ErrorKind, Result as JrResult},
+ ObjValueBuilder, ObjValue, IStr,
+ };
+
+ #typed
+
+ #names_expanded
+
+ impl #impl_generics TypedObj for #ident #ty_generics #where_clause {
+ fn serialize(self, out: &mut ObjValueBuilder) -> JrResult<()> {
+ NAMES.with(|__names| {
+ #(#fields_serialize)*
+
+ Ok(())
+ })
+ }
+ fn parse(obj: &ObjValue) -> JrResult<Self> {
+ NAMES.with(|__names| Ok(Self {
+ #(#fields_parse)*
+ }))
+ }
+ }
+ };
+ })
+}
crates/jrsonnet-stdlib/src/regex.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/regex.rs
+++ b/crates/jrsonnet-stdlib/src/regex.rs
@@ -4,8 +4,9 @@
use jrsonnet_evaluator::{
error::{ErrorKind::*, Result},
rustc_hash::FxBuildHasher,
+ typed::Typed,
val::StrValue,
- IStr, ObjValueBuilder, Val,
+ IStr, ObjValue, ObjValueBuilder,
};
use jrsonnet_gcmodule::Acyclic;
use jrsonnet_macros::builtin;
@@ -20,7 +21,7 @@
Self {
cache: RefCell::new(LruCache::with_hasher(
NonZeroUsize::new(20).unwrap(),
- FxBuildHasher::default(),
+ FxBuildHasher,
)),
}
}
@@ -40,21 +41,27 @@
}
}
-pub fn regex_match_inner(regex: &Regex, str: String) -> Result<Val> {
- let mut out = ObjValueBuilder::with_capacity(3);
+#[derive(Typed)]
+pub struct RegexMatch {
+ string: IStr,
+ captures: Vec<IStr>,
+ #[typed(rename = "namedCaptures")]
+ named_captures: ObjValue,
+}
+fn regex_match_inner(regex: &Regex, str: String) -> Result<Option<RegexMatch>> {
let mut captures = Vec::with_capacity(regex.captures_len());
let mut named_captures = ObjValueBuilder::with_capacity(regex.capture_names().len());
let Some(captured) = regex.captures(&str) else {
- return Ok(Val::Null);
+ return Ok(None);
};
for ele in captured.iter().skip(1) {
if let Some(ele) = ele {
- captures.push(Val::Str(StrValue::Flat(ele.as_str().into())));
+ captures.push(ele.as_str().into());
} else {
- captures.push(Val::Str(StrValue::Flat(IStr::empty())));
+ captures.push(IStr::empty());
}
}
for (i, name) in regex
@@ -67,13 +74,11 @@
named_captures.field(name).try_value(capture)?;
}
- out.field("string")
- .value(Val::Str(captured.get(0).unwrap().as_str().into()));
- out.field("captures").value(Val::Arr(captures.into()));
- out.field("namedCaptures")
- .value(Val::Obj(named_captures.build()));
-
- Ok(Val::Obj(out.build()))
+ Ok(Some(RegexMatch {
+ string: captured.get(0).expect("regex matched").as_str().into(),
+ named_captures: named_captures.build(),
+ captures,
+ }))
}
#[builtin(fields(
@@ -83,7 +88,7 @@
this: &builtin_regex_partial_match,
pattern: IStr,
str: String,
-) -> Result<Val> {
+) -> Result<Option<RegexMatch>> {
let regex = this.cache.parse(pattern)?;
regex_match_inner(®ex, str)
}
@@ -95,7 +100,7 @@
this: &builtin_regex_full_match,
pattern: StrValue,
str: String,
-) -> Result<Val> {
+) -> Result<Option<RegexMatch>> {
let pattern = format!("^{pattern}$").into();
let regex = this.cache.parse(pattern)?;
regex_match_inner(®ex, str)