orion_error/core/
context.rs

1use derive_getters::Getters;
2use serde::Serialize;
3use std::fmt::Display;
4use thiserror::Error;
5
6#[derive(Debug, Clone, Getters, Default)]
7pub struct WithContext {
8    target: Option<String>,
9    context: ErrContext,
10}
11
12impl WithContext {
13    pub fn new() -> Self {
14        Self {
15            target: None,
16            context: ErrContext::default(),
17        }
18    }
19    pub fn want<S: Into<String>>(target: S) -> Self {
20        Self {
21            target: Some(target.into()),
22            context: ErrContext::default(),
23        }
24    }
25    pub fn with<S1: Into<String>, S2: Into<String>>(&mut self, key: S1, val: S2) {
26        self.context.items.push((key.into(), val.into()));
27    }
28}
29
30impl From<String> for WithContext {
31    fn from(value: String) -> Self {
32        Self {
33            target: None,
34            context: ErrContext::from(value.to_string()),
35        }
36    }
37}
38
39impl From<(String, String)> for WithContext {
40    fn from(value: (String, String)) -> Self {
41        Self {
42            target: None,
43            context: ErrContext::from(value),
44        }
45    }
46}
47
48impl From<(&str, &str)> for WithContext {
49    fn from(value: (&str, &str)) -> Self {
50        Self {
51            target: None,
52            context: ErrContext::from(value),
53        }
54    }
55}
56
57impl From<(&str, String)> for WithContext {
58    fn from(value: (&str, String)) -> Self {
59        Self {
60            target: None,
61            context: ErrContext::from(value),
62        }
63    }
64}
65
66impl From<&WithContext> for WithContext {
67    fn from(value: &WithContext) -> Self {
68        value.clone()
69    }
70}
71
72#[derive(Default, Error, Debug, Clone, PartialEq, Serialize)]
73pub struct ErrContext {
74    pub items: Vec<(String, String)>,
75}
76impl From<String> for ErrContext {
77    fn from(value: String) -> Self {
78        Self {
79            items: vec![("msg".into(), value)],
80        }
81    }
82}
83impl From<(String, String)> for ErrContext {
84    fn from(value: (String, String)) -> Self {
85        Self {
86            items: vec![(value.0, value.1)],
87        }
88    }
89}
90
91impl From<(&str, &str)> for ErrContext {
92    fn from(value: (&str, &str)) -> Self {
93        Self {
94            items: vec![(value.0.to_string(), value.1.to_string())],
95        }
96    }
97}
98
99impl From<(&str, String)> for ErrContext {
100    fn from(value: (&str, String)) -> Self {
101        Self {
102            items: vec![(value.0.to_string(), value.1)],
103        }
104    }
105}
106
107pub trait ContextAdd<T> {
108    fn add_context(&mut self, val: T);
109}
110
111impl Display for ErrContext {
112    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113        if !self.items.is_empty() {
114            writeln!(f, "\nerror context:")?;
115        }
116        for (k, v) in &self.items {
117            writeln!(f, "\t{} : {}", k, v)?;
118        }
119        Ok(())
120    }
121}