1use miette::{Diagnostic, SourceOffset, SourceSpan};
3use thiserror::Error;
4
5#[derive(Error, Diagnostic, Debug)]
6pub enum CastError {
7 #[error(transparent)]
8 #[diagnostic(transparent)]
9 JsonError(#[from] JsonError),
10
11 #[error(transparent)]
12 #[diagnostic(transparent)]
13 YamlError(#[from] YamlError),
14
15 #[error(transparent)]
16 #[diagnostic(transparent)]
17 TomlError(#[from] TomlError),
18
19 #[error(transparent)]
20 #[diagnostic(transparent)]
21 HclError(#[from] HclError),
22}
23
24#[derive(Error, Diagnostic, Debug)]
29#[diagnostic(code(cast::json))]
30#[error("Serde: Could not convert Json into Rust types")]
31pub struct JsonError {
32 #[source]
33 pub origin: serde_json::Error,
34 #[label("here")]
35 pub at: SourceSpan,
36 #[source_code]
37 pub src: String,
38}
39impl JsonError {
40 pub fn new(e: serde_json::Error, src: &str) -> Self {
41 JsonError {
42 at: SourceSpan::new(
43 SourceOffset::from_location(
44 src,
46 e.line(),
47 e.column(),
48 ),
49 1,
50 ),
51 src: src.to_owned(),
52 origin: e,
53 }
54 }
55}
56
57#[derive(Error, Diagnostic, Debug)]
62#[diagnostic(code(cast::toml))]
63#[error("Serde: Could not convert Toml into Rust types")]
64pub struct TomlError {
65 #[source]
66 pub origin: toml::de::Error,
67 #[label("here")]
68 pub at: SourceSpan,
69 #[source_code]
70 pub src: String,
71}
72impl TomlError {
73 pub fn new(e: toml::de::Error, src: &str) -> Self {
74 if let Some(span) = e.span() {
75 let line = span.start;
76 let column = span.end;
77 TomlError {
78 at: SourceSpan::new(
79 SourceOffset::from_location(
80 src, line, column,
82 ),
83 1,
84 ),
85 src: src.to_owned(),
86 origin: e,
87 }
88 } else {
89 TomlError {
90 at: SourceSpan::new(0.into(), 0),
91 src: src.to_owned(),
92 origin: e,
93 }
94 }
95 }
96}
97
98#[derive(Error, Diagnostic, Debug)]
103#[diagnostic(code(cast::hcl))]
104#[error("Serde: Could not convert Hcl into Rust types")]
105pub struct HclError {
106 #[source]
107 pub origin: hcl::Error,
108 #[label("here")]
109 pub at: SourceSpan,
110 #[source_code]
111 pub src: String,
112}
113impl HclError {
114 pub fn new(e: hcl::Error, src: &str) -> Self {
115 match e {
116 hcl::Error::Parse(e) => {
117 let line = e.location().line();
118 let column = e.location().column();
119 HclError {
120 at: SourceSpan::new(
121 SourceOffset::from_location(
122 src, line, column,
124 ),
125 1,
126 ),
127 src: src.to_owned(),
128 origin: hcl::Error::from(e),
129 }
130 }
131 _ => HclError {
132 at: SourceSpan::new(0.into(), 0),
133 src: src.to_owned(),
134 origin: e,
135 },
136 }
137 }
138}
139
140#[derive(Error, Diagnostic, Debug)]
145#[diagnostic(code(cast::yaml))]
146#[error("Serde: Could not convert Yaml into Rust types")]
147pub struct YamlError {
148 #[source]
149 pub origin: serde_yaml::Error,
150 #[label("here")]
151 pub at: SourceSpan,
152 #[source_code]
153 pub src: String,
154}
155impl YamlError {
156 pub fn new(e: serde_yaml::Error, src: &str) -> Self {
157 if let Some(location) = e.location() {
158 let line = location.line();
159 let column = location.column();
160 YamlError {
161 at: SourceSpan::new(
162 SourceOffset::from_location(
163 src, line, column,
165 ),
166 1,
167 ),
168 src: src.to_owned(),
169 origin: e,
170 }
171 } else {
172 YamlError {
173 at: SourceSpan::new(0.into(), 0),
174 src: src.to_owned(),
175 origin: e,
176 }
177 }
178 }
179}