Skip to main content

cc_audit/error/
context.rs

1//! Error context types for better error messages.
2
3/// I/O operation types.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum IoOperation {
6    Read,
7    Write,
8    Create,
9    Delete,
10    SetPermissions,
11}
12
13impl std::fmt::Display for IoOperation {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            Self::Read => write!(f, "read"),
17            Self::Write => write!(f, "write"),
18            Self::Create => write!(f, "create"),
19            Self::Delete => write!(f, "delete"),
20            Self::SetPermissions => write!(f, "set permissions"),
21        }
22    }
23}
24
25/// Parse format types.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum ParseFormat {
28    Json,
29    Yaml,
30    Toml,
31    Frontmatter,
32}
33
34impl std::fmt::Display for ParseFormat {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        match self {
37            Self::Json => write!(f, "JSON"),
38            Self::Yaml => write!(f, "YAML"),
39            Self::Toml => write!(f, "TOML"),
40            Self::Frontmatter => write!(f, "frontmatter"),
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_io_operation_display() {
51        assert_eq!(IoOperation::Read.to_string(), "read");
52        assert_eq!(IoOperation::Write.to_string(), "write");
53        assert_eq!(IoOperation::Create.to_string(), "create");
54        assert_eq!(IoOperation::Delete.to_string(), "delete");
55        assert_eq!(IoOperation::SetPermissions.to_string(), "set permissions");
56    }
57
58    #[test]
59    fn test_parse_format_display() {
60        assert_eq!(ParseFormat::Json.to_string(), "JSON");
61        assert_eq!(ParseFormat::Yaml.to_string(), "YAML");
62        assert_eq!(ParseFormat::Toml.to_string(), "TOML");
63        assert_eq!(ParseFormat::Frontmatter.to_string(), "frontmatter");
64    }
65
66    #[test]
67    fn test_io_operation_debug() {
68        let op = IoOperation::Read;
69        let debug_str = format!("{:?}", op);
70        assert!(debug_str.contains("Read"));
71    }
72
73    #[test]
74    fn test_io_operation_clone() {
75        let op = IoOperation::Write;
76        let cloned = op;
77        assert_eq!(cloned, IoOperation::Write);
78    }
79
80    #[test]
81    fn test_io_operation_copy() {
82        let op = IoOperation::Create;
83        let copied = op;
84        assert_eq!(copied, IoOperation::Create);
85    }
86
87    #[test]
88    fn test_parse_format_debug() {
89        let fmt = ParseFormat::Json;
90        let debug_str = format!("{:?}", fmt);
91        assert!(debug_str.contains("Json"));
92    }
93
94    #[test]
95    fn test_parse_format_clone() {
96        let fmt = ParseFormat::Yaml;
97        let cloned = fmt;
98        assert_eq!(cloned, ParseFormat::Yaml);
99    }
100
101    #[test]
102    fn test_parse_format_copy() {
103        let fmt = ParseFormat::Toml;
104        let copied = fmt;
105        assert_eq!(copied, ParseFormat::Toml);
106    }
107
108    #[test]
109    fn test_parse_format_equality() {
110        assert_eq!(ParseFormat::Json, ParseFormat::Json);
111        assert_ne!(ParseFormat::Json, ParseFormat::Yaml);
112    }
113
114    #[test]
115    fn test_io_operation_equality() {
116        assert_eq!(IoOperation::Read, IoOperation::Read);
117        assert_ne!(IoOperation::Read, IoOperation::Write);
118    }
119}