Skip to main content

mermaid_cli/tui/state/
error.rs

1//! Error types for the UI layer
2//!
3//! Structured error logging with severity levels.
4
5use std::time::Instant;
6
7/// Error severity level
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum ErrorSeverity {
10    /// Informational (not really an error)
11    Info,
12    /// Warning - operation completed but with issues
13    Warning,
14    /// Error - operation failed
15    Error,
16    /// Security error - action was rejected
17    Security,
18}
19
20impl ErrorSeverity {
21    pub fn display(&self) -> &str {
22        match self {
23            ErrorSeverity::Info => "INFO",
24            ErrorSeverity::Warning => "WARN",
25            ErrorSeverity::Error => "ERROR",
26            ErrorSeverity::Security => "SECURITY",
27        }
28    }
29}
30
31/// An error entry in the error log
32#[derive(Debug, Clone)]
33pub struct ErrorEntry {
34    pub timestamp: Instant,
35    pub severity: ErrorSeverity,
36    pub message: String,
37    pub context: Option<String>,
38}
39
40impl ErrorEntry {
41    pub fn new(severity: ErrorSeverity, message: String) -> Self {
42        Self {
43            timestamp: Instant::now(),
44            severity,
45            message,
46            context: None,
47        }
48    }
49
50    pub fn with_context(severity: ErrorSeverity, message: String, context: String) -> Self {
51        Self {
52            timestamp: Instant::now(),
53            severity,
54            message,
55            context: Some(context),
56        }
57    }
58
59    pub fn display(&self) -> String {
60        match &self.context {
61            Some(ctx) => format!(
62                "[{}] {} - {}",
63                self.severity.display(),
64                self.message,
65                ctx
66            ),
67            None => format!("[{}] {}", self.severity.display(), self.message),
68        }
69    }
70}