Skip to content

Commit

Permalink
add a template to default completion
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jan 8, 2025
1 parent 2d9cd39 commit 8caadf1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,8 @@ pub fn __private_complete_span<
lvl: Option<&'b (impl CaptureLevel + ?Sized)>,
panic_lvl: Option<&'b (impl CaptureLevel + ?Sized)>,
) {
let mut completion = span::completion::Default::new(rt.emitter(), rt.ctxt());
let mut completion =
span::completion::Default::new(rt.emitter(), rt.ctxt()).with_tpl(tpl.tpl_control_param());

if let Some(lvl) = lvl.and_then(|lvl| lvl.capture()) {
completion = completion.with_lvl(lvl);
Expand All @@ -881,7 +882,7 @@ pub fn __private_complete_span<
completion = completion.with_panic_lvl(lvl);
}

completion.complete(span.with_tpl(tpl.tpl_control_param()));
completion.complete(span);
}

#[track_caller]
Expand Down
15 changes: 15 additions & 0 deletions src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ impl<'a, P> Metric<'a, P> {
props,
}
}

/**
Map the properties of the metric.
*/
pub fn map_props<U>(self, map: impl FnOnce(P) -> U) -> Metric<'a, U> {
Metric {
mdl: self.mdl,
extent: self.extent,
tpl: self.tpl,
name: self.name,
agg: self.agg,
value: self.value,
props: map(self.props),
}
}
}

impl<'a, P: Props> ToEvent for Metric<'a, P> {
Expand Down
75 changes: 63 additions & 12 deletions src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,19 @@ impl<'a, P: Props> Span<'a, P> {
}
}

/**
Map the properties of the span.
*/
pub fn map_props<U>(self, map: impl FnOnce(P) -> U) -> Span<'a, U> {
Span {
mdl: self.mdl,
extent: self.extent,
name: self.name,
tpl: self.tpl,
props: map(self.props),
}
}

/**
Get the template that will be used to render the span.
*/
Expand Down Expand Up @@ -971,6 +984,7 @@ pub mod completion {
empty::Empty,
event::ToEvent,
props::{ErasedProps, Props},
template::Template,
value::{ToValue, Value},
well_known::{KEY_ERR, KEY_LVL},
};
Expand Down Expand Up @@ -1009,14 +1023,15 @@ pub mod completion {
This type can be created directly, or via [`default`].
*/
pub struct Default<E, C, L = Level> {
pub struct Default<'a, E, C, L = Level> {
emitter: E,
ctxt: C,
tpl: Option<Template<'a>>,
lvl: Option<L>,
panic_lvl: Option<L>,
}

impl<E: Emitter, C: Ctxt, L: ToValue> Completion for Default<E, C, L> {
impl<'a, E: Emitter, C: Ctxt, L: ToValue> Completion for Default<'a, E, C, L> {
fn complete<P: Props>(&self, span: Span<P>) {
struct PanicError;

Expand Down Expand Up @@ -1078,25 +1093,26 @@ pub mod completion {
]
};

emit_core::emit(
&self.emitter,
Empty,
&self.ctxt,
Empty,
span.to_event()
.map_props(|span_props| completion_props.and_props(span_props)),
);
let tpl = self.tpl.as_ref().unwrap_or_else(|| span.tpl()).by_ref();

let evt = span
.to_event()
.with_tpl(tpl)
.map_props(|span_props| completion_props.and_props(span_props));

emit_core::emit(&self.emitter, Empty, &self.ctxt, Empty, evt);
}
}

impl<E, C, L> Default<E, C, L> {
impl<'a, E, C, L> Default<'a, E, C, L> {
/**
Wrap the given emitter and context.
*/
pub const fn new(emitter: E, ctxt: C) -> Self {
Default {
emitter,
ctxt,
tpl: None,
lvl: None,
panic_lvl: None,
}
Expand All @@ -1111,6 +1127,7 @@ pub mod completion {
Default {
emitter: self.emitter,
ctxt: self.ctxt,
tpl: self.tpl,
lvl: Some(lvl),
panic_lvl: self.panic_lvl,
}
Expand All @@ -1123,10 +1140,24 @@ pub mod completion {
Default {
emitter: self.emitter,
ctxt: self.ctxt,
tpl: self.tpl,
lvl: self.lvl,
panic_lvl: Some(lvl),
}
}

/**
A template to use for the span on completion.
*/
pub fn with_tpl<'b>(self, tpl: impl Into<Template<'b>>) -> Default<'b, E, C, L> {
Default {
emitter: self.emitter,
ctxt: self.ctxt,
tpl: Some(tpl.into()),
lvl: self.lvl,
panic_lvl: self.panic_lvl,
}
}
}

/**
Expand All @@ -1136,7 +1167,7 @@ pub mod completion {
If the completion is called during a panic, it will attach an error to the span.
*/
pub const fn default<E: Emitter, C: Ctxt>(emitter: E, ctxt: C) -> Default<E, C> {
pub const fn default<'a, E: Emitter, C: Ctxt>(emitter: E, ctxt: C) -> Default<'a, E, C> {
Default::new(emitter, ctxt)
}

Expand Down Expand Up @@ -1321,6 +1352,26 @@ pub mod completion {
assert!(called.get());
}

#[test]
fn default_completion_uses_tpl() {
let called = Cell::new(false);

let completion = default(
emitter::from_fn(|evt| {
assert_eq!("test template", evt.msg().to_string());

called.set(true);
}),
Empty,
)
.with_lvl(Level::Info)
.with_tpl(Template::literal("test template"));

completion.complete(Span::new(Path::new_raw("test"), "test", Empty, Empty));

assert!(called.get());
}

#[cfg(feature = "std")]
struct Guard<T: Completion>(T);

Expand Down

0 comments on commit 8caadf1

Please sign in to comment.