Coder Social home page Coder Social logo

therealhieu / format_serde_error Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexanderthaller/format_serde_error

0.0 0.0 0.0 966 KB

Serde error messages for humans.

Home Page: https://crates.io/crates/format_serde_error

Shell 1.22% Rust 98.78%

format_serde_error's Introduction

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, serde_json and toml 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.

toml

Enabled by default: yes

Enables support for errors emitted by toml.

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"

toml

"example toml"

custom

"example custom output"

custom_tabs

"example custom_tabs output"

format_serde_error's People

Contributors

alexanderthaller avatar hendrikmaus avatar pizzamig avatar therealhieu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.