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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use std::string::FromUtf8Error;

use thiserror::Error;

/// This trait contains all the conversion methods needed for
/// working with points of the SunSpec models.
pub trait Value: Sized {
    // FIXME we currently don't distinguish between decode errors
    // and not-set values. SunSpec does define special values for
    // unsupported/unset data. To do this 100% correct we would need to
    // wrap all primitives into some kind of value container type
    // that disallows the use of those special values.
    /// Decode value from a given slice of u16
    fn decode(data: &[u16]) -> Result<Self, DecodeError>;
    /// Encode value into a u16 array
    fn encode(self) -> Box<[u16]>;
}

/// This trait marks points with a fixed size. All non-string
/// values are actually fixed size.
pub trait FixedSize: Value {
    /// The size of this value
    const SIZE: u16;
}

impl Value for u16 {
    fn decode(data: &[u16]) -> Result<Self, DecodeError> {
        let &[w0] = data else {
            return Err(DecodeError::OutOfBounds);
        };
        Ok(w0)
    }
    fn encode(self) -> Box<[u16]> {
        Box::new([self])
    }
}

impl FixedSize for u16 {
    const SIZE: u16 = 1;
}

impl Value for u32 {
    fn decode(words: &[u16]) -> Result<Self, DecodeError> {
        let &[w1, w0] = words else {
            return Err(DecodeError::OutOfBounds);
        };
        Ok((w1 as u32) << 16 | w0 as u32)
    }
    fn encode(self) -> Box<[u16]> {
        Box::new([(self >> 16) as u16, self as u16])
    }
}

impl FixedSize for u32 {
    const SIZE: u16 = 2;
}

impl Value for u64 {
    fn decode(data: &[u16]) -> Result<Self, DecodeError> {
        let &[w3, w2, w1, w0] = data else {
            return Err(DecodeError::OutOfBounds);
        };
        Ok((w3 as u64) << 0x30 | (w2 as u64) << 0x20 | (w1 as u64) << 0x10 | w0 as u64)
    }
    fn encode(self) -> Box<[u16]> {
        Box::new([
            (self >> 0x30) as u16,
            (self >> 0x20) as u16,
            (self >> 0x10) as u16,
            self as u16,
        ])
    }
}

impl FixedSize for u64 {
    const SIZE: u16 = 4;
}

impl Value for u128 {
    fn decode(words: &[u16]) -> Result<Self, DecodeError> {
        let &[w7, w6, w5, w4, w3, w2, w1, w0] = words else {
            return Err(DecodeError::OutOfBounds);
        };
        Ok((w7 as u128) << 0x70
            | (w6 as u128) << 0x60
            | (w5 as u128) << 0x50
            | (w4 as u128) << 0x40
            | (w3 as u128) << 0x30
            | (w2 as u128) << 0x20
            | (w1 as u128) << 0x10
            | (w0 as u128))
    }
    fn encode(self) -> Box<[u16]> {
        Box::new([
            (self >> 0x70) as u16,
            (self >> 0x60) as u16,
            (self >> 0x50) as u16,
            (self >> 0x40) as u16,
            (self >> 0x30) as u16,
            (self >> 0x20) as u16,
            (self >> 0x10) as u16,
            self as u16,
        ])
    }
}

impl FixedSize for u128 {
    const SIZE: u16 = 8;
}

impl Value for i16 {
    fn decode(data: &[u16]) -> Result<Self, DecodeError> {
        Ok(u16::decode(data)? as Self)
    }
    fn encode(self) -> Box<[u16]> {
        (self as u16).encode()
    }
}

impl FixedSize for i16 {
    const SIZE: u16 = 1;
}

impl Value for i32 {
    fn decode(data: &[u16]) -> Result<Self, DecodeError> {
        Ok(u32::decode(data)? as Self)
    }
    fn encode(self) -> Box<[u16]> {
        (self as u32).encode()
    }
}

impl FixedSize for i32 {
    const SIZE: u16 = 2;
}

impl Value for i64 {
    fn decode(data: &[u16]) -> Result<Self, DecodeError> {
        Ok(u64::decode(data)? as Self)
    }
    fn encode(self) -> Box<[u16]> {
        (self as u64).encode()
    }
}

impl FixedSize for i64 {
    const SIZE: u16 = 4;
}

impl Value for f32 {
    fn decode(data: &[u16]) -> Result<Self, DecodeError> {
        let &[w1, w0] = data else {
            return Err(DecodeError::OutOfBounds);
        };
        Ok(f32::from_be_bytes([
            (w1 >> 8) as u8,
            w1 as u8,
            (w0 >> 8) as u8,
            w0 as u8,
        ]))
    }
    fn encode(self) -> Box<[u16]> {
        let [b3, b2, b1, b0] = self.to_be_bytes();
        Box::new([
            (b3 as u16) << 8 | (b2 as u16),
            (b1 as u16) << 8 | (b0 as u16),
        ])
    }
}

impl FixedSize for f32 {
    const SIZE: u16 = 2;
}

impl Value for f64 {
    fn decode(data: &[u16]) -> Result<Self, DecodeError> {
        let &[w3, w2, w1, w0] = data else {
            return Err(DecodeError::OutOfBounds);
        };
        Ok(f64::from_be_bytes([
            (w3 >> 8) as u8,
            w3 as u8,
            (w2 >> 8) as u8,
            w2 as u8,
            (w1 >> 8) as u8,
            w1 as u8,
            (w0 >> 8) as u8,
            w0 as u8,
        ]))
    }
    fn encode(self) -> Box<[u16]> {
        let [b7, b6, b5, b4, b3, b2, b1, b0] = self.to_be_bytes();
        Box::new([
            (b7 as u16) << 8 | (b6 as u16),
            (b5 as u16) << 8 | (b4 as u16),
            (b3 as u16) << 8 | (b2 as u16),
            (b1 as u16) << 8 | (b0 as u16),
        ])
    }
}

impl FixedSize for f64 {
    const SIZE: u16 = 4;
}

impl Value for String {
    fn decode(data: &[u16]) -> Result<Self, DecodeError> {
        let bytes = data
            .iter()
            .flat_map(|word| word.to_be_bytes())
            .take_while(|&b| b != 0)
            .collect::<Vec<_>>();
        Ok(String::from_utf8(bytes)?)
    }
    fn encode(self) -> Box<[u16]> {
        self.as_bytes()
            .chunks(2)
            .map(|chunk| match *chunk {
                [b1, b0] => (b1 as u16) << 8 | (b0 as u16),
                [b1] => (b1 as u16) << 8,
                _ => unreachable!(),
            })
            .collect()
    }
}

/// This error is returned if there was an error decoding
/// the value of a given point.
#[derive(Debug, Error, Eq, PartialEq)]
pub enum DecodeError {
    /// The value could not be decoded because the given data was not large
    /// enough.
    #[error("Out of bounds")]
    OutOfBounds,
    /// The given data was not valid UTF-8
    #[error("Invalid UTF-8 data")]
    Utf8(#[from] FromUtf8Error),
}

#[test]
fn test_u16() {
    assert_eq!(*0x0001i16.encode(), [0x1]);
    assert_eq!(u16::decode(&[0x1]), Ok(0x0001u16));
}

#[test]
fn test_u32() {
    assert_eq!(*0x00010002u32.encode(), [0x1, 0x2]);
    assert_eq!(u32::decode(&[0x1, 0x2]), Ok(0x00010002u32));
}

#[test]
fn test_u64() {
    assert_eq!(*0x0001000200030004u64.encode(), [0x1, 0x2, 0x3, 0x4]);
    assert_eq!(
        u64::decode(&[0x1, 0x2, 0x3, 0x4]),
        Ok(0x0001000200030004u64)
    );
}

#[test]
fn test_u128() {
    assert_eq!(
        *0x00010002000300040005000600070008u128.encode(),
        [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]
    );
    assert_eq!(
        u128::decode(&[0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]),
        Ok(0x00010002000300040005000600070008u128)
    );
}

#[test]
fn test_i16() {
    assert_eq!(*(-1i16).encode(), [0xffff]);
    assert_eq!(i16::decode(&[0xffff]), Ok(-1i16));
}

#[test]
fn test_i32() {
    assert_eq!(*(-1i32).encode(), [0xffff, 0xffff]);
    assert_eq!(i32::decode(&[0xffff, 0xffff]), Ok(-1i32));
}

#[test]
fn test_i64() {
    assert_eq!(*(-1i64).encode(), [0xffff, 0xffff, 0xffff, 0xffff]);
    assert_eq!(i64::decode(&[0xffff, 0xffff, 0xffff, 0xffff]), Ok(-1i64));
}

#[test]
fn test_f32() {
    assert_eq!(*(0.5f32).encode(), [0x3f00, 0x0000]);
    assert_eq!(f32::decode(&[0x3f00, 0x0000]), Ok(0.5f32));
}

#[test]
fn test_f64() {
    assert_eq!(*(0.5f64).encode(), [0x3fe0, 0x0000, 0x0000, 0x0000]);
    assert_eq!(f64::decode(&[0x3fe0, 0x0000, 0x0000, 0x0000]), Ok(0.5f64));
}

#[test]
fn test_string() {
    assert_eq!(*String::from("SunS").encode(), [0x5375, 0x6e53]);
    assert_eq!(String::decode(&[0x5375, 0x6e53]), Ok(String::from("SunS")));
    assert_eq!(*String::from("pad").encode(), [0x7061, 0x6400]);
    assert_eq!(String::decode(&[0x7061, 0x6400]), Ok(String::from("pad")));
    assert_eq!(
        String::decode(&[0x7061, 0x6400, 0x0000]),
        Ok(String::from("pad"))
    );
}