flareapp-core 0.1.0

The core of the Flare client: config, the client, and the worker thread that sends data.
Documentation
//! The levels Flare knows for messages and glows, and how OTLP severities map to
//! them.

use std::fmt;

use flareapp_otlp::Severity;

/// How serious a message or glow is. These are the levels Flare knows, the same as
/// the PSR-3 log levels in PHP.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Level {
    /// Detailed information for debugging.
    Debug,
    /// Normal events, like a user logging in.
    Info,
    /// Normal but noteworthy events.
    Notice,
    /// Something unexpected that the application can handle.
    Warning,
    /// An error that made an operation fail.
    Error,
    /// A critical problem, like a part of the application that is down.
    Critical,
    /// Someone must act right away.
    Alert,
    /// The application is unusable.
    Emergency,
}

impl Level {
    /// Returns the name Flare uses, like `warning`.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Debug => "debug",
            Self::Info => "info",
            Self::Notice => "notice",
            Self::Warning => "warning",
            Self::Error => "error",
            Self::Critical => "critical",
            Self::Alert => "alert",
            Self::Emergency => "emergency",
        }
    }
}

impl From<Severity> for Level {
    /// Maps an OTLP severity to the closest Flare level. Flare has no trace level,
    /// so `Trace` becomes `Debug`, and `Fatal` becomes `Emergency`.
    fn from(severity: Severity) -> Self {
        match severity {
            Severity::Trace | Severity::Debug => Self::Debug,
            Severity::Info => Self::Info,
            Severity::Warn => Self::Warning,
            Severity::Error => Self::Error,
            Severity::Fatal => Self::Emergency,
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn severities_map_to_levels() {
        let levels = [
            Severity::Trace,
            Severity::Debug,
            Severity::Info,
            Severity::Warn,
            Severity::Error,
            Severity::Fatal,
        ]
        .map(Level::from);

        assert_eq!(
            levels,
            [
                Level::Debug,
                Level::Debug,
                Level::Info,
                Level::Warning,
                Level::Error,
                Level::Emergency,
            ]
        );
    }
}