1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::fmt::{Display, Formatter};
use thiserror::Error;
use vg_errortools::FatIOError;

#[derive(Debug, Error)]
pub enum Error {
    #[error("Error opening file: {0}")]
    IOError(#[from] FatIOError),
    #[error("Error parsing first json: {0}")]
    JSON(#[from] serde_json::Error),
}

#[derive(Debug)]
pub enum DiffType {
    RootMismatch,
    LeftExtra,
    RightExtra,
    Mismatch,
}

impl Display for DiffType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let msg = match self {
            DiffType::RootMismatch => "Mismatch at root.",
            DiffType::LeftExtra => "Extra on left",
            DiffType::RightExtra => "Extra on right",
            DiffType::Mismatch => "Mismatched",
        };
        write!(f, "{}", msg)
    }
}

pub enum ValueType {
    Key(String),
    Value {
        key: String,
        value_left: String,
        value_right: String,
    },
}

impl ValueType {
    pub fn new_value(key: String, value_left: String, value_right: String) -> Self {
        Self::Value {
            value_right,
            value_left,
            key,
        }
    }
    pub fn new_key(key: String) -> Self {
        Self::Key(key)
    }

    pub fn get_key(&self) -> &str {
        match self {
            ValueType::Value { key, .. } => key.as_str(),
            ValueType::Key(key) => key.as_str(),
        }
    }
}

impl Display for ValueType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ValueType::Key(key) => write!(f, "{key}"),
            ValueType::Value {
                value_left,
                key,
                value_right,
            } => {
                write!(f, "{key} {{ {value_left} != {value_right} }}")
            }
        }
    }
}