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
use crate::field_map::{Field, FieldMap};

mod body;
mod header;
mod repeating_group;
mod trailer;

use hotfix_dictionary::{IsFieldDefinition, TagU32};

use crate::encoding::FieldValueError;
use crate::{FieldType, HardCodedFixFieldDefinition};
pub(crate) use body::Body;
pub(crate) use header::Header;
pub(crate) use repeating_group::RepeatingGroup;
pub(crate) use trailer::Trailer;

// TODO: what a rubbish name.. but can't think of anything better that's not overloaded with fefix names
pub trait Part {
    fn get_field_map(&self) -> &FieldMap;
    fn get_field_map_mut(&mut self) -> &mut FieldMap;

    fn store_field(&mut self, field: Field) {
        self.get_field_map_mut().insert(field)
    }

    #[inline]
    fn get<'a, V>(
        &'a self,
        field: &HardCodedFixFieldDefinition,
    ) -> Result<V, FieldValueError<V::Error>>
    where
        V: FieldType<'a>,
    {
        self.get_raw(field)
            .map(V::deserialize)
            .transpose()
            .map_err(FieldValueError::Invalid)
            .and_then(|opt| opt.ok_or(FieldValueError::Missing))
    }

    #[inline]
    fn get_raw(&self, field: &HardCodedFixFieldDefinition) -> Option<&[u8]> {
        self.get_field_map().get_raw(field.tag())
    }

    fn pop(&mut self, tag: &TagU32) -> Option<Field> {
        self.get_field_map_mut().fields.remove(tag)
    }

    fn set_groups(&mut self, start_tag: TagU32, groups: Vec<RepeatingGroup>) {
        self.get_field_map_mut().set_groups(start_tag, groups);
    }

    fn get_group(&self, start_tag: TagU32, index: usize) -> Option<&RepeatingGroup> {
        self.get_field_map().get_group(start_tag, index)
    }

    fn calculate_length(&self) -> usize {
        self.get_field_map().calculate_length(&[])
    }
}