replace failure with thiserror, fix error not implementing Error

master v1.0.3
Ondřej Hruška 4 years ago
parent c8a1166789
commit e564922c0a
  1. 5
      Cargo.toml
  2. 4
      README.md
  3. 21
      src/lib.rs

@ -1,6 +1,6 @@
[package] [package]
name = "json_dotpath" name = "json_dotpath"
version = "1.0.2" version = "1.0.3"
authors = ["Ondřej Hruška <ondra@ondrovo.com>"] authors = ["Ondřej Hruška <ondra@ondrovo.com>"]
edition = "2018" edition = "2018"
license = "MIT" license = "MIT"
@ -16,5 +16,4 @@ categories = [
serde = "1" serde = "1"
serde_derive = "1" serde_derive = "1"
serde_json = "1" serde_json = "1"
failure = "0.1.7" thiserror = "1"
failure_derive = "0.1.7"

@ -4,6 +4,10 @@ Access members of nested JSON arrays and objects using "dotted paths".
## Changes ## Changes
### 1.0.3
Replaced `failure` with `thiserror`, implement `std::error::Error` for the error type.
### 1.0.0 ### 1.0.0
The API changed to return `Result<Option<T>>` instead of panicking internally on error. The API changed to return `Result<Option<T>>` instead of panicking internally on error.

@ -2,41 +2,34 @@ use serde::de::DeserializeOwned;
use serde::Serialize; use serde::Serialize;
use serde_json::{Map, Value}; use serde_json::{Map, Value};
use std::mem; use std::mem;
use thiserror::Error;
#[cfg(test)] #[cfg(test)]
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
#[macro_use]
extern crate failure;
/// Errors from dot_path methods /// Errors from dot_path methods
#[derive(Debug, Fail)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
/// Path hit a value in the JSON object that is not array or map /// Path hit a value in the JSON object that is not array or map
/// and could not continue the traversal. /// and could not continue the traversal.
/// ///
/// (e.g. `foo.bar` in `{"foo": 123}`) /// (e.g. `foo.bar` in `{"foo": 123}`)
#[fail(display = "Unexpected value reached while traversing path")] #[error("Unexpected value reached while traversing path")]
BadPathElement, BadPathElement,
/// Array index out of range /// Array index out of range
#[fail(display = "Invalid array index: {}", _0)] #[error("Invalid array index: {0}")]
BadIndex(usize), BadIndex(usize),
/// Invalid (usually empty) key used in Map or Array. /// Invalid (usually empty) key used in Map or Array.
/// If the key is valid but out of bounds, `BadIndex` will be used. /// If the key is valid but out of bounds, `BadIndex` will be used.
#[fail(display = "Invalid key: {}", _0)] #[error("Invalid key: {0}")]
InvalidKey(String), InvalidKey(String),
/// Error serializing or deserializing a value /// Error serializing or deserializing a value
#[fail(display = "Invalid array or map key")] #[error("Invalid array or map key")]
SerdeError(#[fail(cause)] serde_json::Error), SerdeError(#[from] serde_json::Error),
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::SerdeError(e)
}
} }
use crate::Error::{BadIndex, BadPathElement, InvalidKey}; use crate::Error::{BadIndex, BadPathElement, InvalidKey};

Loading…
Cancel
Save