defmt_elf2table/
symbol.rs

1use defmt_decoder::Tag;
2use serde::Deserialize;
3
4#[derive(Deserialize, PartialEq, Eq, Hash)]
5pub struct Symbol {
6    /// Name of the Cargo package in which the symbol is being instantiated. Used for avoiding
7    /// symbol name collisions.
8    package: String,
9
10    /// Unique identifier that disambiguates otherwise equivalent invocations in the same crate.
11    disambiguator: String,
12
13    /// Symbol categorization. Known values:
14    /// * `defmt_prim` for primitive formatting strings that are placed at the start of the `.defmt`
15    ///   section.
16    /// * `defmt_fmt`, `defmt_str` for interned format strings and string literals.
17    /// * `defmt_trace`, `defmt_debug`, `defmt_info`, `defmt_warn`, `defmt_error` for logging
18    ///   messages used at the different log levels.
19    /// * Anything starting with `defmt_` is reserved for use by defmt, other prefixes are free for
20    ///   use by third-party apps (but they all should use a prefix!).
21    tag: String,
22
23    /// Symbol data for use by the host tooling. Interpretation depends on `tag`.
24    data: String,
25}
26
27pub enum SymbolTag<'a> {
28    /// `defmt_*` tag that we can interpret.
29    Defmt(Tag),
30
31    /// Non-`defmt_*` tag for custom tooling.
32    Custom(&'a str),
33}
34
35impl Symbol {
36    pub fn demangle(raw: &str) -> Result<Self, serde_json::Error> {
37        serde_json::from_str(raw)
38    }
39
40    pub fn tag(&self) -> SymbolTag<'_> {
41        match &*self.tag {
42            "defmt_prim" | "defmt_fmt" => SymbolTag::Defmt(Tag::Fmt),
43            "defmt_str" => SymbolTag::Defmt(Tag::Str),
44            "defmt_trace" => SymbolTag::Defmt(Tag::Trace),
45            "defmt_debug" => SymbolTag::Defmt(Tag::Debug),
46            "defmt_info" => SymbolTag::Defmt(Tag::Info),
47            "defmt_warn" => SymbolTag::Defmt(Tag::Warn),
48            "defmt_error" => SymbolTag::Defmt(Tag::Error),
49            _ => SymbolTag::Custom(&self.tag),
50        }
51    }
52
53    pub fn data(&self) -> &str {
54        &self.data
55    }
56}