difftreelog
style remove unused code
in: master
2 files changed
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -200,6 +200,11 @@
s.as_str().into()
}
}
+impl From<&String> for IStr {
+ fn from(s: &String) -> Self {
+ s.as_str().into()
+ }
+}
impl From<&[u8]> for IBytes {
fn from(v: &[u8]) -> Self {
intern_bytes(v)
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, 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 name: Ident,73 _colon: Token![:],74 ty: Type,75}76impl Parse for Field {77 fn parse(input: ParseStream) -> syn::Result<Self> {78 Ok(Self {79 name: input.parse()?,80 _colon: input.parse()?,81 ty: input.parse()?,82 })83 }84}8586mod kw {87 syn::custom_keyword!(fields);88 syn::custom_keyword!(rename);89 syn::custom_keyword!(flatten);90 syn::custom_keyword!(ok);91}9293struct EmptyAttr;94impl Parse for EmptyAttr {95 fn parse(_input: ParseStream) -> Result<Self> {96 Ok(Self)97 }98}99100struct BuiltinAttrs {101 fields: Vec<Field>,102}103impl Parse for BuiltinAttrs {104 fn parse(input: ParseStream) -> syn::Result<Self> {105 if input.is_empty() {106 return Ok(Self { fields: Vec::new() });107 }108 input.parse::<kw::fields>()?;109 let fields;110 parenthesized!(fields in input);111 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;112 Ok(Self {113 fields: p.into_iter().collect(),114 })115 }116}117118enum ArgInfo {119 Normal {120 ty: Box<Type>,121 is_option: bool,122 name: Option<String>,123 cfg_attrs: Vec<Attribute>,124 // ident: Ident,125 },126 Lazy {127 is_option: bool,128 name: Option<String>,129 },130 Context,131 Location,132 This,133}134135impl ArgInfo {136 fn parse(name: &str, arg: &FnArg) -> Result<Self> {137 let FnArg::Typed(arg) = arg else {138 unreachable!()139 };140 let ident = match &arg.pat as &Pat {141 Pat::Ident(i) => Some(i.ident.clone()),142 _ => None,143 };144 let ty = &arg.ty;145 if type_is_path(ty, "Context").is_some() {146 return Ok(Self::Context);147 } else if type_is_path(ty, "CallLocation").is_some() {148 return Ok(Self::Location);149 } else if type_is_path(ty, "Thunk").is_some() {150 return Ok(Self::Lazy {151 is_option: false,152 name: ident.map(|v| v.to_string()),153 });154 }155156 match ty as &Type {157 Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),158 _ => {}159 }160161 let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {162 if type_is_path(ty, "Thunk").is_some() {163 return Ok(Self::Lazy {164 is_option: true,165 name: ident.map(|v| v.to_string()),166 });167 }168169 (true, Box::new(ty.clone()))170 } else {171 (false, ty.clone())172 };173174 let cfg_attrs = arg175 .attrs176 .iter()177 .filter(|a| a.path.is_ident("cfg"))178 .cloned()179 .collect();180181 Ok(Self::Normal {182 ty,183 is_option,184 name: ident.map(|v| v.to_string()),185 cfg_attrs,186 })187 }188}189190#[proc_macro_attribute]191pub fn builtin(192 attr: proc_macro::TokenStream,193 item: proc_macro::TokenStream,194) -> proc_macro::TokenStream {195 let attr = parse_macro_input!(attr as BuiltinAttrs);196 let item: ItemFn = parse_macro_input!(item);197198 match builtin_inner(attr, item) {199 Ok(v) => v.into(),200 Err(e) => e.into_compile_error().into(),201 }202}203204fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {205 let ReturnType::Type(_, result) = &fun.sig.output else {206 return Err(Error::new(207 fun.sig.span(),208 "builtin should return something",209 ))210 };211212 let Some(args) = type_is_path(result, "Result") else {213 return Err(Error::new(result.span(), "return value should be result"));214215 };216 let PathArguments::AngleBracketed(params) = args else {217 return Err(Error::new(args.span(), "missing result generic"));218 };219 let generic_arg = params.args.iter().next().unwrap();220 // This argument must be a type:221 let GenericArgument::Type(result_inner) = generic_arg else {222 return Err(Error::new(223 generic_arg.span(),224 "option generic should be a type",225 ))226 };227228 let name = fun.sig.ident.to_string();229 let args = fun230 .sig231 .inputs232 .iter()233 .map(|arg| ArgInfo::parse(&name, arg))234 .collect::<Result<Vec<_>>>()?;235236 let params_desc = args.iter().flat_map(|a| match a {237 ArgInfo::Normal {238 is_option,239 name,240 cfg_attrs,241 ..242 } => {243 let name = name244 .as_ref()245 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})246 .unwrap_or_else(|| quote! {None});247 Some(quote! {248 #(#cfg_attrs)*249 BuiltinParam {250 name: #name,251 has_default: #is_option,252 },253 })254 }255 ArgInfo::Lazy { is_option, name } => {256 let name = name257 .as_ref()258 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})259 .unwrap_or_else(|| quote! {None});260 Some(quote! {261 BuiltinParam {262 name: #name,263 has_default: #is_option,264 },265 })266 }267 ArgInfo::Context => None,268 ArgInfo::Location => None,269 ArgInfo::This => None,270 });271272 let mut id = 0usize;273 let pass = args274 .iter()275 .map(|a| match a {276 ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {277 let cid = id;278 id += 1;279 (quote! {#cid}, a)280 }281 ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {282 (quote! {compile_error!("should not use id")}, a)283 }284 })285 .map(|(id, a)| match a {286 ArgInfo::Normal {287 ty,288 is_option,289 name,290 cfg_attrs,291 } => {292 let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");293 let eval = quote! {jrsonnet_evaluator::State::push_description(294 || format!("argument <{}> evaluation", #name),295 || <#ty>::from_untyped(value.evaluate()?),296 )?};297 let value = if *is_option {298 quote! {if let Some(value) = &parsed[#id] {299 Some(#eval)300 } else {301 None302 },}303 } else {304 quote! {{305 let value = parsed[#id].as_ref().expect("args shape is checked");306 #eval307 },}308 };309 quote! {310 #(#cfg_attrs)*311 #value312 }313 }314 ArgInfo::Lazy { is_option, .. } => {315 if *is_option {316 quote! {if let Some(value) = &parsed[#id] {317 Some(value.clone())318 } else {319 None320 }}321 } else {322 quote! {323 parsed[#id].as_ref().expect("args shape is correct").clone(),324 }325 }326 }327 ArgInfo::Context => quote! {ctx.clone(),},328 ArgInfo::Location => quote! {location,},329 ArgInfo::This => quote! {self,},330 });331332 let fields = attr.fields.iter().map(|field| {333 let name = &field.name;334 let ty = &field.ty;335 quote! {336 pub #name: #ty,337 }338 });339340 let name = &fun.sig.ident;341 let vis = &fun.vis;342 let static_ext = if attr.fields.is_empty() {343 quote! {344 impl #name {345 pub const INST: &'static dyn StaticBuiltin = &#name {};346 }347 impl StaticBuiltin for #name {}348 }349 } else {350 quote! {}351 };352 let static_derive_copy = if attr.fields.is_empty() {353 quote! {, Copy}354 } else {355 quote! {}356 };357358 Ok(quote! {359 #fun360 #[doc(hidden)]361 #[allow(non_camel_case_types)]362 #[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]363 #vis struct #name {364 #(#fields)*365 }366 const _: () = {367 use ::jrsonnet_evaluator::{368 State, Val,369 function::{builtin::{Builtin, StaticBuiltin, BuiltinParam}, CallLocation, ArgsLike, parse::parse_builtin_call},370 error::Result, Context, typed::Typed,371 parser::ExprLocation,372 };373 const PARAMS: &'static [BuiltinParam] = &[374 #(#params_desc)*375 ];376377 #static_ext378 impl Builtin for #name379 where380 Self: 'static381 {382 fn name(&self) -> &str {383 stringify!(#name)384 }385 fn params(&self) -> &[BuiltinParam] {386 PARAMS387 }388 fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {389 let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;390391 let result: #result = #name(#(#pass)*);392 let result = result?;393 <#result_inner>::into_untyped(result)394 }395 }396 };397 })398}399400#[derive(Default)]401struct TypedAttr {402 rename: Option<String>,403 flatten: bool,404 /// flatten(ok) strategy for flattened optionals405 /// field would be None in case of any parsing error (as in serde)406 flatten_ok: bool,407}408impl Parse for TypedAttr {409 fn parse(input: ParseStream) -> syn::Result<Self> {410 let mut out = Self::default();411 loop {412 let lookahead = input.lookahead1();413 if lookahead.peek(kw::rename) {414 input.parse::<kw::rename>()?;415 input.parse::<Token![=]>()?;416 let name = input.parse::<LitStr>()?;417 if out.rename.is_some() {418 return Err(Error::new(419 name.span(),420 "rename attribute may only be specified once",421 ));422 }423 out.rename = Some(name.value());424 } else if lookahead.peek(kw::flatten) {425 input.parse::<kw::flatten>()?;426 out.flatten = true;427 if input.peek(token::Paren) {428 let content;429 parenthesized!(content in input);430 let lookahead = content.lookahead1();431 if lookahead.peek(kw::ok) {432 content.parse::<kw::ok>()?;433 out.flatten_ok = true;434 } else {435 return Err(lookahead.error());436 }437 }438 } else if input.is_empty() {439 break;440 } else {441 return Err(lookahead.error());442 }443 if input.peek(Token![,]) {444 input.parse::<Token![,]>()?;445 } else {446 break;447 }448 }449 // input.parse::<kw::rename>()?;450 // input.parse::<Token![=]>()?;451 // let rename = input.parse::<LitStr>()?.value();452 Ok(out)453 }454}455456struct TypedField {457 attr: TypedAttr,458 ident: Ident,459 ty: Type,460 is_option: bool,461}462impl TypedField {463 fn parse(field: &syn::Field) -> Result<Self> {464 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();465 let Some(ident) = field.ident.clone() else {466 return Err(Error::new(467 field.span(),468 "this field should appear in output object, but it has no visible name",469 ));470 };471 let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {472 (true, ty.clone())473 } else {474 (false, field.ty.clone())475 };476 if is_option && attr.flatten {477 if !attr.flatten_ok {478 return Err(Error::new(479 field.span(),480 "strategy should be set when flattening Option",481 ));482 }483 } else if attr.flatten_ok {484 return Err(Error::new(485 field.span(),486 "flatten(ok) is only useable on optional fields",487 ));488 }489490 Ok(Self {491 attr,492 ident,493 ty,494 is_option,495 })496 }497 /// None if this field is flattened in jsonnet output498 fn name(&self) -> Option<String> {499 if self.attr.flatten {500 return None;501 }502 Some(503 self.attr504 .rename505 .clone()506 .unwrap_or_else(|| self.ident.to_string()),507 )508 }509510 fn expand_field(&self) -> Option<TokenStream> {511 if self.is_option {512 return None;513 }514 let name = self.name()?;515 let ty = &self.ty;516 Some(quote! {517 (#name, <#ty>::TYPE)518 })519 }520 fn expand_parse(&self) -> TokenStream {521 let ident = &self.ident;522 let ty = &self.ty;523 if self.attr.flatten {524 // optional flatten is handled in same way as serde525 return if self.is_option {526 quote! {527 #ident: <#ty>::parse(&obj).ok(),528 }529 } else {530 quote! {531 #ident: <#ty>::parse(&obj)?,532 }533 };534 };535536 let name = self.name().unwrap();537 let value = if self.is_option {538 quote! {539 if let Some(value) = obj.get(#name.into())? {540 Some(<#ty>::from_untyped(value)?)541 } else {542 None543 }544 }545 } else {546 quote! {547 <#ty>::from_untyped(obj.get(#name.into())?.ok_or_else(|| Error::NoSuchField(#name.into(), vec![]))?)?548 }549 };550551 quote! {552 #ident: #value,553 }554 }555 fn expand_serialize(&self) -> Result<TokenStream> {556 let ident = &self.ident;557 let ty = &self.ty;558 Ok(if let Some(name) = self.name() {559 if self.is_option {560 quote! {561 if let Some(value) = self.#ident {562 out.member(#name.into()).value(<#ty>::into_untyped(value)?)?;563 }564 }565 } else {566 quote! {567 out.member(#name.into()).value(<#ty>::into_untyped(self.#ident)?)?;568 }569 }570 } else if self.is_option {571 quote! {572 if let Some(value) = self.#ident {573 value.serialize(out)?;574 }575 }576 } else {577 quote! {578 self.#ident.serialize(out)?;579 }580 })581 }582}583584#[proc_macro_derive(Typed, attributes(typed))]585pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {586 let input = parse_macro_input!(item as DeriveInput);587588 match derive_typed_inner(input) {589 Ok(v) => v.into(),590 Err(e) => e.to_compile_error().into(),591 }592}593594fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {595 let syn::Data::Struct(data) = &input.data else {596 return Err(Error::new(input.span(), "only structs supported"));597 };598599 let ident = &input.ident;600 let fields = data601 .fields602 .iter()603 .map(TypedField::parse)604 .collect::<Result<Vec<_>>>()?;605606 let typed = {607 let fields = fields608 .iter()609 .flat_map(TypedField::expand_field)610 .collect::<Vec<_>>();611 let len = fields.len();612 quote! {613 const ITEMS: [(&'static str, &'static ComplexValType); #len] = [614 #(#fields,)*615 ];616 impl Typed for #ident {617 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);618619 fn from_untyped(value: Val) -> Result<Self> {620 let obj = value.as_obj().expect("shape is correct");621 Self::parse(&obj)622 }623624 fn into_untyped(value: Self) -> Result<Val> {625 let mut out = ObjValueBuilder::new();626 value.serialize(&mut out)?;627 Ok(Val::Obj(out.build()))628 }629630 }631 }632 };633634 let fields_parse = fields.iter().map(TypedField::expand_parse);635 let fields_serialize = fields636 .iter()637 .map(TypedField::expand_serialize)638 .collect::<Result<Vec<_>>>()?;639640 Ok(quote! {641 const _: () = {642 use ::jrsonnet_evaluator::{643 typed::{ComplexValType, Typed, TypedObj, CheckType},644 Val, State,645 error::{LocError, Error, Result},646 ObjValueBuilder, ObjValue,647 };648649 #typed650651 impl TypedObj for #ident {652 fn serialize(self, out: &mut ObjValueBuilder) -> Result<(), LocError> {653 #(#fields_serialize)*654655 Ok(())656 }657 fn parse(obj: &ObjValue) -> Result<Self, LocError> {658 Ok(Self {659 #(#fields_parse)*660 })661 }662 }663 };664 })665}1use 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, 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 name: Ident,73 _colon: Token![:],74 ty: Type,75}76impl Parse for Field {77 fn parse(input: ParseStream) -> syn::Result<Self> {78 Ok(Self {79 name: input.parse()?,80 _colon: input.parse()?,81 ty: input.parse()?,82 })83 }84}8586mod kw {87 syn::custom_keyword!(fields);88 syn::custom_keyword!(rename);89 syn::custom_keyword!(flatten);90 syn::custom_keyword!(ok);91}9293struct EmptyAttr;94impl Parse for EmptyAttr {95 fn parse(_input: ParseStream) -> Result<Self> {96 Ok(Self)97 }98}99100struct BuiltinAttrs {101 fields: Vec<Field>,102}103impl Parse for BuiltinAttrs {104 fn parse(input: ParseStream) -> syn::Result<Self> {105 if input.is_empty() {106 return Ok(Self { fields: Vec::new() });107 }108 input.parse::<kw::fields>()?;109 let fields;110 parenthesized!(fields in input);111 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;112 Ok(Self {113 fields: p.into_iter().collect(),114 })115 }116}117118enum ArgInfo {119 Normal {120 ty: Box<Type>,121 is_option: bool,122 name: Option<String>,123 cfg_attrs: Vec<Attribute>,124 },125 Lazy {126 is_option: bool,127 name: Option<String>,128 },129 Context,130 Location,131 This,132}133134impl ArgInfo {135 fn parse(name: &str, arg: &FnArg) -> Result<Self> {136 let FnArg::Typed(arg) = arg else {137 unreachable!()138 };139 let ident = match &arg.pat as &Pat {140 Pat::Ident(i) => Some(i.ident.clone()),141 _ => None,142 };143 let ty = &arg.ty;144 if type_is_path(ty, "Context").is_some() {145 return Ok(Self::Context);146 } else if type_is_path(ty, "CallLocation").is_some() {147 return Ok(Self::Location);148 } else if type_is_path(ty, "Thunk").is_some() {149 return Ok(Self::Lazy {150 is_option: false,151 name: ident.map(|v| v.to_string()),152 });153 }154155 match ty as &Type {156 Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),157 _ => {}158 }159160 let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {161 if type_is_path(ty, "Thunk").is_some() {162 return Ok(Self::Lazy {163 is_option: true,164 name: ident.map(|v| v.to_string()),165 });166 }167168 (true, Box::new(ty.clone()))169 } else {170 (false, ty.clone())171 };172173 let cfg_attrs = arg174 .attrs175 .iter()176 .filter(|a| a.path.is_ident("cfg"))177 .cloned()178 .collect();179180 Ok(Self::Normal {181 ty,182 is_option,183 name: ident.map(|v| v.to_string()),184 cfg_attrs,185 })186 }187}188189#[proc_macro_attribute]190pub fn builtin(191 attr: proc_macro::TokenStream,192 item: proc_macro::TokenStream,193) -> proc_macro::TokenStream {194 let attr = parse_macro_input!(attr as BuiltinAttrs);195 let item: ItemFn = parse_macro_input!(item);196197 match builtin_inner(attr, item) {198 Ok(v) => v.into(),199 Err(e) => e.into_compile_error().into(),200 }201}202203fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {204 let ReturnType::Type(_, result) = &fun.sig.output else {205 return Err(Error::new(206 fun.sig.span(),207 "builtin should return something",208 ))209 };210211 let Some(args) = type_is_path(result, "Result") else {212 return Err(Error::new(result.span(), "return value should be result"));213214 };215 let PathArguments::AngleBracketed(params) = args else {216 return Err(Error::new(args.span(), "missing result generic"));217 };218 let generic_arg = params.args.iter().next().unwrap();219 // This argument must be a type:220 let GenericArgument::Type(result_inner) = generic_arg else {221 return Err(Error::new(222 generic_arg.span(),223 "option generic should be a type",224 ))225 };226227 let name = fun.sig.ident.to_string();228 let args = fun229 .sig230 .inputs231 .iter()232 .map(|arg| ArgInfo::parse(&name, arg))233 .collect::<Result<Vec<_>>>()?;234235 let params_desc = args.iter().flat_map(|a| match a {236 ArgInfo::Normal {237 is_option,238 name,239 cfg_attrs,240 ..241 } => {242 let name = name243 .as_ref()244 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})245 .unwrap_or_else(|| quote! {None});246 Some(quote! {247 #(#cfg_attrs)*248 BuiltinParam {249 name: #name,250 has_default: #is_option,251 },252 })253 }254 ArgInfo::Lazy { is_option, name } => {255 let name = name256 .as_ref()257 .map(|n| quote! {Some(std::borrow::Cow::Borrowed(#n))})258 .unwrap_or_else(|| quote! {None});259 Some(quote! {260 BuiltinParam {261 name: #name,262 has_default: #is_option,263 },264 })265 }266 ArgInfo::Context => None,267 ArgInfo::Location => None,268 ArgInfo::This => None,269 });270271 let mut id = 0usize;272 let pass = args273 .iter()274 .map(|a| match a {275 ArgInfo::Normal { .. } | ArgInfo::Lazy { .. } => {276 let cid = id;277 id += 1;278 (quote! {#cid}, a)279 }280 ArgInfo::Context | ArgInfo::Location | ArgInfo::This => {281 (quote! {compile_error!("should not use id")}, a)282 }283 })284 .map(|(id, a)| match a {285 ArgInfo::Normal {286 ty,287 is_option,288 name,289 cfg_attrs,290 } => {291 let name = name.as_ref().map(|v| v.as_str()).unwrap_or("<unnamed>");292 let eval = quote! {jrsonnet_evaluator::State::push_description(293 || format!("argument <{}> evaluation", #name),294 || <#ty>::from_untyped(value.evaluate()?),295 )?};296 let value = if *is_option {297 quote! {if let Some(value) = &parsed[#id] {298 Some(#eval)299 } else {300 None301 },}302 } else {303 quote! {{304 let value = parsed[#id].as_ref().expect("args shape is checked");305 #eval306 },}307 };308 quote! {309 #(#cfg_attrs)*310 #value311 }312 }313 ArgInfo::Lazy { is_option, .. } => {314 if *is_option {315 quote! {if let Some(value) = &parsed[#id] {316 Some(value.clone())317 } else {318 None319 }}320 } else {321 quote! {322 parsed[#id].as_ref().expect("args shape is correct").clone(),323 }324 }325 }326 ArgInfo::Context => quote! {ctx.clone(),},327 ArgInfo::Location => quote! {location,},328 ArgInfo::This => quote! {self,},329 });330331 let fields = attr.fields.iter().map(|field| {332 let name = &field.name;333 let ty = &field.ty;334 quote! {335 pub #name: #ty,336 }337 });338339 let name = &fun.sig.ident;340 let vis = &fun.vis;341 let static_ext = if attr.fields.is_empty() {342 quote! {343 impl #name {344 pub const INST: &'static dyn StaticBuiltin = &#name {};345 }346 impl StaticBuiltin for #name {}347 }348 } else {349 quote! {}350 };351 let static_derive_copy = if attr.fields.is_empty() {352 quote! {, Copy}353 } else {354 quote! {}355 };356357 Ok(quote! {358 #fun359 #[doc(hidden)]360 #[allow(non_camel_case_types)]361 #[derive(Clone, jrsonnet_gcmodule::Trace #static_derive_copy)]362 #vis struct #name {363 #(#fields)*364 }365 const _: () = {366 use ::jrsonnet_evaluator::{367 State, Val,368 function::{builtin::{Builtin, StaticBuiltin, BuiltinParam}, CallLocation, ArgsLike, parse::parse_builtin_call},369 error::Result, Context, typed::Typed,370 parser::ExprLocation,371 };372 const PARAMS: &'static [BuiltinParam] = &[373 #(#params_desc)*374 ];375376 #static_ext377 impl Builtin for #name378 where379 Self: 'static380 {381 fn name(&self) -> &str {382 stringify!(#name)383 }384 fn params(&self) -> &[BuiltinParam] {385 PARAMS386 }387 fn call(&self, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {388 let parsed = parse_builtin_call(ctx.clone(), &PARAMS, args, false)?;389390 let result: #result = #name(#(#pass)*);391 let result = result?;392 <#result_inner>::into_untyped(result)393 }394 }395 };396 })397}398399#[derive(Default)]400struct TypedAttr {401 rename: Option<String>,402 flatten: bool,403 /// flatten(ok) strategy for flattened optionals404 /// field would be None in case of any parsing error (as in serde)405 flatten_ok: bool,406}407impl Parse for TypedAttr {408 fn parse(input: ParseStream) -> syn::Result<Self> {409 let mut out = Self::default();410 loop {411 let lookahead = input.lookahead1();412 if lookahead.peek(kw::rename) {413 input.parse::<kw::rename>()?;414 input.parse::<Token![=]>()?;415 let name = input.parse::<LitStr>()?;416 if out.rename.is_some() {417 return Err(Error::new(418 name.span(),419 "rename attribute may only be specified once",420 ));421 }422 out.rename = Some(name.value());423 } else if lookahead.peek(kw::flatten) {424 input.parse::<kw::flatten>()?;425 out.flatten = true;426 if input.peek(token::Paren) {427 let content;428 parenthesized!(content in input);429 let lookahead = content.lookahead1();430 if lookahead.peek(kw::ok) {431 content.parse::<kw::ok>()?;432 out.flatten_ok = true;433 } else {434 return Err(lookahead.error());435 }436 }437 } else if input.is_empty() {438 break;439 } else {440 return Err(lookahead.error());441 }442 if input.peek(Token![,]) {443 input.parse::<Token![,]>()?;444 } else {445 break;446 }447 }448 Ok(out)449 }450}451452struct TypedField {453 attr: TypedAttr,454 ident: Ident,455 ty: Type,456 is_option: bool,457}458impl TypedField {459 fn parse(field: &syn::Field) -> Result<Self> {460 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();461 let Some(ident) = field.ident.clone() else {462 return Err(Error::new(463 field.span(),464 "this field should appear in output object, but it has no visible name",465 ));466 };467 let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {468 (true, ty.clone())469 } else {470 (false, field.ty.clone())471 };472 if is_option && attr.flatten {473 if !attr.flatten_ok {474 return Err(Error::new(475 field.span(),476 "strategy should be set when flattening Option",477 ));478 }479 } else if attr.flatten_ok {480 return Err(Error::new(481 field.span(),482 "flatten(ok) is only useable on optional fields",483 ));484 }485486 Ok(Self {487 attr,488 ident,489 ty,490 is_option,491 })492 }493 /// None if this field is flattened in jsonnet output494 fn name(&self) -> Option<String> {495 if self.attr.flatten {496 return None;497 }498 Some(499 self.attr500 .rename501 .clone()502 .unwrap_or_else(|| self.ident.to_string()),503 )504 }505506 fn expand_field(&self) -> Option<TokenStream> {507 if self.is_option {508 return None;509 }510 let name = self.name()?;511 let ty = &self.ty;512 Some(quote! {513 (#name, <#ty>::TYPE)514 })515 }516 fn expand_parse(&self) -> TokenStream {517 let ident = &self.ident;518 let ty = &self.ty;519 if self.attr.flatten {520 // optional flatten is handled in same way as serde521 return if self.is_option {522 quote! {523 #ident: <#ty>::parse(&obj).ok(),524 }525 } else {526 quote! {527 #ident: <#ty>::parse(&obj)?,528 }529 };530 };531532 let name = self.name().unwrap();533 let value = if self.is_option {534 quote! {535 if let Some(value) = obj.get(#name.into())? {536 Some(<#ty>::from_untyped(value)?)537 } else {538 None539 }540 }541 } else {542 quote! {543 <#ty>::from_untyped(obj.get(#name.into())?.ok_or_else(|| Error::NoSuchField(#name.into(), vec![]))?)?544 }545 };546547 quote! {548 #ident: #value,549 }550 }551 fn expand_serialize(&self) -> Result<TokenStream> {552 let ident = &self.ident;553 let ty = &self.ty;554 Ok(if let Some(name) = self.name() {555 if self.is_option {556 quote! {557 if let Some(value) = self.#ident {558 out.member(#name.into()).value(<#ty>::into_untyped(value)?)?;559 }560 }561 } else {562 quote! {563 out.member(#name.into()).value(<#ty>::into_untyped(self.#ident)?)?;564 }565 }566 } else if self.is_option {567 quote! {568 if let Some(value) = self.#ident {569 value.serialize(out)?;570 }571 }572 } else {573 quote! {574 self.#ident.serialize(out)?;575 }576 })577 }578}579580#[proc_macro_derive(Typed, attributes(typed))]581pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {582 let input = parse_macro_input!(item as DeriveInput);583584 match derive_typed_inner(input) {585 Ok(v) => v.into(),586 Err(e) => e.to_compile_error().into(),587 }588}589590fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {591 let syn::Data::Struct(data) = &input.data else {592 return Err(Error::new(input.span(), "only structs supported"));593 };594595 let ident = &input.ident;596 let fields = data597 .fields598 .iter()599 .map(TypedField::parse)600 .collect::<Result<Vec<_>>>()?;601602 let typed = {603 let fields = fields604 .iter()605 .flat_map(TypedField::expand_field)606 .collect::<Vec<_>>();607 let len = fields.len();608 quote! {609 const ITEMS: [(&'static str, &'static ComplexValType); #len] = [610 #(#fields,)*611 ];612 impl Typed for #ident {613 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);614615 fn from_untyped(value: Val) -> Result<Self> {616 let obj = value.as_obj().expect("shape is correct");617 Self::parse(&obj)618 }619620 fn into_untyped(value: Self) -> Result<Val> {621 let mut out = ObjValueBuilder::new();622 value.serialize(&mut out)?;623 Ok(Val::Obj(out.build()))624 }625626 }627 }628 };629630 let fields_parse = fields.iter().map(TypedField::expand_parse);631 let fields_serialize = fields632 .iter()633 .map(TypedField::expand_serialize)634 .collect::<Result<Vec<_>>>()?;635636 Ok(quote! {637 const _: () = {638 use ::jrsonnet_evaluator::{639 typed::{ComplexValType, Typed, TypedObj, CheckType},640 Val, State,641 error::{LocError, Error, Result},642 ObjValueBuilder, ObjValue,643 };644645 #typed646647 impl TypedObj for #ident {648 fn serialize(self, out: &mut ObjValueBuilder) -> Result<(), LocError> {649 #(#fields_serialize)*650651 Ok(())652 }653 fn parse(obj: &ObjValue) -> Result<Self, LocError> {654 Ok(Self {655 #(#fields_parse)*656 })657 }658 }659 };660 })661}