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    pub fn color(&self) -> &str {
31        match self {
32            ErrorSeverity::Info => "cyan",
33            ErrorSeverity::Warning => "yellow",
34            ErrorSeverity::Error => "red",
35            ErrorSeverity::Security => "magenta",
36        }
37    }
38}
39
40/// An error entry in the error log
41#[derive(Debug, Clone)]
42pub struct ErrorEntry {
43    pub timestamp: Instant,
44    pub severity: ErrorSeverity,
45    pub message: String,
46    pub context: Option<String>,
47}
48
49impl ErrorEntry {
50    pub fn new(severity: ErrorSeverity, message: String) -> Self {
51        Self {
52            timestamp: Instant::now(),
53            severity,
54            message,
55            context: None,
56        }
57    }
58
59    pub fn with_context(severity: ErrorSeverity, message: String, context: String) -> Self {
60        Self {
61            timestamp: Instant::now(),
62            severity,
63            message,
64            context: Some(context),
65        }
66    }
67
68    pub fn display(&self) -> String {
69        match &self.context {
70            Some(ctx) => format!(
71                "[{}] {} - {}",
72                self.severity.display(),
73                self.message,
74                ctx
75            ),
76            None => format!("[{}] {}", self.severity.display(), self.message),
77        }
78    }
79}