Repo of no-std crates for my personal embedded projects

refactor: Improve error messages

+19 -7
+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 - use miette::{Context, IntoDiagnostic, Result, bail, miette}; 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 - || Err(miette!("{} is not a value", value)), 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 - format!("Parsing failure of constant \"{}\" as {}", name, kind) 21 + miette!( 22 + labels = vec![LabeledSpan::at(value.span().unwrap(), "here")], 23 + "Parsing failure of constant \"{}\" as {}", 24 + name, 25 + kind, 26 + ) 22 27 }) 23 28 }, 24 29 ) ··· 34 39 } 35 40 36 41 pub fn parse_config(config: &str) -> Result<String> { 37 - let config_doc = config.parse::<toml_edit::DocumentMut>().into_diagnostic()?; 42 + let config_doc = config 43 + .parse::<toml_edit::Document<String>>() 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 - "{} is not a string value. Actual type: {}", 55 + "{} is not a string value. Actual type: {} @ {:?}", 49 56 name, 50 57 item.type_name(), 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 - "{} is not a string value. Actual type: {}", 75 + "{} is not a string value. Actual type: {} @ {:?}", 68 76 name, 69 77 kind.type_name(), 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 - "{} is not a value. Actual type: {}", 84 + labels = vec![LabeledSpan::at(value.span().unwrap(), "here")], 85 + "{} is not a value. Actual type: {} @ {:?}", 76 86 name, 77 87 value.type_name(), 88 + value.span(), 78 89 ) 90 + .with_source_code(config_doc.to_string()) 79 91 })?; 80 92 81 93 let line = match kind {