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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::{
    interpret_trait::{InterpretableFrom, InterpreterContext, IntoRaw},
    serde_raw::{CheckBytesValueRaw, CheckValueListRaw, ValueSubTree},
};

use std::{fmt, fmt::Write};

use super::BytesValue;

#[derive(Debug)]
pub enum CheckValue<T: Default> {
    Star,
    Equal(T),
}

impl<T> CheckValue<T>
where
    T: InterpretableFrom<ValueSubTree> + Default,
{
    pub fn is_star(&self) -> bool {
        matches!(self, CheckValue::Star)
    }
}

impl<T> Default for CheckValue<T>
where
    T: Default,
{
    fn default() -> Self {
        CheckValue::Star
    }
}

impl<T> InterpretableFrom<CheckBytesValueRaw> for CheckValue<T>
where
    T: InterpretableFrom<ValueSubTree> + Default,
{
    fn interpret_from(from: CheckBytesValueRaw, context: &InterpreterContext) -> Self {
        match from {
            CheckBytesValueRaw::Unspecified => CheckValue::Star,
            CheckBytesValueRaw::Star => CheckValue::Star,
            CheckBytesValueRaw::Equal(bytes_value) => {
                CheckValue::Equal(T::interpret_from(bytes_value, context))
            },
        }
    }
}

impl<T> IntoRaw<CheckBytesValueRaw> for CheckValue<T>
where
    T: IntoRaw<ValueSubTree> + Default,
{
    fn into_raw(self) -> CheckBytesValueRaw {
        match self {
            CheckValue::Star => CheckBytesValueRaw::Unspecified,
            CheckValue::Equal(eq) => CheckBytesValueRaw::Equal(eq.into_raw()),
        }
    }
}

impl<T> CheckValue<T>
where
    T: IntoRaw<ValueSubTree> + Default,
{
    pub fn into_raw_explicit(self) -> CheckBytesValueRaw {
        match self {
            CheckValue::Star => CheckBytesValueRaw::Star,
            CheckValue::Equal(eq) => CheckBytesValueRaw::Equal(eq.into_raw()),
        }
    }
}

impl<T: fmt::Display + Default> fmt::Display for CheckValue<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CheckValue::Star => write!(f, "*"),
            CheckValue::Equal(eq_value) => eq_value.fmt(f),
        }
    }
}

/// Alias for a list of item checks that can be ignored altogether.
/// Valid values (with different behaviors): `"*"`, `["*"]`, `["1", "*"]`, `["*", "*", "*"]`
pub type CheckValueList = CheckValue<Vec<CheckValue<BytesValue>>>;

impl InterpretableFrom<CheckValueListRaw> for CheckValueList {
    fn interpret_from(from: CheckValueListRaw, context: &InterpreterContext) -> Self {
        match from {
            CheckValueListRaw::Unspecified => CheckValue::Star,
            CheckValueListRaw::Star => CheckValue::Star,
            CheckValueListRaw::CheckList(list_raw) => CheckValue::Equal(
                list_raw
                    .into_iter()
                    .map(|check_raw| CheckValue::<BytesValue>::interpret_from(check_raw, context))
                    .collect(),
            ),
        }
    }
}

impl IntoRaw<CheckValueListRaw> for CheckValueList {
    fn into_raw(self) -> CheckValueListRaw {
        match self {
            CheckValue::Star => CheckValueListRaw::Unspecified,
            CheckValue::Equal(list) => CheckValueListRaw::CheckList(
                list.into_iter().map(|cv| cv.into_raw_explicit()).collect(),
            ),
        }
    }
}

impl CheckValueList {
    pub fn pretty_str(&self) -> String {
        match self {
            CheckValue::Star => "*".to_string(),
            CheckValue::Equal(list) => {
                let mut s = String::new();
                s.push('[');
                for (i, check_value) in list.iter().enumerate() {
                    if i > 0 {
                        s.push(',');
                    }
                    write!(s, "{}", check_value).unwrap();
                }
                s.push(']');
                s
            },
        }
    }
}