🛈 Note: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem.

For the release documentation, please see docs.rs, instead.

tracing_mock/
metadata.rs

1use std::fmt;
2
3use tracing_core::Metadata;
4
5#[derive(Clone, Debug, Eq, PartialEq, Default)]
6pub(crate) struct ExpectedMetadata {
7    pub(crate) name: Option<String>,
8    pub(crate) level: Option<tracing::Level>,
9    pub(crate) target: Option<String>,
10}
11
12impl ExpectedMetadata {
13    /// Checks the given metadata against this expected metadata and panics if
14    /// there is a mismatch.
15    ///
16    /// The context `ctx` should fit into the followint sentence:
17    ///
18    /// > expected {ctx} named `expected_name`, but got one named `actual_name`
19    ///
20    /// Examples could be:
21    /// * a new span
22    /// * to enter a span
23    /// * an event
24    ///
25    /// # Panics
26    ///
27    /// This method will panic if any of the expectations that have been
28    /// specified are noto met.
29    ///
30    pub(crate) fn check(
31        &self,
32        actual: &Metadata<'_>,
33        ctx: impl fmt::Display,
34        collector_name: &str,
35    ) {
36        if let Some(ref expected_name) = self.name {
37            let actual_name = actual.name();
38            assert!(
39                expected_name == actual_name,
40                "{}",
41                format_args!(
42                    "\n[{collector_name}] expected {ctx} named `{expected_name}`,\n\
43                    [{collector_name}] but got one named `{actual_name}` instead."
44                ),
45            )
46        }
47
48        if let Some(ref expected_level) = self.level {
49            let actual_level = actual.level();
50            assert!(
51                expected_level == actual_level,
52                "{}",
53                format_args!(
54                    "\n[{collector_name}] expected {ctx} at level `{expected_level:?}`,\n\
55                    [{collector_name}] but got one at level `{actual_level:?}` instead."
56                ),
57            )
58        }
59
60        if let Some(ref expected_target) = self.target {
61            let actual_target = actual.target();
62            assert!(
63                expected_target == actual_target,
64                "{}",
65                format_args!(
66                    "\n[{collector_name}] expected {ctx} with target `{expected_target}`,\n\
67                    [{collector_name}] but got one with target `{actual_target}` instead."
68                ),
69            )
70        }
71    }
72
73    pub(crate) fn has_expectations(&self) -> bool {
74        self.name.is_some() || self.level.is_some() || self.target.is_some()
75    }
76}
77
78impl fmt::Display for ExpectedMetadata {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        if let Some(ref name) = self.name {
81            write!(f, " named `{}`", name)?;
82        }
83
84        if let Some(ref level) = self.level {
85            write!(f, " at the `{:?}` level", level)?;
86        }
87
88        if let Some(ref target) = self.target {
89            write!(f, " with target `{}`", target)?;
90        }
91
92        Ok(())
93    }
94}