3 releases (breaking)

0.3.0 Jul 7, 2021
0.2.0 Jun 7, 2021
0.1.0 May 31, 2021

#73 in Value formatting

Download history 1108/week @ 2023-12-13 570/week @ 2023-12-20 290/week @ 2023-12-27 1355/week @ 2024-01-03 1075/week @ 2024-01-10 1324/week @ 2024-01-17 1073/week @ 2024-01-24 1357/week @ 2024-01-31 1851/week @ 2024-02-07 1984/week @ 2024-02-14 1707/week @ 2024-02-21 2246/week @ 2024-02-28 2296/week @ 2024-03-06 2642/week @ 2024-03-13 2490/week @ 2024-03-20 1935/week @ 2024-03-27

9,805 downloads per month
Used in 13 crates (9 directly)

MIT license

380KB
902 lines

format_serde_error

Build Status crates.io docs.rs

Serde error messages for humans.

Format serde errors in a way to make it obvious where the error in the source file was.

"example serde_json_long output"

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

"example serde_yaml output"

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

"example serde_yaml output"

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.

Crate Features

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.

Examples

serde_json

"example serde_json output"

serde_json_long

"example serde_json_long output"

serde_yaml

"example serde_yaml output"

serde_yaml_long

"example serde_yaml_long output"

custom

"example custom output"

custom_tabs

"example custom_tabs output"

Dependencies

~2–12MB
~115K SLoC