Repo of no-std crates for my personal embedded projects

refactor: Improve error messages

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