Crates.io | format_serde_error |
lib.rs | format_serde_error |
version | 0.3.0 |
source | src |
created_at | 2021-05-31 15:31:16.96356 |
updated_at | 2021-07-07 11:37:14.231825 |
description | Serde error messages for humans. |
homepage | https://github.com/AlexanderThaller/format_serde_error |
repository | https://github.com/AlexanderThaller/format_serde_error |
max_upload_size | |
id | 404274 |
size | 466,379 |
Serde error messages for humans.
Format serde errors in a way to make it obvious where the error in the source file was.
Add this to your Cargo.toml:
[dependencies]
format_serde_error = "0.3"
Currently serde_yaml and serde_json are supported. Extending the library to more data types should be relativly easy as long as the errors emit a line and column.
Also has a custom error type which supports printing a message with a given line and column (see examples/custom.rs).
Usage Example (from examples/serde_yaml.rs):
use format_serde_error::SerdeError;
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Config {
values: Vec<String>,
}
fn main() -> Result<(), anyhow::Error> {
let config_str = "values:
- 'first'
- 'second'
- third:";
let config = serde_yaml::from_str::<Config>(config_str)
.map_err(|err| SerdeError::new(config_str.to_string(), err))?;
dbg!(config);
Ok(())
}
The output will be:
Error:
| values:
| - 'first'
| - 'second'
4 | - third:
| ^ values[2]: invalid type: map, expected a string at line 4 column 10
The crate will also shorten long lines if necessary (from examples/serde_yaml.rs):
Error:
| values:
| - 'first'
| - 'second'
4 | - third: Lorem ipsum dolor sit amet, consectetur adipiscing...
| ^ values[2]: invalid type: map, expected a string at line 4 column 10
The amount of context for lines and characters can be controlled globally and per error. See documentation for how to do that. Adding context and shortening the lines can also be disabled.
serde_yaml
Enabled by default: yes
Enables support for errors emitted by serde_yaml
.
serde_json
Enabled by default: yes
Enables support for errors emitted by serde_json
.
colored
Enabled by default: yes
Enables support for color output to a terminal using the colored
crate.
graphemes_support
Enabled by default: yes
Enables proper support for grapheme cluster when contextualizing long error lines.