Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
some nicer event macros
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Feb 7, 2024
1 parent 4c49dae commit 4016121
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 157 deletions.
24 changes: 22 additions & 2 deletions core/src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
clock::Clock, ctxt::Ctxt, emitter::Emitter, empty::Empty, event::Event, filter::Filter,
props::Props, rng::Rng, timestamp::Timestamp,
clock::Clock, ctxt::Ctxt, emitter::Emitter, empty::Empty, event::Event, extent::ToExtent,
filter::Filter, props::Props, rng::Rng, timestamp::Timestamp,
};

static SHARED: AmbientSlot = AmbientSlot::new();
Expand Down Expand Up @@ -172,6 +172,26 @@ impl<TEmitter, TFilter, TCtxt, TClock, TRng> Runtime<TEmitter, TFilter, TCtxt, T
}
}

impl<TEmitter: Emitter, TFilter: Filter, TCtxt: Ctxt, TClock: Clock, TRng: Rng>
Runtime<TEmitter, TFilter, TCtxt, TClock, TRng>
{
pub fn emit<P: Props>(&self, evt: &Event<P>) {
self.ctxt.with_current(|ctxt| {
let evt = Event::new(
evt.extent()
.cloned()
.or_else(|| self.clock.now().to_extent()),
evt.tpl(),
ctxt.chain(evt.props()),
);

if self.filter.matches(&evt) {
self.emitter.emit(&evt);
}
});
}
}

impl<TEmitter: Emitter, TFilter, TCtxt, TClock, TRng> Emitter
for Runtime<TEmitter, TFilter, TCtxt, TClock, TRng>
{
Expand Down
1 change: 1 addition & 0 deletions core/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const DAYS_PER_100Y: i32 = 365 * 100 + 24;
const DAYS_PER_4Y: i32 = 365 * 4 + 1;
const DAYS_IN_MONTH: [u8; 12] = [31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29];

// TODO: This needs an optional monotonic instant
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timestamp(Duration);

Expand Down
29 changes: 3 additions & 26 deletions macros/src/emit.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use proc_macro2::{Span, TokenStream};
use syn::{parse::Parse, FieldValue, Ident};
use proc_macro2::TokenStream;
use syn::{parse::Parse, FieldValue};

use crate::{
args::{self, Arg},
props::Props,
event::push_event_props,
template,
};

Expand Down Expand Up @@ -78,26 +78,3 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
}
}))
}

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(())
}
71 changes: 71 additions & 0 deletions macros/src/event.rs
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(())
}
Loading

0 comments on commit 4016121

Please sign in to comment.