trafix_codec/message/field/value/
begin_string.rs

1//! Defines the [`BeginString`] enumeration, representing the FIX **8 `BeginString`**
2//! field value.
3
4// TODO(kfejzic): Limit visibility to crate once standards are introduced.
5
6use crate::message::field::value::FromFixBytes;
7
8/// Represents the FIX protocol version (`8`) field value.
9///
10/// This field value determines the message format and version-specific rules
11/// that apply to subsequent tags in the message.
12#[derive(Clone, Copy, Debug, PartialEq)]
13pub enum BeginString {
14    /// FIX.4.4 protocol version (`8=FIX.4.4`).
15    FIX44,
16}
17
18impl BeginString {
19    /// Returns the tag used for [`BeginString`].
20    #[must_use]
21    pub const fn tag() -> u16 {
22        8
23    }
24}
25
26impl From<BeginString> for &'static [u8] {
27    /// Converts a [`BeginString`] variant into its **static byte slice**
28    /// representation.
29    ///
30    /// This form avoids allocation and is suitable for direct use
31    /// when writing FIX messages to a buffer or network stream.
32    ///
33    /// Example usage:
34    /// ```
35    /// use trafix_codec::message::field::value::begin_string::BeginString;
36    /// let bytes: &'static [u8] = BeginString::FIX44.into();
37    /// assert_eq!(bytes, b"FIX.4.4");
38    /// ```
39    fn from(val: BeginString) -> Self {
40        match val {
41            BeginString::FIX44 => b"FIX.4.4",
42        }
43    }
44}
45
46impl From<BeginString> for Vec<u8> {
47    /// Converts a [`BeginString`] variant into an **owned `Vec<u8>`**
48    /// containing its byte representation.
49    ///
50    /// Example usage:
51    /// ```
52    /// use trafix_codec::message::field::value::begin_string::BeginString;
53    /// let bytes: Vec<u8> = BeginString::FIX44.into();
54    /// assert_eq!(bytes, b"FIX.4.4");
55    /// ```
56    fn from(val: BeginString) -> Self {
57        <&[u8]>::from(val).to_vec()
58    }
59}
60
61/// The error type for failed parsing of [`MsgType`]
62#[derive(Debug, Clone, PartialEq, thiserror::Error)]
63pub enum ParseError<'input> {
64    /// Provided byte slice contains data that is not a valid or supported FIX version.
65    #[error("unsupported fix version: {}", String::from_utf8_lossy(.0))]
66    Unsupported(&'input [u8]),
67}
68
69impl FromFixBytes for BeginString {
70    type Error<'input> = ParseError<'input>;
71
72    fn from_fix_bytes(bytes: &[u8]) -> Result<Self, Self::Error<'_>>
73    where
74        Self: Sized,
75    {
76        if bytes == <&[u8]>::from(BeginString::FIX44) {
77            Ok(BeginString::FIX44)
78        } else {
79            Err(ParseError::Unsupported(bytes))
80        }
81    }
82}