oxigdal_core/error/
builder.rs1use super::types::OxiGdalError;
4
5#[cfg(not(feature = "std"))]
6use alloc::collections::BTreeMap as HashMap;
7#[cfg(not(feature = "std"))]
8use alloc::string::String;
9#[cfg(not(feature = "std"))]
10use alloc::vec::Vec;
11
12#[cfg(feature = "std")]
13use std::collections::HashMap;
14#[cfg(feature = "std")]
15use std::path::{Path, PathBuf};
16
17#[derive(Debug, Clone)]
19pub struct ErrorContext {
20 pub category: &'static str,
22 pub details: Vec<(String, String)>,
24 #[cfg(feature = "std")]
26 pub file_path: Option<PathBuf>,
27 pub operation: Option<String>,
29 pub parameters: HashMap<String, String>,
31 pub custom_suggestion: Option<String>,
33}
34
35impl ErrorContext {
36 pub fn new(category: &'static str) -> Self {
38 Self {
39 category,
40 details: Vec::new(),
41 #[cfg(feature = "std")]
42 file_path: None,
43 operation: None,
44 parameters: HashMap::new(),
45 custom_suggestion: None,
46 }
47 }
48
49 pub fn with_detail(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
51 self.details.push((key.into(), value.into()));
52 self
53 }
54
55 #[cfg(feature = "std")]
57 pub fn with_path(mut self, path: PathBuf) -> Self {
58 self.file_path = Some(path);
59 self
60 }
61
62 pub fn with_operation(mut self, operation: String) -> Self {
64 self.operation = Some(operation);
65 self
66 }
67
68 pub fn with_parameter(mut self, key: String, value: String) -> Self {
70 self.parameters.insert(key, value);
71 self
72 }
73
74 pub fn with_custom_suggestion(mut self, suggestion: String) -> Self {
76 self.custom_suggestion = Some(suggestion);
77 self
78 }
79}
80
81#[derive(Debug)]
98pub struct ErrorBuilder {
99 error: OxiGdalError,
100 #[cfg(feature = "std")]
101 path: Option<PathBuf>,
102 operation: Option<String>,
103 parameters: HashMap<String, String>,
104 custom_suggestion: Option<String>,
105}
106
107impl ErrorBuilder {
108 pub fn new(error: OxiGdalError) -> Self {
110 Self {
111 error,
112 #[cfg(feature = "std")]
113 path: None,
114 operation: None,
115 parameters: HashMap::new(),
116 custom_suggestion: None,
117 }
118 }
119
120 #[cfg(feature = "std")]
122 pub fn with_path(mut self, path: impl AsRef<Path>) -> Self {
123 self.path = Some(path.as_ref().to_path_buf());
124 self
125 }
126
127 pub fn with_operation(mut self, operation: impl Into<String>) -> Self {
129 self.operation = Some(operation.into());
130 self
131 }
132
133 pub fn with_parameter(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
135 self.parameters.insert(key.into(), value.into());
136 self
137 }
138
139 pub fn with_suggestion(mut self, suggestion: impl Into<String>) -> Self {
141 self.custom_suggestion = Some(suggestion.into());
142 self
143 }
144
145 pub fn build(self) -> OxiGdalError {
150 self.error
154 }
155
156 pub fn into_error(self) -> OxiGdalError {
158 self.error
159 }
160
161 pub fn error(&self) -> &OxiGdalError {
163 &self.error
164 }
165
166 #[cfg(feature = "std")]
168 pub fn file_path(&self) -> Option<&Path> {
169 self.path.as_deref()
170 }
171
172 pub fn operation_name(&self) -> Option<&str> {
174 self.operation.as_deref()
175 }
176
177 pub fn parameters(&self) -> &HashMap<String, String> {
179 &self.parameters
180 }
181
182 pub fn custom_suggestion(&self) -> Option<&str> {
184 self.custom_suggestion.as_deref()
185 }
186
187 pub fn suggestion(&self) -> Option<String> {
189 if let Some(ref custom) = self.custom_suggestion {
190 Some(custom.clone())
191 } else {
192 self.error.suggestion().map(|s| s.to_string())
193 }
194 }
195
196 pub fn build_context(&self) -> ErrorContext {
198 let mut ctx = self.error.context();
199
200 #[cfg(feature = "std")]
201 if let Some(ref path) = self.path {
202 ctx = ctx.with_path(path.clone());
203 }
204
205 if let Some(ref op) = self.operation {
206 ctx = ctx.with_operation(op.clone());
207 }
208
209 for (key, value) in &self.parameters {
210 ctx = ctx.with_parameter(key.clone(), value.clone());
211 }
212
213 if let Some(ref suggestion) = self.custom_suggestion {
214 ctx = ctx.with_custom_suggestion(suggestion.clone());
215 }
216
217 ctx
218 }
219}
220
221impl From<ErrorBuilder> for OxiGdalError {
223 fn from(builder: ErrorBuilder) -> Self {
224 builder.error
225 }
226}