diff --git a/Cargo.toml b/Cargo.toml index a5d02a1..1fc53c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ categories = ["development-tools", "development-tools::testing"] [dependencies] serde_json = "1.0" -thiserror = "1.0" codespan-reporting = "0.11" [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index c633d16..d4ca682 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { diff --git a/src/validators/mod.rs b/src/validators/mod.rs index 7fc29b1..bbaa29e 100644 --- a/src/validators/mod.rs +++ b/src/validators/mod.rs @@ -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 {