This repository has been archived by the owner on Jun 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
332 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use proc_macro2::{Span, TokenStream}; | ||
use syn::{parse::Parse, FieldValue, Ident}; | ||
|
||
use crate::{ | ||
args::{self, Arg}, | ||
props::Props, | ||
template, | ||
}; | ||
|
||
pub struct ExpandTokens { | ||
pub level: Option<TokenStream>, | ||
pub input: TokenStream, | ||
} | ||
|
||
struct Args { | ||
extent: TokenStream, | ||
} | ||
|
||
impl Parse for Args { | ||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { | ||
let mut extent = Arg::token_stream("extent", |fv| { | ||
let expr = &fv.expr; | ||
|
||
Ok(quote!(#expr)) | ||
}); | ||
|
||
args::set_from_field_values( | ||
input.parse_terminated(FieldValue::parse, Token![,])?.iter(), | ||
[&mut extent], | ||
)?; | ||
|
||
Ok(Args { | ||
extent: extent.take().unwrap_or_else(|| quote!(emit::empty::Empty)), | ||
}) | ||
} | ||
} | ||
|
||
pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> { | ||
let (args, template, mut props) = template::parse2::<Args>(opts.input, true)?; | ||
|
||
push_event_props(&mut props, opts.level)?; | ||
|
||
let extent_tokens = args.extent; | ||
let template_tokens = template.template_tokens(); | ||
let props_tokens = props.props_tokens(); | ||
|
||
Ok(quote!(emit::Event::new(#extent_tokens, #template_tokens, #props_tokens))) | ||
} | ||
|
||
pub fn push_event_props(props: &mut Props, level: Option<TokenStream>) -> Result<(), syn::Error> { | ||
// Add the level as a property | ||
if let Some(level_value) = level { | ||
let level_ident = Ident::new(emit_core::well_known::LVL_KEY, Span::call_site()); | ||
|
||
props.push( | ||
&syn::parse2::<FieldValue>(quote!(#level_ident: emit::Level::#level_value))?, | ||
false, | ||
true, | ||
)?; | ||
} | ||
|
||
// Add the location as a property | ||
let loc_ident = Ident::new(emit_core::well_known::MODULE_KEY, Span::call_site()); | ||
props.push( | ||
&syn::parse2::<FieldValue>(quote!(#loc_ident: emit::__private::__private_module!()))?, | ||
false, | ||
true, | ||
)?; | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.