tangled
alpha
login
or
join now
sachy.dev
/
sachy-embed-core
0
fork
atom
Repo of no-std crates for my personal embedded projects
0
fork
atom
overview
issues
pulls
pipelines
refactor: Improve error messages
sachy.dev
1 week ago
4f0b20c4
b76d1dc1
2/2
miri.yml
success
1min 18s
test.yml
success
1min
+19
-7
1 changed file
expand all
collapse all
unified
split
sachy-config
src
lib.rs
+19
-7
sachy-config/src/lib.rs
···
1
1
use core::{error::Error, fmt::Debug, str::FromStr};
2
2
use std::io::Write;
3
3
4
4
-
use miette::{Context, IntoDiagnostic, Result, bail, miette};
4
4
+
use miette::{Context, IntoDiagnostic, LabeledSpan, Result, bail, miette};
5
5
use toml_edit::Value;
6
6
7
7
fn validate_kind_to_string<T: Debug + FromStr + Send + Sync>(
···
15
15
value
16
16
.as_str()
17
17
.map_or_else(
18
18
-
|| Err(miette!("{} is not a value", value)),
18
18
+
|| Err(miette!("{} is not a value @ {:?}", value, value.span())),
19
19
|value_str| {
20
20
value_str.parse().into_diagnostic().wrap_err_with(|| {
21
21
-
format!("Parsing failure of constant \"{}\" as {}", name, kind)
21
21
+
miette!(
22
22
+
labels = vec![LabeledSpan::at(value.span().unwrap(), "here")],
23
23
+
"Parsing failure of constant \"{}\" as {}",
24
24
+
name,
25
25
+
kind,
26
26
+
)
22
27
})
23
28
},
24
29
)
···
34
39
}
35
40
36
41
pub fn parse_config(config: &str) -> Result<String> {
37
37
-
let config_doc = config.parse::<toml_edit::DocumentMut>().into_diagnostic()?;
42
42
+
let config_doc = config
43
43
+
.parse::<toml_edit::Document<String>>()
44
44
+
.into_diagnostic()?;
38
45
let mut output = String::new();
39
46
40
47
config_doc
···
45
52
.try_for_each(|(name, item)| -> miette::Result<()> {
46
53
let value = item.as_str().ok_or_else(|| {
47
54
miette!(
48
48
-
"{} is not a string value. Actual type: {}",
55
55
+
"{} is not a string value. Actual type: {} @ {:?}",
49
56
name,
50
57
item.type_name(),
58
58
+
item.span()
51
59
)
52
60
})?;
53
61
let static_output = format!("pub static {}: &str = \"{}\";\n", name, value);
···
64
72
let kind = &constant_value["type"];
65
73
let kind = kind.as_str().ok_or_else(|| {
66
74
miette!(
67
67
-
"{} is not a string value. Actual type: {}",
75
75
+
"{} is not a string value. Actual type: {} @ {:?}",
68
76
name,
69
77
kind.type_name(),
78
78
+
constant_value.span()
70
79
)
71
80
})?;
72
81
let value = &constant_value["value"];
73
82
let value = value.as_value().ok_or_else(|| {
74
83
miette!(
75
75
-
"{} is not a value. Actual type: {}",
84
84
+
labels = vec![LabeledSpan::at(value.span().unwrap(), "here")],
85
85
+
"{} is not a value. Actual type: {} @ {:?}",
76
86
name,
77
87
value.type_name(),
88
88
+
value.span(),
78
89
)
90
90
+
.with_source_code(config_doc.to_string())
79
91
})?;
80
92
81
93
let line = match kind {