prosa-utils 0.4.3

ProSA utils
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Implementation of a simple String TVF

use bytes::Bytes;
use chrono::{NaiveDate, NaiveDateTime};

use crate::msg::tvf::{Tvf, TvfError};
use std::{borrow::Cow, collections::hash_map::HashMap};

/// Struct that define a simple string TVF
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct SimpleStringTvf {
    fields: HashMap<usize, String>,
}

static SIMPLE_DATE_FMT: &str = "%Y-%m-%d";
static SIMPLE_DATETIME_FMT: &str = "%Y-%m-%dT%H:%M:%S";

/// TVF implementation of simple string TVF
impl Tvf for SimpleStringTvf {
    /// Test if the TVF is empty (no value in it)
    ///
    /// # Examples
    ///
    /// ```
    /// use prosa_utils::msg::tvf::Tvf;
    /// use prosa_utils::msg::simple_string_tvf::SimpleStringTvf;
    ///
    /// let tvf: SimpleStringTvf = Default::default();
    ///
    /// assert_eq!(true, tvf.is_empty());
    /// ```
    fn is_empty(&self) -> bool {
        self.fields.is_empty()
    }

    /// Get the length of the TVF (number of value in it)
    ///
    /// # Examples
    ///
    /// ```
    /// use prosa_utils::msg::tvf::Tvf;
    /// use prosa_utils::msg::simple_string_tvf::SimpleStringTvf;
    ///
    /// let mut tvf: SimpleStringTvf = Default::default();
    /// tvf.put_string(1, String::from("first_val"));
    /// tvf.put_string(2, String::from("second_val"));
    ///
    /// assert_eq!(2, tvf.len());
    /// ```
    fn len(&self) -> usize {
        self.fields.len()
    }

    fn contains(&self, id: usize) -> bool {
        self.fields.contains_key(&id)
    }

    fn remove(&mut self, id: usize) {
        self.fields.remove(&id);
    }

    fn into_keys(self) -> Vec<usize> {
        self.fields.into_keys().collect::<_>()
    }

    fn keys(&self) -> Vec<usize> {
        self.fields.keys().cloned().collect()
    }

    fn get_buffer(&self, id: usize) -> Result<Cow<'_, SimpleStringTvf>, TvfError> {
        match self.fields.get(&id) {
            Some(str_value) => Ok(Cow::Owned(SimpleStringTvf::deserialize(str_value)?)),
            None => Err(TvfError::FieldNotFound(id)),
        }
    }

    fn get_unsigned(&self, id: usize) -> Result<u64, TvfError> {
        match self.fields.get(&id) {
            Some(str_value) => match str_value.parse::<u64>() {
                Ok(value) => Ok(value),
                Err(_) => Err(TvfError::TypeMismatch),
            },
            None => Err(TvfError::FieldNotFound(id)),
        }
    }

    fn get_signed(&self, id: usize) -> Result<i64, TvfError> {
        match self.fields.get(&id) {
            Some(str_value) => match str_value.parse::<i64>() {
                Ok(value) => Ok(value),
                Err(_) => Err(TvfError::TypeMismatch),
            },
            None => Err(TvfError::FieldNotFound(id)),
        }
    }

    fn get_byte(&self, id: usize) -> Result<u8, TvfError> {
        match self.fields.get(&id) {
            Some(str_value) => match hex::decode(str_value) {
                Ok(bytes) => Ok(bytes[0]),
                Err(_) => Err(TvfError::TypeMismatch),
            },
            None => Err(TvfError::FieldNotFound(id)),
        }
    }

    fn get_float(&self, id: usize) -> Result<f64, TvfError> {
        match self.fields.get(&id) {
            Some(str_value) => match str_value.parse::<f64>() {
                Ok(value) => Ok(value),
                Err(_) => Err(TvfError::TypeMismatch),
            },
            None => Err(TvfError::FieldNotFound(id)),
        }
    }

    fn get_string(&self, id: usize) -> Result<Cow<'_, String>, TvfError> {
        match self.fields.get(&id) {
            Some(value) => Ok(Cow::Borrowed(value)),
            None => Err(TvfError::FieldNotFound(id)),
        }
    }

    fn get_bytes(&self, id: usize) -> Result<Cow<'_, Bytes>, TvfError> {
        match self.fields.get(&id) {
            Some(str_value) => match hex::decode(str_value) {
                Ok(bytes) => Ok(Cow::Owned(Bytes::from(bytes))),
                Err(_) => Err(TvfError::TypeMismatch),
            },
            None => Err(TvfError::FieldNotFound(id)),
        }
    }

    fn get_date(&self, id: usize) -> Result<NaiveDate, TvfError> {
        match self.fields.get(&id) {
            Some(str_value) => match NaiveDate::parse_from_str(str_value, SIMPLE_DATE_FMT) {
                Ok(d) => Ok(d),
                Err(e) => Err(TvfError::ConvertionError(e.to_string())),
            },
            None => Err(TvfError::FieldNotFound(id)),
        }
    }

    fn get_datetime(&self, id: usize) -> Result<NaiveDateTime, TvfError> {
        match self.fields.get(&id) {
            Some(str_value) => {
                match NaiveDateTime::parse_from_str(str_value, SIMPLE_DATETIME_FMT) {
                    Ok(d) => Ok(d),
                    Err(e) => Err(TvfError::ConvertionError(e.to_string())),
                }
            }
            None => Err(TvfError::FieldNotFound(id)),
        }
    }

    fn put_buffer(&mut self, id: usize, buffer: SimpleStringTvf) {
        self.fields.insert(id, buffer.serialize());
    }

    fn put_unsigned(&mut self, id: usize, unsigned: u64) {
        self.fields.insert(id, unsigned.to_string());
    }

    fn put_signed(&mut self, id: usize, signed: i64) {
        self.fields.insert(id, signed.to_string());
    }

    fn put_byte(&mut self, id: usize, byte: u8) {
        let s = hex::encode(vec![byte]);
        self.fields.insert(id, s);
    }

    fn put_float(&mut self, id: usize, float: f64) {
        self.fields.insert(id, float.to_string());
    }

    fn put_string<T: Into<String>>(&mut self, id: usize, string: T) {
        self.fields.insert(id, string.into());
    }

    fn put_bytes(&mut self, id: usize, buffer: bytes::Bytes) {
        let s = hex::encode(buffer);
        self.fields.insert(id, s);
    }

    fn put_date(&mut self, id: usize, date: chrono::NaiveDate) {
        self.fields
            .insert(id, date.format(SIMPLE_DATE_FMT).to_string());
    }

    fn put_datetime(&mut self, id: usize, datetime: chrono::NaiveDateTime) {
        self.fields
            .insert(id, datetime.format(SIMPLE_DATETIME_FMT).to_string());
    }
}

impl SimpleStringTvf {
    /// Serialize this TVF to String
    pub fn serialize(&self) -> String {
        let mut out_str = String::new();
        //let mut writer = BufWriter::new(&out_str);

        for (k, v) in self.fields.iter() {
            let len = v.len();
            out_str.push_str(&format!("{k};{len};{v};"));
        }

        out_str
    }

    /// Load a TVF from String
    pub fn deserialize(serial: &str) -> Result<SimpleStringTvf, TvfError> {
        let mut buffer: SimpleStringTvf = Default::default();
        let mut w_serial = serial;

        while let Some((k, lv)) = w_serial.split_once(';') {
            let key = k
                .parse::<usize>()
                .map_err(|e| TvfError::SerializationError(e.to_string()))?;

            if let Some((l, rest)) = lv.split_once(';') {
                let len = l
                    .parse::<usize>()
                    .map_err(|e| TvfError::SerializationError(e.to_string()))?;
                buffer.fields.insert(key, String::from(&rest[0..len]));
                if rest.chars().nth(len) != Some(';') {
                    return Err(TvfError::SerializationError(
                        "Bad field termination char".into(),
                    ));
                }
                w_serial = &rest[len + 1..];
            } else {
                return Err(TvfError::SerializationError("No len after key".into()));
            }
        }

        Ok(buffer)
    }
}

#[cfg(test)]
mod tests {
    use crate::msg::tvf::TvfFilter;

    use super::*;
    use std::fmt::Debug;

    fn test_tvf<T: Tvf + Default + Debug + PartialEq + Clone>(tvf: &mut T) {
        let mut sub_buffer: T = Default::default();
        sub_buffer.put_float(200, 154.5);
        sub_buffer.put_string(201, "Hello rust!");
        sub_buffer.remove(201);
        assert!(!sub_buffer.contains(201));
        sub_buffer.put_string(201, "Hello world!");
        assert!(sub_buffer.contains(201));

        assert!(tvf.is_empty());
        tvf.put_unsigned(1, 42);
        assert_eq!(1, tvf.len());
        assert!(!tvf.contains(2));
        tvf.put_string(2, String::from("The great string"));
        assert!(tvf.contains(2));
        tvf.put_signed(3, -1);
        assert_eq!(Ok(-1), tvf.get_signed(3));
        tvf.put_float(5, 6.56);
        tvf.put_byte(6, 32u8);
        tvf.put_bytes(
            7,
            Bytes::from(hex::decode("aabb77ff").expect("Hexadecimal should be decode")),
        );
        tvf.put_date(
            8,
            NaiveDate::from_ymd_opt(2023, 6, 5).expect("NaiveDate should be build"),
        );
        tvf.put_datetime(
            9,
            NaiveDate::from_ymd_opt(2023, 6, 5)
                .expect("NaiveDate should be build")
                .and_hms_opt(15, 2, 0)
                .expect("NaiveDateTime should be build"),
        );
        tvf.put_buffer(10, sub_buffer.clone());
        assert_eq!(Err(TvfError::TypeMismatch), tvf.get_unsigned(2));
        assert_eq!(Err(TvfError::TypeMismatch), tvf.get_signed(2));
        assert_eq!(Err(TvfError::TypeMismatch), tvf.get_float(2));
        assert_eq!(Err(TvfError::TypeMismatch), tvf.get_byte(2));
        assert_eq!(Err(TvfError::TypeMismatch), tvf.get_bytes(2));
        assert_eq!(
            Err(TvfError::ConvertionError(
                "input contains invalid characters".to_string()
            )),
            tvf.get_date(2)
        );
        assert_eq!(
            Err(TvfError::ConvertionError(
                "input contains invalid characters".to_string()
            )),
            tvf.get_datetime(2)
        );

        assert_eq!(Ok(42), tvf.get_unsigned(1));
        assert_eq!(
            Ok(Cow::Borrowed(&String::from("The great string"))),
            tvf.get_string(2)
        );
        assert_eq!(Ok(-1), tvf.get_signed(3));
        assert_eq!(Err(TvfError::FieldNotFound(4)), tvf.get_float(4));
        assert_eq!(Ok(6.56), tvf.get_float(5));
        assert_eq!(Ok(32), tvf.get_byte(6));
        assert_eq!(
            Ok(Cow::Owned(Bytes::from(
                hex::decode("aabb77ff").expect("Hexadecimal should be decode")
            ))),
            tvf.get_bytes(7)
        );
        assert_eq!(
            Ok(NaiveDate::parse_from_str("2023-06-05", SIMPLE_DATE_FMT)
                .expect("NaiveDate should be build")),
            tvf.get_date(8)
        );
        assert_eq!(
            Ok(
                NaiveDateTime::parse_from_str("2023-06-05T15:02:00", SIMPLE_DATETIME_FMT)
                    .expect("NaiveDateTime should be build")
            ),
            tvf.get_datetime(9)
        );
        assert_eq!(Ok(Cow::Owned(sub_buffer)), tvf.get_buffer(10));

        assert_eq!(Err(TvfError::FieldNotFound(100)), tvf.get_unsigned(100));
        assert_eq!(Err(TvfError::FieldNotFound(110)), tvf.get_signed(110));
        assert_eq!(Err(TvfError::FieldNotFound(120)), tvf.get_float(120));
        assert_eq!(Err(TvfError::FieldNotFound(130)), tvf.get_string(130));
        assert_eq!(Err(TvfError::FieldNotFound(140)), tvf.get_byte(140));
        assert_eq!(Err(TvfError::FieldNotFound(150)), tvf.get_bytes(150));
        assert_eq!(Err(TvfError::FieldNotFound(160)), tvf.get_date(160));
        assert_eq!(Err(TvfError::FieldNotFound(170)), tvf.get_datetime(170));
        assert_eq!(Err(TvfError::FieldNotFound(180)), tvf.get_buffer(180));
    }

    #[test]
    fn test_simple_tvf() {
        let mut simple_tvf: SimpleStringTvf = Default::default();
        test_tvf(&mut simple_tvf);
        assert!(!format!("{simple_tvf:?}").is_empty());
        let keys = simple_tvf.keys();
        let into_keys = simple_tvf.clone().into_keys();
        assert_eq!(keys, into_keys);
        assert_eq!(9, keys.len());
        let serial = simple_tvf.serialize();
        let unserial = SimpleStringTvf::deserialize(&serial)
            .expect("The SimpleStringTvf should be deserialized");
        assert_eq!(simple_tvf, unserial);

        assert_eq!(
            Err(TvfError::SerializationError(
                "invalid digit found in string".into()
            )),
            SimpleStringTvf::deserialize("jean;luc")
        );
        assert_eq!(
            Err(TvfError::SerializationError("No len after key".into())),
            SimpleStringTvf::deserialize("1;")
        );
        assert_eq!(
            Err(TvfError::SerializationError(
                "invalid digit found in string".into()
            )),
            SimpleStringTvf::deserialize("1;jean;")
        );
        assert_eq!(
            Err(TvfError::SerializationError(
                "Bad field termination char".into()
            )),
            SimpleStringTvf::deserialize("1;2;to,")
        );
    }

    enum TvfTestFilter {}

    impl TvfFilter for TvfTestFilter {
        fn filter<T: Tvf>(mut buf: T) -> T {
            buf = <TvfTestFilter as TvfFilter>::mask_tvf_str_field(buf, 1, "0");
            buf
        }
    }

    #[test]
    fn test_tvf_filter() {
        let mut simple_tvf: SimpleStringTvf = Default::default();
        simple_tvf.put_string(1, "1234");
        simple_tvf.put_string(2, "1234");
        assert_eq!(2, simple_tvf.len());

        simple_tvf = TvfTestFilter::filter(simple_tvf);
        assert_eq!(2, simple_tvf.len());
        assert_eq!(
            Ok("0000"),
            simple_tvf.get_string(1).map(|v| v.to_string()).as_deref()
        );
        assert_eq!(
            Ok("1234"),
            simple_tvf.get_string(2).map(|v| v.to_string()).as_deref()
        );
    }
}