Skip to content

Commit

Permalink
Merge pull request #3 from bheylin/rm-thiserror
Browse files Browse the repository at this point in the history
Rm `thiserror` dep
  • Loading branch information
charlesvdv authored Oct 30, 2024
2 parents 264de8e + 9c1e075 commit 464fe8c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ categories = ["development-tools", "development-tools::testing"]

[dependencies]
serde_json = "1.0"
thiserror = "1.0"
codespan-reporting = "0.11"

[dev-dependencies]
Expand Down
42 changes: 29 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,51 @@
//! }
//! ```

use core::fmt;

/// A JSON-value. Used by the [Validator] trait.
pub type Value = serde_json::Value;

fn get_value_type_id(val: &Value) -> String {
fn get_value_type_id(val: &Value) -> &'static str {
match val {
serde_json::Value::Null => String::from("null"),
serde_json::Value::Bool(_) => String::from("bool"),
serde_json::Value::Number(_) => String::from("number"),
serde_json::Value::String(_) => String::from("string"),
serde_json::Value::Array(_) => String::from("array"),
serde_json::Value::Object(_) => String::from("object"),
serde_json::Value::Null => "null",
serde_json::Value::Bool(_) => "bool",
serde_json::Value::Number(_) => "number",
serde_json::Value::String(_) => "string",
serde_json::Value::Array(_) => "array",
serde_json::Value::Object(_) => "object",
}
}

/// Validation error
#[derive(thiserror::Error, Debug, PartialEq)]
#[derive(Debug, PartialEq)]
pub enum Error<'a> {
#[error("Invalid type. Expected {} but got {}.", .1, get_value_type_id(.0))]
InvalidType(&'a Value, String),
#[error("Invalid value. Expected {1} but got {0}.")]
InvalidValue(&'a Value, String),
#[error("Missing key '{1}' in object")]
MissingObjectKey(&'a Value, String),
#[error("Key '{1}' is not expected in object")]
UnexpectedObjectKey(&'a Value, String),
#[error("No match for expected array element {1}")]
UnmatchedValidator(&'a Value, usize),
}

impl<'a> std::error::Error for Error<'a> {}

impl<'a> fmt::Display for Error<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidType(v, s) => write!(
f,
"Invalid type. Expected {} but got {}.",
s,
get_value_type_id(v)
),
Self::InvalidValue(v, s) => write!(f, "Invalid value. Expected {s} but got {v}."),
Self::MissingObjectKey(_v, s) => write!(f, "Missing key '{s}' in object"),
Self::UnexpectedObjectKey(_v, s) => write!(f, "Key '{s}' is not expected in object"),
Self::UnmatchedValidator(_v, s) => write!(f, "No match for expected array element {s}"),
}
}
}

impl<'a> Error<'a> {
fn location(&self) -> &'a Value {
match self {
Expand Down
5 changes: 4 additions & 1 deletion src/validators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ where
fn validate<'a>(&self, value: &'a Value) -> Result<(), Error<'a>> {
let expected_val = self.expected.clone().into();
if get_value_type_id(&expected_val) != get_value_type_id(value) {
return Err(Error::InvalidType(value, get_value_type_id(&expected_val)));
return Err(Error::InvalidType(
value,
get_value_type_id(&expected_val).to_string(),
));
}

if value == &expected_val {
Expand Down

0 comments on commit 464fe8c

Please sign in to comment.