1use serde_json::Value;
3use thiserror;
4
5#[derive(Debug)]
7pub enum JsonType {
8 Null,
9 Bool,
10 Number,
11 String,
12 Array,
13 Object,
14}
15
16impl From<&Value> for JsonType {
17 fn from(value: &Value) -> JsonType {
18 match value {
19 Value::Null => JsonType::Null,
20 Value::Bool(_) => JsonType::Bool,
21 Value::Number(_) => JsonType::Number,
22 Value::String(_) => JsonType::String,
23 Value::Array(_) => JsonType::Array,
24 Value::Object(_) => JsonType::Object,
25 }
26 }
27}
28
29#[derive(thiserror::Error, Debug)]
31pub enum Error {
32 #[error("Malformed JSON string, unable to parse")]
33 MalformedJson { source: serde_json::Error },
34 #[error("{parser:?} > Expected a JSON type: {wanted:?}, but got a JSON type: {got:?}")]
35 UnexpectedJsonType {
36 got: JsonType,
37 wanted: JsonType,
38 parser: String,
39 },
40 #[error("{parser:?} > Expected a JSON type as {wanted:?}, but got {got:?}")]
41 UnexpectedJsonInvariant {
42 got: String,
43 wanted: String,
44 parser: String,
45 },
46 #[error(
47 "{parser:?} > Expected a field name in a JSON Object: {wanted:?}, but got fields named {got:?}"
48 )]
49 UnexpectedFieldName {
50 got: Vec<String>,
51 wanted: String,
52 parser: String,
53 },
54 #[error(
55 "{parser:?} > Expected a JSON Array with {wanted:?} elements, but got {got:?} elements"
56 )]
57 UnexpectedArrayLength {
58 got: usize,
59 wanted: usize,
60 parser: String,
61 },
62 #[error("Some internal error happened: {0}")]
63 InternalError(String),
64}