1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

//! This crate provides the basic types that we rely on for logs.

#![warn(missing_docs)]

use fidl_fuchsia_diagnostics as fdiagnostics;
use std::str::FromStr;
use std::{cmp, fmt};

#[cfg(feature = "serde")]
#[doc(hidden)]
pub mod serde_ext;

// LINT.IfChange

/// Severities a log message can have, often called the log's "level".
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[repr(u8)]
pub enum Severity {
    /// Trace severity level
    Trace = 0x10,
    /// Debug severity level
    Debug = 0x20,
    /// Info severity level
    Info = 0x30,
    /// Warn severity level
    Warn = 0x40,
    /// Error severity level
    Error = 0x50,
    /// Fatal severity level
    Fatal = 0x60,
}
// LINT.ThenChange(/src/lib/assembly/config_schema/src/platform_config/diagnostics_config.rs)

#[cfg(feature = "serde")]
impl serde::Serialize for Severity {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serde_ext::severity::serialize(self, serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Severity {
    fn deserialize<D>(deserializer: D) -> Result<Severity, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        serde_ext::severity::deserialize(deserializer)
    }
}

impl Severity {
    /// Returns a severity and also the raw severity if it's  not an exact match of a severity value.
    pub fn parse_exact(raw_severity: u8) -> (Option<u8>, Severity) {
        if raw_severity == Severity::Trace as u8 {
            (None, Severity::Trace)
        } else if raw_severity == Severity::Debug as u8 {
            (None, Severity::Debug)
        } else if raw_severity == Severity::Info as u8 {
            (None, Severity::Info)
        } else if raw_severity == Severity::Warn as u8 {
            (None, Severity::Warn)
        } else if raw_severity == Severity::Error as u8 {
            (None, Severity::Error)
        } else if raw_severity == Severity::Fatal as u8 {
            (None, Severity::Fatal)
        } else {
            (Some(raw_severity), Severity::from(raw_severity))
        }
    }

    /// Returns the string representation of a severity.
    pub fn as_str(&self) -> &'static str {
        match self {
            Severity::Trace => "TRACE",
            Severity::Debug => "DEBUG",
            Severity::Info => "INFO",
            Severity::Warn => "WARN",
            Severity::Error => "ERROR",
            Severity::Fatal => "FATAL",
        }
    }
}

macro_rules! impl_from_signed {
    ($($type:ty),*) => {
        $(
            impl From<$type> for Severity {
                fn from(value: $type) -> Severity {
                    match value {
                        ..0x00 => Severity::Trace,
                        0x00..=0x10 => Severity::Trace,
                        0x11..=0x20 => Severity::Debug,
                        0x21..=0x30 => Severity::Info,
                        0x31..=0x40 => Severity::Warn,
                        0x41..=0x50 => Severity::Error,
                        0x51.. => Severity::Fatal,
                    }
                }
            }
        )*
    }
}

macro_rules! impl_from_unsigned {
    ($($type:ty),*) => {
        $(
            impl From<$type> for Severity {
                fn from(value: $type) -> Severity {
                    match value {
                        0x00..=0x10 => Severity::Trace,
                        0x11..=0x20 => Severity::Debug,
                        0x21..=0x30 => Severity::Info,
                        0x31..=0x40 => Severity::Warn,
                        0x41..=0x50 => Severity::Error,
                        0x51.. => Severity::Fatal,
                    }
                }
            }
        )*
    }
}

impl_from_signed!(i8, i16, i32, i64, i128);
impl_from_unsigned!(u8, u16, u32, u64, u128);

impl From<Severity> for tracing::Level {
    fn from(s: Severity) -> tracing::Level {
        match s {
            Severity::Trace => tracing::Level::TRACE,
            Severity::Debug => tracing::Level::DEBUG,
            Severity::Info => tracing::Level::INFO,
            Severity::Warn => tracing::Level::WARN,
            Severity::Fatal | Severity::Error => tracing::Level::ERROR,
        }
    }
}

impl From<tracing::Level> for Severity {
    fn from(level: tracing::Level) -> Severity {
        match level {
            tracing::Level::TRACE => Severity::Trace,
            tracing::Level::DEBUG => Severity::Debug,
            tracing::Level::INFO => Severity::Info,
            tracing::Level::WARN => Severity::Warn,
            tracing::Level::ERROR => Severity::Error,
        }
    }
}

impl From<Severity> for fdiagnostics::Severity {
    fn from(s: Severity) -> fdiagnostics::Severity {
        match s {
            Severity::Trace => fdiagnostics::Severity::Trace,
            Severity::Debug => fdiagnostics::Severity::Debug,
            Severity::Info => fdiagnostics::Severity::Info,
            Severity::Warn => fdiagnostics::Severity::Warn,
            Severity::Error => fdiagnostics::Severity::Error,
            Severity::Fatal => fdiagnostics::Severity::Fatal,
        }
    }
}

impl From<fdiagnostics::Severity> for Severity {
    fn from(s: fdiagnostics::Severity) -> Severity {
        match s {
            fdiagnostics::Severity::Trace => Severity::Trace,
            fdiagnostics::Severity::Debug => Severity::Debug,
            fdiagnostics::Severity::Info => Severity::Info,
            fdiagnostics::Severity::Warn => Severity::Warn,
            fdiagnostics::Severity::Error => Severity::Error,
            fdiagnostics::Severity::Fatal => Severity::Fatal,
        }
    }
}

impl AsRef<str> for Severity {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl fmt::Display for Severity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Parsing error for severities.
#[derive(thiserror::Error, Debug)]
pub enum Error {
    /// Attempted to parse a string that didn't map to a valid severity.
    #[error("invalid severity: {0}")]
    Invalid(String),
}

impl FromStr for Severity {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.to_lowercase();
        match s.as_str() {
            "trace" => Ok(Severity::Trace),
            "debug" => Ok(Severity::Debug),
            "info" => Ok(Severity::Info),
            "warn" | "warning" => Ok(Severity::Warn),
            "error" => Ok(Severity::Error),
            "fatal" => Ok(Severity::Fatal),
            other => Err(Error::Invalid(other.to_string())),
        }
    }
}

impl PartialEq<fdiagnostics::Severity> for Severity {
    fn eq(&self, other: &fdiagnostics::Severity) -> bool {
        match (self, other) {
            (Severity::Trace, fdiagnostics::Severity::Trace)
            | (Severity::Debug, fdiagnostics::Severity::Debug)
            | (Severity::Info, fdiagnostics::Severity::Info)
            | (Severity::Warn, fdiagnostics::Severity::Warn)
            | (Severity::Error, fdiagnostics::Severity::Error)
            | (Severity::Fatal, fdiagnostics::Severity::Fatal) => true,
            _ => false,
        }
    }
}

impl PartialOrd<fdiagnostics::Severity> for Severity {
    fn partial_cmp(&self, other: &fdiagnostics::Severity) -> Option<cmp::Ordering> {
        let other = Severity::from(*other);
        self.partial_cmp(&other)
    }
}