hotfix_message/encoding/
field_access.rs

1use std::iter::FusedIterator;
2use std::ops::Range;
3
4use crate::encoding::Buffer;
5
6/// Either a field that is missing or has an invalid value.
7#[derive(Debug, thiserror::Error)]
8pub enum FieldValueError<E> {
9    /// No such field was found.
10    #[error("Missing field tag")]
11    Missing,
12    /// The field was found, but can't be parsed.
13    #[error("Invalid field value: {0}")]
14    Invalid(#[from] E),
15}
16
17impl<E> PartialEq<FieldValueError<E>> for FieldValueError<E> {
18    fn eq(&self, other: &FieldValueError<E>) -> bool {
19        matches!(
20            (self, other),
21            (FieldValueError::Missing, FieldValueError::Missing)
22        )
23    }
24}
25
26impl<E> From<Option<E>> for FieldValueError<E> {
27    fn from(e: Option<E>) -> Self {
28        match e {
29            Some(e) => FieldValueError::Invalid(e),
30            None => FieldValueError::Missing,
31        }
32    }
33}
34
35/// Provides (de)serialization logic for a Rust type as FIX field values.
36///
37/// See the [`field_types`](crate::field_types) module for more information.
38pub trait FieldType<'a>
39where
40    Self: Sized,
41{
42    /// The error type that can arise during deserialization.
43    type Error;
44    /// A type with values that customize the serialization algorithm, e.g.
45    /// padding information.
46    type SerializeSettings: Default;
47
48    /// Writes `self` to `buffer` using default settings.
49    #[inline]
50    fn serialize<B>(&self, buffer: &mut B) -> usize
51    where
52        B: Buffer,
53    {
54        self.serialize_with(buffer, Self::SerializeSettings::default())
55    }
56
57    /// Writes `self` to `buffer` using custom serialization `settings`.
58    fn serialize_with<B>(&self, buffer: &mut B, settings: Self::SerializeSettings) -> usize
59    where
60        B: Buffer;
61
62    /// Parses and deserializes from `data`.
63    fn deserialize(data: &'a [u8]) -> Result<Self, Self::Error>;
64
65    /// Like [`FieldType::deserialize`], but it's allowed to skip *some* amount of
66    /// input checking. Invalid inputs might not trigger errors and instead be
67    /// deserialized as random values.
68    ///
69    /// # Safety
70    ///
71    /// This method remains 100% safe even on malformed inputs.
72    fn deserialize_lossy(data: &'a [u8]) -> Result<Self, Self::Error> {
73        Self::deserialize(data)
74    }
75
76    /// Serializes `self` to a [`Vec`] of bytes, allocated on the fly.
77    fn to_bytes(&self) -> Vec<u8> {
78        let mut buffer = Vec::new();
79        self.serialize(&mut buffer);
80        buffer
81    }
82
83    /// Allocates a [`String`] representation of `self`, using [`FieldType::to_bytes`].
84    ///
85    /// # Panics
86    ///
87    /// This function will panic if the underlying byte representation is not
88    /// valid UTF-8. As such, you should only *ever* use this function for
89    /// [`FieldType`] implementors that are guaranteed to be representable with
90    /// valid UTF-8 (like numbers with ASCII digits).
91    fn to_string(&self) -> String {
92        String::from_utf8(self.to_bytes()).expect("Invalid UTF-8 representation of FIX field.")
93    }
94}
95
96/// Provides random (i.e. non-sequential) access to FIX fields and groups within
97/// messages.
98///
99/// # Methods
100///
101/// [`FieldMap`] provides two kinds of methods:
102///
103/// 1. Group getters: [`FieldMap::group`] and
104///    [`FieldMap::group_opt`].
105///
106/// 2. Field getters: [`FieldMap::get_raw`], [`FieldMap::get`],
107///    etc..
108///
109/// The most basic form of field access is done via
110/// [`FieldMap::get_raw`], which performs no deserialization at all: it
111/// simply returns the bytes contents associated with a FIX field, if found.
112///
113/// Building upon [`FieldMap::get_raw`] and [`FieldType`], the other
114/// field access methods all provide some utility deserialization logic. These
115/// methods all have the `get_` prefix, with the following considerations:
116///
117/// - `get_lossy` methods perform "lossy" deserialization via
118///   [`FieldType::deserialize_lossy`]. Unlike lossless deserialization, these
119///   methods may skip some error checking logic and thus prove to be faster.
120///   Memory-safety is still guaranteed, but malformed FIX fields won't be
121///   detected 100% of the time.
122/// - `get_opt` methods work exactly like their non-`_opt` counterparties, but they
123///   have a different return type: instead of returning [`Err(None)`] for missing
124///   fields, these methods return [`None`] for missing fields and
125///   [`Some(Ok(field))`] for existing fields.
126///
127/// # Type parameters
128///
129/// This trait is generic over a type `F`, which must univocally identify FIX
130/// fields (besides FIX repeating groups, which allow repetitions).
131#[allow(dead_code)]
132pub trait FieldMap<F> {
133    /// The type returned by [`FieldMap::group`] and
134    /// [`FieldMap::group_opt`].
135    type Group: RepeatingGroup<Entry = Self>;
136
137    /// Looks for a `field` within `self` and then returns its raw byte
138    /// contents, if it exists.
139    fn get_raw(&self, field: F) -> Option<&[u8]>;
140
141    /// Looks for a group that starts with `field` within `self`.
142    fn group(&self, field: F) -> Result<Self::Group, FieldValueError<<usize as FieldType>::Error>>;
143
144    /// Like [`FieldMap::group`], but doesn't return an [`Err`] if the
145    /// group is missing.
146    #[inline]
147    fn group_opt(&self, field: F) -> Result<Option<Self::Group>, <usize as FieldType>::Error> {
148        match self.group(field) {
149            Ok(group) => Ok(Some(group)),
150            Err(FieldValueError::Missing) => Ok(None),
151            Err(FieldValueError::Invalid(e)) => Err(e),
152        }
153    }
154
155    /// Looks for a `field` within `self` and then decodes its raw byte contents
156    /// via [`FieldType::deserialize`], if found.
157    #[inline]
158    fn get<'a, V>(&'a self, field: F) -> Result<V, FieldValueError<V::Error>>
159    where
160        V: FieldType<'a>,
161    {
162        self.get_opt(field)
163            .map_err(FieldValueError::Invalid)
164            .and_then(|opt| opt.ok_or(FieldValueError::Missing))
165    }
166
167    /// Like [`FieldMap::get`], but with lossy deserialization.
168    #[inline]
169    fn get_lossy<'a, V>(&'a self, field: F) -> Result<V, FieldValueError<V::Error>>
170    where
171        V: FieldType<'a>,
172    {
173        self.get_lossy_opt(field)
174            .map_err(FieldValueError::Invalid)
175            .and_then(|opt| opt.ok_or(FieldValueError::Missing))
176    }
177
178    /// Like [`FieldMap::get`], but doesn't return an [`Err`] if `field`
179    /// is missing.
180    #[inline]
181    fn get_opt<'a, V>(&'a self, field: F) -> Result<Option<V>, V::Error>
182    where
183        V: FieldType<'a>,
184    {
185        self.get_raw(field).map(V::deserialize).transpose()
186    }
187
188    /// Like [`FieldMap::get_opt`], but with lossy deserialization.
189    #[inline]
190    fn get_lossy_opt<'a, V>(&'a self, field: F) -> Result<Option<V>, V::Error>
191    where
192        V: FieldType<'a>,
193    {
194        self.get_raw(field).map(V::deserialize_lossy).transpose()
195    }
196}
197
198/// Provides access to entries within a FIX repeating group.
199pub trait RepeatingGroup: Sized {
200    /// The type of entries in this FIX repeating group. Must implement
201    /// [`FieldMap`].
202    type Entry;
203
204    /// Returns the number of FIX group entries in `self`.
205    fn len(&self) -> usize;
206
207    /// Returns the `i` -th entry in `self`, if present.
208    fn get(&self, i: usize) -> Option<Self::Entry>;
209
210    /// Creates and returns an [`Iterator`] over the entries in `self`.
211    /// Iteration MUST be done in sequential order, i.e. in which they appear in
212    /// the original FIX message.
213    #[allow(dead_code)]
214    fn entries(&self) -> GroupEntries<Self> {
215        GroupEntries {
216            group: self,
217            range: 0..self.len(),
218        }
219    }
220}
221
222/// An [`Iterator`] over the entries of a FIX repeating group.
223///
224/// This `struct` is created by the method [`RepeatingGroup::entries`]. It
225/// also implements [`FusedIterator`], [`DoubleEndedIterator`], and
226/// [`ExactSizeIterator`].
227#[derive(Debug, Clone)]
228pub struct GroupEntries<'a, G> {
229    group: &'a G,
230    range: Range<usize>,
231}
232
233impl<'a, G> Iterator for GroupEntries<'a, G>
234where
235    G: RepeatingGroup,
236{
237    type Item = G::Entry;
238
239    fn next(&mut self) -> Option<Self::Item> {
240        let i = self.range.next()?;
241        self.group.get(i)
242    }
243
244    fn size_hint(&self) -> (usize, Option<usize>) {
245        self.range.size_hint()
246    }
247}
248
249impl<'a, G> FusedIterator for GroupEntries<'a, G> where G: RepeatingGroup {}
250impl<'a, G> ExactSizeIterator for GroupEntries<'a, G> where G: RepeatingGroup {}
251
252impl<'a, G> DoubleEndedIterator for GroupEntries<'a, G>
253where
254    G: RepeatingGroup,
255{
256    fn next_back(&mut self) -> Option<Self::Item> {
257        let i = self.range.next_back()?;
258        self.group.get(i)
259    }
260}