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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use std::{error::Error, fmt};

pub mod basic_types {
    pub use chrono::{Date, DateTime, NaiveDate, NaiveTime, Utc};
    pub use rust_decimal::Decimal;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::{borrow, fmt, ops};

    pub type Int = i64;
    pub type TagNum = u16;
    pub type SeqNum = u32;
    pub type NumInGroup = u8;
    pub type DayOfMonth = u8;

    pub type Float = Decimal;
    pub type Qty = Float;
    pub type Price = Float;
    pub type PriceOffset = Float;
    pub type Amt = Float;
    pub type Percentage = Float;

    pub type Boolean = bool;

    pub type Char = u8;
    pub type MultipleCharValue = Vec<Char>;

    #[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
    pub struct FixString(Vec<u8>);
    pub type MultipleStringValue = Vec<FixString>;

    pub use crate::country::Country;
    pub use crate::currency::Currency;
    pub type Exchange = [u8; 4];
    pub type MonthYear = Vec<u8>;
    pub type Language = [u8; 2];

    pub type UtcTimestamp = DateTime<Utc>;
    pub type UtcTimeOnly = Vec<u8>;
    pub type UtcDateOnly = Date<Utc>;

    pub type LocalMktTime = NaiveTime;
    pub type LocalMktDate = NaiveDate;

    pub type TzTimestamp = Vec<u8>;
    pub type TzTimeOnly = Vec<u8>;

    pub type Length = u16;
    pub type Data = Vec<u8>;
    pub type XmlData = Data;

    pub type Tenor = Vec<u8>;

    #[derive(Debug)]
    pub struct FixStringError {
        idx: usize,
        value: u8,
    }

    impl fmt::Display for FixStringError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(
                f,
                "Unexpected character '{:#04x}' at idx {}",
                self.value, self.idx
            )
        }
    }

    impl std::error::Error for FixStringError {}

    // TODO: Optional feature for ISO 8859-1 encoded strings
    impl FixString {
        pub fn new() -> FixString {
            FixString(Vec::new())
        }

        pub fn from_ascii(buf: Vec<u8>) -> Result<FixString, FixStringError> {
            for i in 0..buf.len() {
                // SAFETY: `i` never exceeds buf.len()
                let c = unsafe { *buf.get_unchecked(i) };
                if c < 0x20 || c > 0x7f {
                    return Err(FixStringError { idx: i, value: c });
                }
            }
            Ok(FixString(buf))
        }

        pub unsafe fn from_ascii_unchecked(buf: Vec<u8>) -> FixString {
            FixString(buf)
        }

        pub fn from_ascii_lossy(mut buf: Vec<u8>) -> FixString {
            for i in 0..buf.len() {
                // SAFETY: `i` never exceeds buf.len()
                let c = unsafe { buf.get_unchecked_mut(i) };
                if *c < 0x20 || *c > 0x7f {
                    *c = b'?';
                }
            }
            FixString(buf)
        }

        pub fn as_utf8(&self) -> &str {
            // SAFETY: ASCII is always valid UTF-8
            unsafe { std::str::from_utf8_unchecked(&self.0) }
        }

        pub fn into_utf8(self) -> String {
            // SAFETY: ASCII is always valid UTF-8
            unsafe { String::from_utf8_unchecked(self.0) }
        }

        pub fn into_bytes(self) -> Vec<u8> {
            self.0
        }
    }

    impl fmt::Display for FixString {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
            self.as_utf8().fmt(f)
        }
    }

    impl fmt::Debug for FixString {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "FixString(\"{}\")", self)
        }
    }

    impl ops::Deref for FixString {
        type Target = [u8];

        fn deref(&self) -> &[u8] {
            self.0.deref()
        }
    }

    impl AsRef<[u8]> for FixString {
        fn as_ref(&self) -> &[u8] {
            self.0.as_ref()
        }
    }

    impl borrow::Borrow<[u8]> for FixString {
        fn borrow(&self) -> &[u8] {
            self.0.borrow()
        }
    }

    impl From<&[u8]> for FixString {
        fn from(input: &[u8]) -> FixString {
            FixString(input.into())
        }
    }

    impl TryFrom<Vec<u8>> for FixString {
        type Error = FixStringError;

        fn try_from(buf: Vec<u8>) -> Result<FixString, Self::Error> {
            FixString::from_ascii(buf)
        }
    }

    impl TryFrom<&str> for FixString {
        type Error = FixStringError;

        fn try_from(buf: &str) -> Result<FixString, Self::Error> {
            FixString::from_ascii(buf.as_bytes().to_owned())
        }
    }

    impl TryFrom<String> for FixString {
        type Error = FixStringError;

        fn try_from(buf: String) -> Result<FixString, Self::Error> {
            FixString::from_ascii(buf.into_bytes())
        }
    }

    impl<const N: usize> TryFrom<[u8; N]> for FixString {
        type Error = FixStringError;

        fn try_from(buf: [u8; N]) -> Result<FixString, Self::Error> {
            FixString::from_ascii(buf.to_vec())
        }
    }

    impl<const N: usize> From<&[u8; N]> for FixString {
        fn from(input: &[u8; N]) -> FixString {
            FixString(input.as_slice().into())
        }
    }

    impl PartialEq<[u8]> for FixString {
        fn eq(&self, other: &[u8]) -> bool {
            self.0.eq(other)
        }
    }

    impl PartialEq<&[u8]> for FixString {
        fn eq(&self, other: &&[u8]) -> bool {
            self.0.eq(other)
        }
    }

    impl<const N: usize> PartialEq<[u8; N]> for FixString {
        fn eq(&self, other: &[u8; N]) -> bool {
            self.0.eq(other)
        }
    }

    impl<const N: usize> PartialEq<&'_ [u8; N]> for FixString {
        fn eq(&self, other: &&[u8; N]) -> bool {
            self.0.eq(other)
        }
    }

    impl PartialEq<Vec<u8>> for FixString {
        fn eq(&self, other: &Vec<u8>) -> bool {
            self.0.eq(other)
        }
    }

    impl PartialEq<&str> for FixString {
        fn eq(&self, other: &&str) -> bool {
            self.0.eq(other.as_bytes())
        }
    }

    impl PartialEq<str> for FixString {
        fn eq(&self, other: &str) -> bool {
            self.0.eq(other.as_bytes())
        }
    }

    impl PartialEq<String> for FixString {
        fn eq(&self, other: &String) -> bool {
            self.0.eq(other.as_bytes())
        }
    }

    use serde::de::{self, Visitor};

    struct FixStringVisitor;

    impl<'de> Visitor<'de> for FixStringVisitor {
        type Value = FixString;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("string")
        }

        fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            value.try_into().map_err(de::Error::custom)
        }
    }

    impl<'de> Deserialize<'de> for FixString {
        fn deserialize<D>(deserializer: D) -> Result<FixString, D::Error>
        where
            D: Deserializer<'de>,
        {
            deserializer.deserialize_str(FixStringVisitor)
        }
    }

    impl Serialize for FixString {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_str(&self.as_utf8())
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;

        #[test]
        fn fix_string_fail_on_ctrl_character() {
            let buf = b"Hello\x01world!".to_vec();
            assert!(FixString::from_ascii(buf).is_err());
        }

        #[test]
        fn fix_string_fail_on_out_of_range_character() {
            let buf = b"Hello\x85world!".to_vec();
            assert!(FixString::from_ascii(buf).is_err());
        }

        #[test]
        fn fix_string_replacemen_character_on_ctrl() {
            let buf = b"Hello\x01world!".to_vec();
            assert_eq!(FixString::from_ascii_lossy(buf), "Hello?world!");
        }

        #[test]
        fn fix_string_replacemen_character_on_out_of_range() {
            let buf = b"Hello\x85world!".to_vec();
            assert_eq!(FixString::from_ascii_lossy(buf), "Hello?world!");
        }
    }
}

pub use basic_types::*;

#[derive(Debug)]
pub enum DeserializeError {
    // TODO: enum maybe?
    GarbledMessage(String),
    Logout,
    Reject {
        msg_type: Vec<u8>,
        seq_num: SeqNum,
        tag: Option<TagNum>,
        reason: RejectReason,
    },
}

impl fmt::Display for DeserializeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DeserializeError::GarbledMessage(reason) => write!(f, "garbled message: {}", reason),
            DeserializeError::Logout => write!(f, "MsgSeqNum missing, force logout"),
            DeserializeError::Reject {
                msg_type,
                seq_num,
                tag,
                reason,
            } => write!(
                f,
                "message {:?}/{} rejected: {:?} (tag={:?})",
                msg_type, seq_num, reason, tag
            ),
        }
    }
}

impl Error for DeserializeError {}

// TODO:
// enum GarbledReason

#[derive(Debug)]
pub enum RejectReason {
    InvalidTagNumber,
    RequiredTagMissing,
    TagNotDefinedForThisMessageType,
    UndefinedTag,
    TagSpecifiedWithoutAValue,
    ValueIsIncorrect,
    IncorrectDataFormatForValue,
    DecryptionProblem,
    SignatureProblem,
    CompIdProblem,
    SendingTimeAccuracyProblem,
    InvalidMsgType,
    XmlValidationError,
    TagAppearsMoreThanOnce,
    TagSpecifiedOutOfRequiredOrder,
    RepeatingGroupFieldsOutOfOrder,
    IncorrectNumInGroupCountForRepeatingGroup,
    Non,
    Invalid,
    Other,
    //0	= Invalid Tag Number[InvalidTagNumber]
    //1	= Required Tag Missing[RequiredTagMissing]
    //2	= Tag not defined for this message type[TagNotDefinedForThisMessageType]
    //3	= Undefined tag[UndefinedTag]
    //4	= Tag specified without a value[TagSpecifiedWithoutAValue]
    //5	= Value is incorrect (out of range) for this tag[ValueIsIncorrect]
    //6	= Incorrect data format for value[IncorrectDataFormatForValue]
    //7	= Decryption problem[DecryptionProblem]
    //8	= Signature problem[SignatureProblem]
    //9	= CompID problem[CompIDProblem]
    //10	= SendingTime Accuracy Problem[SendingTimeAccuracyProblem]
    //11	= Invalid MsgType[InvalidMsgType]
    //12	= XML Validation Error[XMLValidationError]
    //13	= Tag appears more than once[TagAppearsMoreThanOnce]
    //14	= Tag specified out of required order[TagSpecifiedOutOfRequiredOrder]
    //15	= Repeating group fields out of order[RepeatingGroupFieldsOutOfOrder]
    //16	= Incorrect NumInGroup count for repeating group[IncorrectNumInGroupCountForRepeatingGroup]
    //17	= Non Data value includes field delimiter (<SOH> character)[Non]
    //18	= Invalid/Unsupported Application Version[Invalid]
    //99	= Other[Other]
}

// ----------------------------------------------------------------------------

pub fn parse_u16(input: &[u8]) -> Result<u16, &[u8]> {
    let i = input;

    if i.len() == 0 {
        return Err(input);
    }

    let mut value: u16 = 0;
    for i in input {
        match i {
            b'0'..=b'9' => {
                if let Some(v) = value.checked_mul(10).and_then(|v| v.checked_add(*i as u16)) {
                    value = v;
                } else {
                    return Err(input);
                }
            }
            _ => return Err(input),
        }
    }
    Ok(value)
}

/*






fn serialize_int(&self);
fn serialize_tag_num(&self);
fn serialize_seq_num(&self);
fn serialize_num_in_group(&self);
fn serialize_dat_of_month(&self);

fn serialize_float(&self);
fn serialize_qty(&self);
fn serialize_price(&self);
fn serialize_price_offset(&self);
fn serialize_percentage(&self);

fn serialize_boolean(&self);

fn serialize_char(&self);
fn serialize_multiple_char_value(&self);

fn serialize_string(&self);
fn serialize_multiple_string_value(&self);

fn serialize_country(&self);
fn serialize_currency(&self);
fn serialize_exchange(&self);
fn serialize_month_year(&self);
fn serialize_language(&self);

fn serialize_utc_timestamp(&self);
fn serialize_utc_time_only(&self);
fn serialize_utc_data_only(&self);

fn serialize_local_mkt_time(&self);
fn serialize_local_mkt_data(&self);

fn serialize_tz_timestamp(&self);
fn serialize_tz_timeonly(&self);

fn serialize_length(&self);
fn serialize_data(&self);
fn serialize_xml(&self);

fn serialize_tenor(&self);
*/