|
| 1 | +use proc_macro2::TokenStream; |
| 2 | +use quote::quote; |
| 3 | +use serde::Deserialize; |
| 4 | + |
| 5 | +use crate::template::{TemplatePart, parse_template_into_parts}; |
| 6 | + |
| 7 | +#[derive(Debug, Clone, Deserialize)] |
| 8 | +pub struct Operation { |
| 9 | + pub ops: Vec<String>, |
| 10 | + pub templates: Vec<String>, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Debug, Clone, Deserialize)] |
| 14 | +pub struct Description { |
| 15 | + pub operations: Vec<Operation>, |
| 16 | +} |
| 17 | + |
| 18 | +/// Returns a `TokenStream` representing Rust code defining a `description_for_op_and_attributes` method. |
| 19 | +/// This method constructs a description for the given op and attributes based on the span description |
| 20 | +/// conventions defined in the `sentry-conventions` repository. |
| 21 | +/// |
| 22 | +/// Some clippy lints are explicitly allowed, when fixing them would complicate the codegen. |
| 23 | +pub fn description_file_output(descriptions: impl Iterator<Item = Description>) -> TokenStream { |
| 24 | + let operations = descriptions.flat_map(|description| description.operations); |
| 25 | + |
| 26 | + let match_arms = operations.map(|Operation { ops, templates }| { |
| 27 | + let (templates_with_attributes, literal_template) = match templates.split_last() { |
| 28 | + Some((last, init)) if !last.contains("{{")=> (init, Some(last.as_str())), |
| 29 | + _ => (&templates[..], None), |
| 30 | + }; |
| 31 | + |
| 32 | + let conditional_attribute_blocks = templates_with_attributes.iter().map(|template| { |
| 33 | + let parts = parse_template_into_parts(template); |
| 34 | + if !parts.iter().any(|part| matches!(part, TemplatePart::Attribute(_, _))) { |
| 35 | + panic!("templates before the final template must contain attributes (bad template: {})", template); |
| 36 | + } |
| 37 | + |
| 38 | + // First, each attribute becomes a let clause for our if block. |
| 39 | + let if_clauses = parts.iter().flat_map(|part| { |
| 40 | + if let TemplatePart::Attribute(name, ident) = part { |
| 41 | + Some(quote! { |
| 42 | + let Some(#ident @ (Val::String(_) | Val::Bool(_) | Val::U64(_) | Val::I64(_) | Val::F64(_))) = attributes.get_value(#name) |
| 43 | + }) |
| 44 | + } else { |
| 45 | + None |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + // Then, construct the format string and argument list for a `format!` call to produce the name. |
| 50 | + let format_string = parts.iter().map(|part| match part { |
| 51 | + TemplatePart::Literal(l) => *l, |
| 52 | + TemplatePart::Attribute(_, _) => "{}", |
| 53 | + }).collect::<String>(); |
| 54 | + let format_args = parts.iter().flat_map(|part| { |
| 55 | + if let TemplatePart::Attribute(_, ident) = part { |
| 56 | + Some(quote! { DisplayVal(#ident) }) |
| 57 | + } else { |
| 58 | + None |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + Some(quote! { |
| 63 | + if #(#if_clauses)&&* { |
| 64 | + return Some(format!(#format_string, #(#format_args),*)); |
| 65 | + }; |
| 66 | + }) |
| 67 | + }); |
| 68 | + |
| 69 | + let literal_name_fallback = match literal_template { |
| 70 | + Some(literal_template) => quote! { Some(#literal_template.to_owned()) }, |
| 71 | + None => quote! { None }, |
| 72 | + }; |
| 73 | + |
| 74 | + // Assemble the match arm, with `ops` forming the match clause and the match body checking |
| 75 | + // each template in turn before falling back to a literal (zero-attribute) template. |
| 76 | + quote! { |
| 77 | + #(#ops)|* => { |
| 78 | + #(#conditional_attribute_blocks)* |
| 79 | + #literal_name_fallback |
| 80 | + } |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + quote! { |
| 85 | + use relay_protocol::{Getter, Val}; |
| 86 | + use std::fmt; |
| 87 | + use std::fmt::Display; |
| 88 | + |
| 89 | + pub fn description_for_op_and_attributes(op: &str, attributes: &impl Getter) -> Option<String> { |
| 90 | + match op { |
| 91 | + #(#match_arms)* |
| 92 | + _ => None |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + struct DisplayVal<'a>(Val<'a>); |
| 97 | + |
| 98 | + impl Display for DisplayVal<'_> { |
| 99 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 100 | + match self.0 { |
| 101 | + Val::Bool(b) => write!(f, "{b}"), |
| 102 | + Val::I64(i) => write!(f, "{i}"), |
| 103 | + Val::U64(u) => write!(f, "{u}"), |
| 104 | + Val::F64(fl) => write!(f, "{fl}"), |
| 105 | + Val::String(s) => f.write_str(s), |
| 106 | + Val::HexId(_) | Val::Array(_) | Val::Object(_) => Ok(()), |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments