Crate format_serde_error[][src]

Expand description

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

Example:

use format_serde_error::SerdeError;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct Config {
    values: Vec<String>,
}

fn parse_config() -> Result<Config, 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))?;

  Ok(config)
}

The output will be:

Error:
   | values:
   |   - 'first'
   |   - 'second'
 4 |   - third:
   |           ^ values[2]: invalid type: map, expected a string at line 4 column 10

Structs

SerdeError

Struct for formatting the error together with the source file to give a nicer output.

Enums

ColoringMode

Different behaviors for the crate to allow overriding the colored output behaviors. Creating the environment variable NO_COLOR (value is not relevant) will disable all coloring. There is also some detection going on to decide what kind of terminal type is used and if coloring should be used or not. See colored::control for more information.

ErrorTypes

Supported error types by the crate. Currently only serde_yaml and serde_json are supported.

Constants

CONTEXT_LINES

Amount of lines to show before and after the line containing the error.

Functions

always_color

Set coloring mode to never use color in the output (ColoringMode::NeverColor).

never_color

Set coloring mode to always use color in the output (ColoringMode::AlwaysColor).

set_coloring_mode

Change coloring mode across the library. See ColoringMode for more information. By default the library will detect if the output should use color or not ColoringMode::UseEnvironment.

use_environment

Set coloring mode detect if color should be used in the output or not (ColoringMode::UseEnvironment).