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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use std::fmt::{Display, Formatter};

use crate::{Error, Result};
use crate::utils::bytes::string_to_bytes;

/// Represents type of field instruction.
///
/// It can be field instruction (including sequence and group) or template reference instruction.
#[derive(Debug, PartialEq, Clone)]
pub enum ValueType {
    UInt32,
    Int32,
    UInt64,
    Int64,
    Length,
    Exponent,
    Mantissa,
    Decimal,
    ASCIIString,
    UnicodeString,
    Bytes,
    Sequence,
    Group,
    TemplateReference,
}

impl ValueType {
    pub fn new_from_tag(tag: &str, unicode: bool) -> Result<Self> {
        match tag {
            "uInt32" => Ok(Self::UInt32),
            "int32" => Ok(Self::Int32),
            "uInt64" => Ok(Self::UInt64),
            "int64" => Ok(Self::Int64),
            "length" => Ok(Self::Length),
            "exponent" => Ok(Self::Exponent),
            "mantissa" => Ok(Self::Mantissa),
            "decimal" => Ok(Self::Decimal),
            "string" => {
                if unicode {
                    Ok(Self::UnicodeString)
                } else {
                    Ok(Self::ASCIIString)
                }
            }
            "byteVector" => Ok(Self::Bytes),
            "sequence" => Ok(Self::Sequence),
            "group" => Ok(Self::Group),
            "templateRef" => Ok(Self::TemplateReference),
            _ => Err(Error::Static(format!("Unknown type: {}", tag))),
        }
    }

    pub fn type_str(&self) -> &'static str {
        match self {
            ValueType::UInt32 => "uInt32",
            ValueType::Int32 => "int32",
            ValueType::UInt64 => "uInt64",
            ValueType::Int64 => "int64",
            ValueType::Length => "length",
            ValueType::Exponent => "exponent",
            ValueType::Mantissa => "mantissa",
            ValueType::Decimal => "decimal",
            ValueType::ASCIIString => "string",
            ValueType::UnicodeString => "string",
            ValueType::Bytes => "byteVector",
            ValueType::Sequence => "sequence",
            ValueType::Group => "group",
            ValueType::TemplateReference => "templateRef",
        }
    }

    pub fn to_default_value(&self) -> Result<Value> {
        match self {
            ValueType::UInt32 => Ok(Value::UInt32(0)),
            ValueType::Int32 => Ok(Value::Int32(0)),
            ValueType::UInt64 => Ok(Value::UInt64(0)),
            ValueType::Int64 => Ok(Value::Int64(0)),
            ValueType::Length => Ok(Value::UInt32(0)),
            ValueType::Exponent => Ok(Value::Int32(0)),
            ValueType::Mantissa => Ok(Value::Int64(0)),
            ValueType::Decimal => Ok(Value::Decimal(0.0)),
            ValueType::ASCIIString => Ok(Value::ASCIIString(String::new())),
            ValueType::UnicodeString => Ok(Value::UnicodeString(String::new())),
            ValueType::Bytes => Ok(Value::Bytes(Vec::new())),
            _ => Err(Error::Runtime(format!("{} cannot be converted to value", self.type_str()))),
        }
    }

    pub fn str_to_value(&self, s: &str) -> Result<Value> {
        let mut value = match self {
            ValueType::UInt32 => Value::UInt32(0),
            ValueType::Int32 => Value::Int32(0),
            ValueType::UInt64 => Value::UInt64(0),
            ValueType::Int64 => Value::Int64(0),
            ValueType::Length => Value::UInt32(0),
            ValueType::Exponent => Value::Int32(0),
            ValueType::Mantissa => Value::Int64(0),
            ValueType::Decimal => Value::Decimal(0.0),
            ValueType::ASCIIString => Value::ASCIIString(String::new()),
            ValueType::UnicodeString => Value::UnicodeString(String::new()),
            ValueType::Bytes => Value::Bytes(Vec::new()),
            _ => return Err(Error::Runtime(format!("{} cannot be converted to value", self.type_str()))),
        };
        value.set_from_string(s)?;
        Ok(value)
    }
}


/// Represents current value of a field.
#[derive(Debug, PartialEq, Clone)]
pub enum Value {
    UInt32(u32),
    Int32(i32),
    UInt64(u64),
    Int64(i64),
    Decimal(f64),
    ASCIIString(String),
    UnicodeString(String),
    Bytes(Vec<u8>),
}

impl Value {
    // It is a dynamic error [ERR D11] if a string does not match the syntax.
    pub fn set_from_string(&mut self, s: &str) -> Result<()> {
        match self {
            Value::UInt32(_) => {
                *self = Value::UInt32(s.parse()?);
            }
            Value::Int32(_) => {
                *self = Value::Int32(s.parse()?);
            }
            Value::UInt64(_) => {
                *self = Value::UInt64(s.parse()?);
            }
            Value::Int64(_) => {
                *self = Value::Int64(s.parse()?);
            }
            Value::Decimal(_) => {
                *self = Value::Decimal(s.parse()?);
            }
            Value::ASCIIString(_) => {
                *self = Value::ASCIIString(s.to_string());
            }
            Value::UnicodeString(_) => {
                *self = Value::UnicodeString(s.to_string());
            }
            // The string is interpreted as an even number of hexadecimal digits [0-9A-Fa-f] possibly interleaved
            // with whitespace. The literal is turned into a byte vector by first stripping any whitespace.
            // Then each pair of characters is interpreted as a hexadecimal number representing a single byte.
            Value::Bytes(_) => {
                *self = Value::Bytes(string_to_bytes(s)?);
            }
        }
        Ok(())
    }

    pub fn apply_delta(&self, delta: Value, sub: i32) -> Result<Value> {
        match (self, &delta) {
            (Value::UInt32(v), Value::Int64(d)) => {
                if *d < 0 {
                    Ok(Value::UInt32(*v - (-*d) as u32))
                } else {
                    Ok(Value::UInt32(*v + *d as u32))
                }
            }
            (Value::Int32(v), Value::Int64(d)) => {
                Ok(Value::Int32(v + *d as i32))
            }
            (Value::UInt64(v), Value::Int64(d)) => {
                if *d < 0 {
                    Ok(Value::UInt64(*v - (-*d) as u64))
                } else {
                    Ok(Value::UInt64(*v + *d as u64))
                }
            }
            (Value::Int64(v), Value::Int64(d)) => {
                Ok(Value::Int64(v + *d))
            }
            (Value::ASCIIString(v), Value::ASCIIString(d)) => {
                let (front, i) = sub2index(sub, v.len())?;
                let s;
                if front {
                    s = format!("{}{}", d, v[i..].to_string());
                } else {
                    s = format!("{}{}", v[..i].to_string(), d);
                }
                Ok(Value::ASCIIString(s))
            }
            (Value::Bytes(v), Value::Bytes(d)) => {
                Ok(Value::Bytes(bytes_delta(v, d, sub)?))
            }
            (Value::UnicodeString(v), Value::Bytes(d)) => {
                let b = bytes_delta(v.as_bytes(), d, sub)?;
                let s = String::from_utf8(b)?; // [ERR R2]
                Ok(Value::UnicodeString(s))
            }
            _ => Err(Error::Runtime(format!("Cannot apply delta {:?} to {:?}", delta, self))),
        }
    }

    pub fn apply_tail(&self, tail: Value) -> Result<Value> {
        let len: usize;
        match (self, &tail) {
            (Value::ASCIIString(_), Value::ASCIIString(t)) => {
                len = t.len();
            }
            (Value::UnicodeString(_), Value::Bytes(t)) => {
                len = t.len();
            }
            (Value::Bytes(_), Value::Bytes(t)) => {
                len = t.len();
            }
            _ => return Err(Error::Runtime(format!("Cannot apply tail {:?} to {:?}", tail, self))),
        }
        self.apply_delta(tail, len as i32)
    }

    pub fn apply_increment(&self) -> Result<Value> {
        match self {
            Value::UInt32(v) => {
                Ok(Value::UInt32(v + 1))
            }
            Value::Int32(v) => {
                Ok(Value::Int32(v + 1))
            }
            Value::UInt64(v) => {
                Ok(Value::UInt64(v + 1))
            }
            Value::Int64(v) => {
                Ok(Value::Int64(v + 1))
            }
            _ => Err(Error::Runtime(format!("Cannot apply increment to {:?}", self)))
        }
    }
}

impl Display for Value {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Value::UInt32(v) => f.write_fmt(format_args!("{v}")),
            Value::Int32(v) => f.write_fmt(format_args!("{v}")),
            Value::UInt64(v) => f.write_fmt(format_args!("{v}")),
            Value::Int64(v) => f.write_fmt(format_args!("{v}")),
            Value::Decimal(v) => f.write_fmt(format_args!("{v}")),
            Value::ASCIIString(s) => f.write_str(s),
            Value::UnicodeString(s) => f.write_fmt(format_args!("{s}")),
            Value::Bytes(b) => {
                let mut s = String::with_capacity(2 * b.len());
                for v in b {
                    s += &format!("{:02x}", v);
                }
                f.write_fmt(format_args!("{s}"))
            }
        }
    }
}

fn sub2index(sub: i32, len: usize) -> Result<(bool, usize)> {
    // A negative subtraction length is used to remove values from the front of the string.
    // Negative zero is used to append values to the front of the string.
    let front: bool;
    let mut i: usize;
    if sub < 0 {
        front = true;
        i = (-sub - 1) as usize;
    } else {
        front = false;
        i = sub as usize;
    }
    if i > len {
        return Err(Error::Dynamic(format!("subtraction length ({i}) is larger than string length ('{len}')")))  // [ERR D7]
    }
    if !front {
        i = len - i;
    }
    Ok((front, i))
}

fn bytes_delta(v: &[u8], d: &[u8], sub: i32) -> Result<Vec<u8>> {
    let (front, i) = sub2index(sub, v.len())?;
    let mut b = Vec::with_capacity(v.len() + d.len());
    if front {
        b.extend_from_slice(d);
        b.extend_from_slice(&v[i..]);
    } else {
        b.extend_from_slice(&v[..i]);
        b.extend_from_slice(d);
    }
    Ok(b)
}