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
// Copyright 2016 James Bendig. See the COPYRIGHT file at the top-level
// directory of this distribution.
//
// Licensed under:
//   the MIT license
//     <LICENSE-MIT or https://opensource.org/licenses/MIT>
//   or the Apache License, Version 2.0
//     <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>,
// at your option. This file may not be copied, modified, or distributed
// except according to those terms.

//Helper macros

macro_rules! define_enum_field_type_impl {
    ( DEFAULT_VALUE_FUNC $field_type_type:ident { $( $base_type_field:path ),* $(),* } ) => {
        $( return $base_type_field; )*
    };

    ( DEFAULT_VALUE_FUNC $field_type_type:ty { $( $base_type_field:path ),* $(),* } ) => {
        return None;
    };

    ( IS_EMPTY_FUNC $field_type_type:ident ) => {
        |_field: &$field_type_type| {
            false
        }
    };

    ( IS_EMPTY_FUNC $field_type_type:ty ) => {
        |field: &$field_type_type| {
            field.is_none()
        }
    };

    ( NEW_VALUE_FUNC $base_type:ident, $field_type_type:ident ) => {
        |bytes: &[u8]| {
            $base_type::new(bytes)
        }
    };

    ( NEW_VALUE_FUNC $base_type:ident, $field_type_type:ty ) => {
        |bytes: &[u8]| {
            let new_value = $base_type::new(bytes);
            if new_value.is_some() {
                Some(new_value)
            }
            else {
                None
            }
        }
    };

    ( READ_FUNC_DEF $field_type_type:ident ) => {
        fn read(field: &Self::Type,_fix_version: $crate::fix_version::FIXVersion,_message_version: $crate::message_version::MessageVersion,buf: &mut Vec<u8>) -> usize {
            let bytes = field.to_bytes();
            buf.write(bytes).unwrap()
        }
    };

    ( READ_FUNC_DEF $field_type_type:ty ) => {
        fn read(field: &Self::Type,_fix_version: $crate::fix_version::FIXVersion,_message_version: $crate::message_version::MessageVersion,buf: &mut Vec<u8>) -> usize {
            if let Some(ref field) = *field {
                let bytes = field.to_bytes();
                return buf.write(bytes).unwrap()
            }

            0
        }
    };

    ( 1=> $base_type:ident { $( $base_type_field:path => $base_type_value:expr ),* $(),* } ) => {
       impl $base_type {
            fn new(bytes: &[u8]) -> Option<$base_type> {
                static MAPPING: ::phf::Map<&'static [u8],$base_type> = phf_map! {
                    $( $base_type_value => $base_type_field, )*
                };

                MAPPING.get(bytes).cloned()
            }

            fn to_bytes(&self) -> &'static [u8] {
                match *self {
                    $( $base_type_field => $base_type_value, )*
                }
            }
        }
    };

    ( 2=> $base_type:ident, $field_type:ident [ $( $field_type_type:tt )* ] { $( $base_type_field:path ),* $(),* } WITH_CUSTOM_SET_VALUE_ERROR_CHECK $custom_set_value_error_check_func:expr ) => {
        pub struct $field_type;

        impl $crate::field_type::FieldType for $field_type {
            type Type = $( $field_type_type )*;

            #[allow(unreachable_code)]
            fn default_value() -> Self::Type {
                define_enum_field_type_impl!( DEFAULT_VALUE_FUNC $( $field_type_type )* { $( $base_type_field, )* } );
            }

            fn set_value(field: &mut Self::Type,bytes: &[u8]) -> Result<(),$crate::message::SetValueError> {
                if let Some(value) = define_enum_field_type_impl!( NEW_VALUE_FUNC $base_type, $( $field_type_type )* )(bytes) {
                    *field = value;
                    return Ok(());
                }

                $custom_set_value_error_check_func(bytes)
            }

            fn is_empty(field: &Self::Type) -> bool {
                define_enum_field_type_impl!( IS_EMPTY_FUNC $( $field_type_type )* )(field)
            }

            fn len(_field: &Self::Type) -> usize {
                0 //Unused for this type.
            }

            define_enum_field_type_impl!( READ_FUNC_DEF $( $field_type_type )* );
        }
    };

    ( 2=> $base_type:ident, $field_type:ident [ $( $field_type_type:tt )* ] { $( $base_type_field:path ),* $(),* } MUST_BE_STRING ) => {
        define_enum_field_type_impl!( 2=> $base_type, $field_type [ $( $field_type_type )* ] { $( $base_type_field, )* } WITH_CUSTOM_SET_VALUE_ERROR_CHECK |_bytes: &[u8]| {
            //Not one of the supported bytes.
            Err($crate::message::SetValueError::OutOfRange)
        });
    };

    ( 2=> $base_type:ident, $field_type:ident [ $( $field_type_type:tt )* ] { $( $base_type_field:path ),* $(),* } MUST_BE_CHAR ) => {
        define_enum_field_type_impl!( 2=> $base_type, $field_type [ $( $field_type_type )* ] { $( $base_type_field, )* } WITH_CUSTOM_SET_VALUE_ERROR_CHECK |bytes: &[u8]| {
            //Figure out what went wrong.
            if bytes.len() == 1 {
                //Not one of the supported bytes.
                Err($crate::message::SetValueError::OutOfRange)
            }
            else {
                //Too many bytes provided.
                Err($crate::message::SetValueError::WrongFormat)
            }
        });
    };

    ( 2=> $base_type:ident, $field_type:ident [ $( $field_type_type:tt )* ] { $( $base_type_field:path ),* $(),* } MUST_BE_INT ) => {
        define_enum_field_type_impl!( 2=> $base_type, $field_type [ $( $field_type_type )* ] { $( $base_type_field, )* } WITH_CUSTOM_SET_VALUE_ERROR_CHECK |bytes: &[u8]| {
            //Figure out what went wrong.
            let value_string = String::from_utf8_lossy(bytes).into_owned();
            if <$crate::dictionary::field_types::generic::IntFieldType as $crate::field_type::FieldType>::Type::from_str(&value_string).is_ok() {
                //Not one of the supported integers.
                Err($crate::message::SetValueError::OutOfRange)
            }
            else {
                //Not an integer.
                Err($crate::message::SetValueError::WrongFormat)
            }
        });
    };

    ( REQUIRED, $base_type:ident, $field_type:ident { $( $base_type_field:path => $base_type_value:expr ),* $(),* } $must_be_sym:tt ) => {
        define_enum_field_type_impl!( 1=> $base_type { $( $base_type_field => $base_type_value, )* } );
        define_enum_field_type_impl!( 2=> $base_type, $field_type [ $base_type ] { $( $base_type_field, )* } $must_be_sym);
    };

    ( NOT_REQUIRED, $base_type:ident, $field_type:ident { $( $base_type_field:path => $base_type_value:expr ),* $(),* } $must_be_sym:tt ) => {
        define_enum_field_type_impl!( 1=> $base_type { $( $base_type_field => $base_type_value, )* } );
        define_enum_field_type_impl!( 2=> $base_type, $field_type [ Option<$base_type> ] { $( $base_type_field, )* } $must_be_sym);
    };

    ( REQUIRED_AND_NOT_REQUIRED, $base_type:ident, $required_field_type:ident, $not_required_field_type:ident { $( $base_type_field:path => $base_type_value:expr ),* $(),* } $must_be_sym:tt ) => {
        define_enum_field_type_impl!( 1=> $base_type { $( $base_type_field => $base_type_value, )* } );
        define_enum_field_type_impl!( 2=> $base_type, $required_field_type [ $base_type ] { $( $base_type_field, )* } $must_be_sym);
        define_enum_field_type_impl!( 2=> $base_type, $not_required_field_type [ Option<$base_type> ] { $( $base_type_field, )* } $must_be_sym);
    };
}

#[macro_export]
macro_rules! define_enum_field_type {
    (FIELD $enum_name:ident {
        $( $variant:ident => $value:expr, )+
    } $other_variant:tt,
    FIELD_TYPE [REQUIRED_AND_NOT_REQUIRED,BYTES] $required_field_type:ident $not_required_field_type:ident ) => {
        #[derive(Clone,Debug,PartialEq)]
        pub enum $enum_name {
            $( $variant ),+,
            $other_variant(Vec<u8>),
        }

        define_enum_field_type_with_reserved!(BYTES, $enum_name, $required_field_type, $not_required_field_type { $( $enum_name::$variant => $value ),+ , } $enum_name::$other_variant);
    };

    (FIELD $enum_name:ident {
        $( $variant:ident => $value:expr, )+
    } $other_variant:tt => WITH_MINIMUM $minimum_value:expr,
    FIELD_TYPE [$required_sym:tt] $field_type:ident ) => {
        #[derive(Clone,Debug,PartialEq)]
        pub enum $enum_name {
            $( $variant ),+,
            $other_variant(i64),
        }

        define_enum_field_type_with_reserved!($required_sym, $enum_name, $field_type { $( $enum_name::$variant => $value ),+ , } $enum_name::$other_variant => WITH_MINIMUM $minimum_value);
    };

    (FIELD $enum_name:ident {
        $( $variant:ident => $value:expr, )+
    },
    FIELD_TYPE [REQUIRED_AND_NOT_REQUIRED,$must_be_sym:tt] $required_field_type:ident $not_required_field_type:ident ) => {
        #[derive(Clone,Debug,PartialEq)]
        pub enum $enum_name {
            $( $variant ),+
        }

        define_enum_field_type_impl!(REQUIRED_AND_NOT_REQUIRED, $enum_name, $required_field_type, $not_required_field_type { $( $enum_name::$variant => $value ),+ , } $must_be_sym);
    };

    (FIELD $enum_name:ident {
        $( $variant:ident => $value:expr, )+
    },
    FIELD_TYPE [$required_sym:tt,$must_be_sym:tt] $field_type:ident ) => {
        #[derive(Clone,Debug,PartialEq)]
        pub enum $enum_name {
            $( $variant ),+
        }

        define_enum_field_type_impl!($required_sym, $enum_name, $field_type { $( $enum_name::$variant => $value ),+ , } $must_be_sym);
    };
}

#[macro_export]
macro_rules! define_enum_field_type_with_reserved {
    ( NEW_VALUE_FUNC $base_type:ident, $field_type_type:ident ) => {
        |new_int_value: i64| {
            $base_type::new(new_int_value)
        }
    };

    ( NEW_VALUE_FUNC $base_type:ident, $field_type_type:ty ) => {
        |new_int_value: i64| {
            let new_value = $base_type::new(new_int_value);
            if new_value.is_some() {
                Some(new_value)
            }
            else {
                None
            }
        }
    };

    ( READ_FUNC_DEF $field_type_type:ident ) => {
        fn read(field: &Self::Type,_fix_version: $crate::fix_version::FIXVersion,_message_version: $crate::message_version::MessageVersion,buf: &mut Vec<u8>) -> usize {
            let value = field.to_value();
            let value_string = value.to_string();
            let value_bytes = value_string.as_bytes();
            buf.write(value_bytes).unwrap()
        }
    };

    ( READ_FUNC_DEF $field_type_type:ty ) => {
        fn read(field: &Self::Type,_fix_version: $crate::fix_version::FIXVersion,_message_version: $crate::message_version::MessageVersion,buf: &mut Vec<u8>) -> usize {
            if let Some(ref field) = *field {
                let value = field.to_value();
                let value_string = value.to_string();
                let value_bytes = value_string.as_bytes();
                return buf.write(value_bytes).unwrap()
            }

            0
        }
    };

    ( AS_INT $base_type:ident, $field_type:ident, [ $( $field_type_type:tt )* ] { $( $base_type_field:path => $base_type_value:expr ),* $(),* } $base_type_reserved_field:path => WITH_MINIMUM $base_type_reserved_field_minimum:expr ) => {
        impl $base_type {
            fn new(value: i64) -> Option<$base_type> {
                match value {
                    $( $base_type_value => Some($base_type_field), )*
                    _ if value >= $base_type_reserved_field_minimum => Some($base_type_reserved_field(value)),
                    _ => None,
                }
            }

            fn to_value(&self) -> i64 {
                match *self {
                    $( $base_type_field => $base_type_value, )*
                    $base_type_reserved_field(value) => value,
                }
            }
        }

        pub struct $field_type;

        impl $crate::field_type::FieldType for $field_type {
            type Type = $( $field_type_type )*;

            #[allow(unreachable_code)]
            #[allow(needless_return)]
            fn default_value() -> Self::Type {
                define_enum_field_type_impl!( DEFAULT_VALUE_FUNC $( $field_type_type )* { $( $base_type_field, )* } );
            }

            fn set_value(field: &mut Self::Type,bytes: &[u8]) -> Result<(),$crate::message::SetValueError> {
                let value_string = String::from_utf8_lossy(bytes).into_owned();
                if let Ok(new_int_value) = i64::from_str(&value_string) {
                    if let Some(new_value) = define_enum_field_type_with_reserved!( NEW_VALUE_FUNC $base_type, $( $field_type_type )* )(new_int_value) {
                        *field = new_value;
                        Ok(())
                    }
                    else {
                        Err($crate::message::SetValueError::OutOfRange)
                    }
                }
                else {
                    Err($crate::message::SetValueError::WrongFormat)
                }
            }

            fn is_empty(field: &Self::Type) -> bool {
                define_enum_field_type_impl!( IS_EMPTY_FUNC $( $field_type_type )* )(field)
            }

            fn len(_field: &Self::Type) -> usize {
                0 //Unused for this type.
            }

            define_enum_field_type_with_reserved!( READ_FUNC_DEF $( $field_type_type )* );
        }
    };

    ( AS_BYTES_REQUIRED $base_type:ident, $field_type:ident { $( $base_type_field:path => $base_type_value:expr ),* $(),* } ) => {
        pub struct $field_type;

        impl $crate::field_type::FieldType for $field_type {
            type Type = $base_type;

            #[allow(unreachable_code)]
            #[allow(needless_return)]
            fn default_value() -> Self::Type {
                define_enum_field_type_impl!( DEFAULT_VALUE_FUNC $base_type { $( $base_type_field, )* } );
            }

            fn set_value(field: &mut Self::Type,bytes: &[u8]) -> Result<(),$crate::message::SetValueError> {
                if let Some(new_value) = $base_type::new(bytes) {
                    *field = new_value;
                    return Ok(());
                }

                Err($crate::message::SetValueError::OutOfRange)
            }

            fn is_empty(_field: &Self::Type) -> bool {
                false
            }

            fn len(_field: &Self::Type) -> usize {
                0 //Unused for this type
            }

            fn read(field: &Self::Type,_fix_version: $crate::fix_version::FIXVersion,_message_version: $crate::message_version::MessageVersion,buf: &mut Vec<u8>) -> usize {
                let value_bytes = field.as_bytes();
                return buf.write(value_bytes).unwrap()
            }
        }
    };

    ( AS_BYTES_NOT_REQUIRED $base_type:ident, $field_type:ident { $( $base_type_field:path => $base_type_value:expr ),* $(),* } ) => {
        pub struct $field_type;

        impl $crate::field_type::FieldType for $field_type {
            type Type = Option<$base_type>;

            #[allow(unreachable_code)]
            #[allow(needless_return)]
            fn default_value() -> Self::Type {
                define_enum_field_type_impl!( DEFAULT_VALUE_FUNC Self::Type { $( $base_type_field, )* } );
            }

            fn set_value(field: &mut Self::Type,bytes: &[u8]) -> Result<(),$crate::message::SetValueError> {
                *field = $base_type::new(bytes);
                Ok(())
            }

            fn is_empty(field: &Self::Type) -> bool {
                field.is_none()
            }

            fn len(_field: &Self::Type) -> usize {
                0 //Unused for this type.
            }

            fn read(field: &Self::Type,_fix_version: $crate::fix_version::FIXVersion,_message_version: $crate::message_version::MessageVersion,buf: &mut Vec<u8>) -> usize {
                if let Some(ref field) = *field {
                    let value_bytes = field.as_bytes();
                    return buf.write(value_bytes).unwrap()
                }

                0
            }
        }
    };

    ( REQUIRED, $base_type:ident, $field_type:ident { $( $base_type_field:path => $base_type_value:expr ),* $(),* } $base_type_reserved_field:path => WITH_MINIMUM $base_type_reserved_field_minimum:expr ) => {
        define_enum_field_type_with_reserved!( AS_INT $base_type, $field_type, [ $base_type ] { $( $base_type_field => $base_type_value,)* } $base_type_reserved_field => WITH_MINIMUM $base_type_reserved_field_minimum);
    };

    ( NOT_REQUIRED, $base_type:ident, $field_type:ident { $( $base_type_field:path => $base_type_value:expr ),* $(),* } $base_type_reserved_field:path => WITH_MINIMUM $base_type_reserved_field_minimum:expr ) => {
        define_enum_field_type_with_reserved!( AS_INT $base_type, $field_type, [ Option<$base_type> ] { $( $base_type_field => $base_type_value,)* } $base_type_reserved_field => WITH_MINIMUM $base_type_reserved_field_minimum);
    };

    ( BYTES, $base_type:ident, $required_field_type:ident, $not_required_field_type:ident { $( $base_type_field:path => $base_type_value:expr ),* $(),* } $base_type_reserved_field:path ) => {
        impl $base_type {
            fn new(value: &[u8]) -> Option<$base_type> {
                static MAPPING: ::phf::Map<&'static [u8],$base_type> = phf_map! {
                    $( $base_type_value => $base_type_field, )*
                };

                MAPPING.get(value).cloned()
            }

            fn as_bytes<'a>(&'a self) -> &'a [u8] {
                match *self {
                    $( $base_type_field => $base_type_value, )*
                    $base_type_reserved_field(ref value) => &value[..],
                }
            }
        }

        define_enum_field_type_with_reserved!( AS_BYTES_REQUIRED $base_type, $required_field_type { $( $base_type_field => $base_type_value,)* } );
        define_enum_field_type_with_reserved!( AS_BYTES_NOT_REQUIRED $base_type, $not_required_field_type { $( $base_type_field => $base_type_value,)* } );
    };
}