Skip to main content

hotfix_message/
parts.rs

1use crate::field_map::{Field, FieldMap};
2use std::collections::HashSet;
3
4mod body;
5mod header;
6mod repeating_group;
7mod trailer;
8
9use hotfix_dictionary::{IsFieldDefinition, TagU32};
10
11use crate::encoding::FieldValueError;
12use crate::error::SetGroupsError;
13use crate::{FieldType, HardCodedFixFieldDefinition};
14pub(crate) use body::Body;
15pub(crate) use header::Header;
16pub use repeating_group::RepeatingGroup;
17pub(crate) use trailer::Trailer;
18
19pub trait Part {
20    fn get_field_map(&self) -> &FieldMap;
21    fn get_field_map_mut(&mut self) -> &mut FieldMap;
22
23    fn set<'a, V>(&'a mut self, field_definition: &HardCodedFixFieldDefinition, value: V)
24    where
25        V: FieldType<'a>,
26    {
27        let field = Field::new(field_definition.tag(), value.to_bytes());
28        self.store_field(field);
29    }
30
31    fn store_field(&mut self, field: Field) {
32        self.get_field_map_mut().insert(field)
33    }
34
35    #[inline]
36    fn get<'a, V>(
37        &'a self,
38        field: &HardCodedFixFieldDefinition,
39    ) -> Result<V, FieldValueError<V::Error>>
40    where
41        V: FieldType<'a>,
42    {
43        self.get_raw(field)
44            .map(V::deserialize)
45            .transpose()
46            .map_err(FieldValueError::Invalid)
47            .and_then(|opt| opt.ok_or(FieldValueError::Missing))
48    }
49
50    #[inline]
51    fn get_raw(&self, field: &HardCodedFixFieldDefinition) -> Option<&[u8]> {
52        self.get_field_map().get_raw(field.tag())
53    }
54
55    fn pop(&mut self, field: &HardCodedFixFieldDefinition) -> Option<Field> {
56        self.get_field_map_mut().fields.shift_remove(&field.tag())
57    }
58
59    fn set_groups(&mut self, groups: Vec<RepeatingGroup>) -> Result<(), SetGroupsError> {
60        let tags: HashSet<(TagU32, TagU32)> = groups
61            .iter()
62            .map(|g| (g.start_tag, g.delimiter_tag))
63            .collect();
64        let (start_tag, _) = &tags
65            .iter()
66            .next()
67            .ok_or_else(|| SetGroupsError::EmptyGroups)?;
68        if tags.len() > 1 {
69            return Err(SetGroupsError::MultipleStartTagsAndDelimiters(tags));
70        }
71        self.get_field_map_mut().set_groups(*start_tag, groups);
72
73        Ok(())
74    }
75
76    fn get_group(&self, start_tag: TagU32, index: usize) -> Option<&RepeatingGroup> {
77        self.get_field_map().get_group(start_tag, index)
78    }
79
80    fn calculate_length(&self) -> usize {
81        self.get_field_map().calculate_length(&[])
82    }
83}