1#![allow(unused_assignments)]
6
7use miette::Diagnostic;
8use std::path::Path;
9use thiserror::Error;
10
11#[derive(Error, Debug, Diagnostic)]
13pub enum Error {
14 #[error("I/O {operation} failed{}", path.as_ref().map_or(String::new(), |p| format!(": {}", p.display())))]
16 #[diagnostic(
17 code(cuenv::cache::io),
18 help("Check file permissions and ensure the path exists")
19 )]
20 Io {
21 #[source]
23 source: std::io::Error,
24 path: Option<Box<Path>>,
26 operation: String,
28 },
29
30 #[error("Cache configuration error: {message}")]
32 #[diagnostic(code(cuenv::cache::config))]
33 Configuration {
34 message: String,
36 },
37
38 #[error("Cache key not found: {key}")]
40 #[diagnostic(
41 code(cuenv::cache::not_found),
42 help("The cache entry may have been evicted or never existed")
43 )]
44 NotFound {
45 key: String,
47 },
48
49 #[error("Serialization error: {message}")]
51 #[diagnostic(code(cuenv::cache::serialization))]
52 Serialization {
53 message: String,
55 },
56}
57
58impl Error {
59 #[must_use]
61 pub fn configuration(msg: impl Into<String>) -> Self {
62 Self::Configuration {
63 message: msg.into(),
64 }
65 }
66
67 #[must_use]
69 pub fn io(
70 source: std::io::Error,
71 path: impl AsRef<Path>,
72 operation: impl Into<String>,
73 ) -> Self {
74 Self::Io {
75 source,
76 path: Some(path.as_ref().into()),
77 operation: operation.into(),
78 }
79 }
80
81 #[must_use]
83 pub fn io_no_path(source: std::io::Error, operation: impl Into<String>) -> Self {
84 Self::Io {
85 source,
86 path: None,
87 operation: operation.into(),
88 }
89 }
90
91 #[must_use]
93 pub fn not_found(key: impl Into<String>) -> Self {
94 Self::NotFound { key: key.into() }
95 }
96
97 #[must_use]
99 pub fn serialization(msg: impl Into<String>) -> Self {
100 Self::Serialization {
101 message: msg.into(),
102 }
103 }
104}
105
106pub type Result<T> = std::result::Result<T, Error>;