hotfix_encoding/field_access.rs
1use std::iter::FusedIterator;
2use std::ops::Range;
3
4use crate::buffer::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).
131pub trait FieldMap<F> {
132 /// The type returned by [`FieldMap::group`] and
133 /// [`FieldMap::group_opt`].
134 type Group: RepeatingGroup<Entry = Self>;
135
136 /// Looks for a `field` within `self` and then returns its raw byte
137 /// contents, if it exists.
138 fn get_raw(&self, field: F) -> Option<&[u8]>;
139
140 /// Looks for a group that starts with `field` within `self`.
141 fn group(&self, field: F) -> Result<Self::Group, FieldValueError<<usize as FieldType>::Error>>;
142
143 /// Like [`FieldMap::group`], but doesn't return an [`Err`] if the
144 /// group is missing.
145 #[inline]
146 fn group_opt(&self, field: F) -> Result<Option<Self::Group>, <usize as FieldType>::Error> {
147 match self.group(field) {
148 Ok(group) => Ok(Some(group)),
149 Err(FieldValueError::Missing) => Ok(None),
150 Err(FieldValueError::Invalid(e)) => Err(e),
151 }
152 }
153
154 /// Looks for a `field` within `self` and then decodes its raw byte contents
155 /// via [`FieldType::deserialize`], if found.
156 #[inline]
157 fn get<'a, V>(&'a self, field: F) -> Result<V, FieldValueError<V::Error>>
158 where
159 V: FieldType<'a>,
160 {
161 self.get_opt(field)
162 .map_err(FieldValueError::Invalid)
163 .and_then(|opt| opt.ok_or(FieldValueError::Missing))
164 }
165
166 /// Like [`FieldMap::get`], but with lossy deserialization.
167 #[inline]
168 fn get_lossy<'a, V>(&'a self, field: F) -> Result<V, FieldValueError<V::Error>>
169 where
170 V: FieldType<'a>,
171 {
172 self.get_lossy_opt(field)
173 .map_err(FieldValueError::Invalid)
174 .and_then(|opt| opt.ok_or(FieldValueError::Missing))
175 }
176
177 /// Like [`FieldMap::get`], but doesn't return an [`Err`] if `field`
178 /// is missing.
179 #[inline]
180 fn get_opt<'a, V>(&'a self, field: F) -> Result<Option<V>, V::Error>
181 where
182 V: FieldType<'a>,
183 {
184 self.get_raw(field).map(V::deserialize).transpose()
185 }
186
187 /// Like [`FieldMap::get_opt`], but with lossy deserialization.
188 #[inline]
189 fn get_lossy_opt<'a, V>(&'a self, field: F) -> Result<Option<V>, V::Error>
190 where
191 V: FieldType<'a>,
192 {
193 self.get_raw(field).map(V::deserialize_lossy).transpose()
194 }
195}
196
197/// Provides access to entries within a FIX repeating group.
198pub trait RepeatingGroup: Sized {
199 /// The type of entries in this FIX repeating group. Must implement
200 /// [`FieldMap`].
201 type Entry;
202
203 /// Returns the number of FIX group entries in `self`.
204 fn len(&self) -> usize;
205
206 fn is_empty(&self) -> bool;
207
208 /// Returns the `i` -th entry in `self`, if present.
209 fn get(&self, i: usize) -> Option<Self::Entry>;
210
211 /// Creates and returns an [`Iterator`] over the entries in `self`.
212 /// Iteration MUST be done in sequential order, i.e. in which they appear in
213 /// the original FIX message.
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}