Skip to main content

Serializer

Struct Serializer 

Source
pub struct Serializer<'a> { /* private fields */ }
Expand description

Writes FIX tag-value fields into a caller-provided byte buffer.

The buffer is the message size limit: nothing is ever allocated, and a message that does not fit fails with SerializeError::MaxMessageSizeExceeded rather than growing. Size the buffer for the largest message the session may emit.

A field is three calls - tag prefix, typed value, delimiter:

let mut buf = [0u8; 64];
let mut serializer = Serializer::new(&mut buf);
serializer.put_slice(b"49=")?;
serializer.serialize_string(fix_str!("SENDER"))?;
serializer.put_soh()?;
assert_eq!(serializer.written(), b"49=SENDER\x01");

A whole message additionally brackets the body with serialize_body_len (which reserves the BodyLength(9) placeholder) and serialize_checksum (which patches that placeholder and appends CheckSum(10)).

Implementations§

Source§

impl<'a> Serializer<'a>

Source

pub fn new(output: &'a mut [u8]) -> Serializer<'a>

Wraps output, whose length becomes this message’s size limit.

Source

pub fn pos(&self) -> usize

Number of bytes written so far.

Source

pub fn written(&self) -> &[u8] ⓘ

The bytes written so far.

Source

pub fn put_slice(&mut self, bytes: &[u8]) -> Result<(), SerializeError>

Append raw bytes.

Source

pub fn put_u8(&mut self, byte: u8) -> Result<(), SerializeError>

Append a single byte.

Source

pub fn put_soh(&mut self) -> Result<(), SerializeError>

Write the FIX field delimiter (SOH, 0x01).

Source

pub fn serialize_body_len(&mut self) -> Result<(), SerializeError>

Write the BodyLength(9) field as an all-zero placeholder and mark the body start.

The placeholder width comes from the buffer size, not from the message, so the real length is patched in later by serialize_checksum. Call this once, directly after BeginString(8).

Source

pub fn serialize_checksum(&mut self) -> Result<(), SerializeError>

Close the message: patch the real body length into the placeholder, then append CheckSum(10) over every byte written before it.

Returns SerializeError::MaxMessageSizeExceeded when the body length needs more digits than the placeholder reserved - including the case where serialize_body_len was never called, which leaves no placeholder to patch.

Source

pub fn serialize_tag_num( &mut self, tag_num: &TagNum, ) -> Result<(), SerializeError>

Serialize sequence of character digits without commas or decimals. Value must be positive and may not contain leading zeros.

Source

pub fn serialize_int(&mut self, int: &Int) -> Result<(), SerializeError>

Serialize sequence of character digits without commas or decimals and optional sign character (characters - and 0 - 9). The sign character utilizes one octet (i.e., positive int is 99999 while negative int is -99999).

Source

pub fn serialize_seq_num( &mut self, seq_num: &SeqNum, ) -> Result<(), SerializeError>

Serialize sequence of character digits without commas or decimals.

Zero is a legal value here, unlike for the other digit types. Several SeqNum-typed fields give it a meaning of their own: EndSeqNo(16) and ApplEndSeqNum(1183) use it for “no upper bound”, and LastMsgSeqNumProcessed(369) for “nothing processed yet”. Which zeros are legal is a per-tag question the codec has no way to answer - Deserializer::deserialize_seq_num already accepts every one of them, and rejecting them on the way out would leave the codec able to read messages it cannot write.

Sequence-number validity belongs to the session layer, which owns the counters and checks them against protocol state.

Source

pub fn serialize_num_in_group( &mut self, num_in_group: &NumInGroup, ) -> Result<(), SerializeError>

Serialize sequence of character digits without commas or decimals. Value must be positive.

Source

pub fn serialize_day_of_month( &mut self, day_of_month: DayOfMonth, ) -> Result<(), SerializeError>

Serialize sequence of character digits without commas or decimals (values 1 to 31).

Source

pub fn serialize_float(&mut self, float: &Float) -> Result<(), SerializeError>

Serialize sequence of character digits with optional decimal point and sign character (characters -, 0 - 9 and .); the absence of the decimal point within the string will be interpreted as the float representation of an integer value. Note that float values may contain leading zeros (e.g. 00023.23 = 23.23) and may contain or omit trailing zeros after the decimal point (e.g. 23.0 = 23.0000 = 23 = 23.).

All float fields must accommodate up to fifteen significant digits. The number of decimal places used should be a factor of business/market needs and mutual agreement between counterparties.

Source

pub fn serialize_qty(&mut self, qty: &Qty) -> Result<(), SerializeError>

Serialize a Qty - the Float grammar under a different name.

Source

pub fn serialize_price(&mut self, price: &Price) -> Result<(), SerializeError>

Serialize a Price - the Float grammar under a different name.

Source

pub fn serialize_price_offset( &mut self, price_offset: &PriceOffset, ) -> Result<(), SerializeError>

Serialize a PriceOffset - the Float grammar under a different name.

Source

pub fn serialize_amt(&mut self, amt: &Amt) -> Result<(), SerializeError>

Serialize an Amt - the Float grammar under a different name.

Source

pub fn serialize_percentage( &mut self, percentage: &Percentage, ) -> Result<(), SerializeError>

Serialize a Percentage - the Float grammar under a different name.

Source

pub fn serialize_boolean( &mut self, boolean: &Boolean, ) -> Result<(), SerializeError>

Serialize a single-character boolean: Y for true, N for false.

Source

pub fn serialize_char(&mut self, c: &Char) -> Result<(), SerializeError>

Use any ASCII character except control characters.

Source

pub fn serialize_multiple_char_value( &mut self, mcv: &MultipleCharValue, ) -> Result<(), SerializeError>

Serialize string containing one or more space-delimited single character values, e.g. 2 A F.

Source

pub fn serialize_string(&mut self, input: &FixStr) -> Result<(), SerializeError>

Serialize alphanumeric free-format strings can include any character except control characters.

Source

pub fn serialize_multiple_string_value( &mut self, input: &MultipleStringValue, ) -> Result<(), SerializeError>

Serialize string containing one or more space-delimited multiple character values, e.g. AV AN A.

Source

pub fn serialize_country( &mut self, country: &Country, ) -> Result<(), SerializeError>

Serialize ISO 3166-1:2013 Codes for the representation of names of countries and their subdivision (2-character code).

Returns SerializeError::EmptyValue when the country is unspecified.

Source

pub fn serialize_currency( &mut self, currency: &Currency, ) -> Result<(), SerializeError>

Serialize ISO 4217:2015 Codes for the representation of currencies and funds (3-character code).

Returns SerializeError::EmptyValue when the currency is unspecified.

Source

pub fn serialize_exchange( &mut self, exchange: &Exchange, ) -> Result<(), SerializeError>

Serialize ISO 10383:2012 Securities and related financial instruments - Codes for exchanges and market identification (MIC) (4-character code).

Returns SerializeError::EmptyValue when the code is not set.

Source

pub fn serialize_month_year( &mut self, input: &MonthYear, ) -> Result<(), SerializeError>

Serialize string representing month of a year. An optional day of the month can be appended or an optional week code.

§Valid formats:

YYYYMM YYYYMMDD YYYYMMWW

§Valid values:

YYYY = 0000-9999; MM = 01-12; DD = 01-31; WW = w1, w2, w3, w4, w5.

Source

pub fn serialize_language( &mut self, input: &Language, ) -> Result<(), SerializeError>

Serialize ISO 639-1:2002 Codes for the representation of names of languages (2-character code).

Returns SerializeError::EmptyValue when the language is unspecified.

Source

pub fn serialize_utc_timestamp( &mut self, input: &UtcTimestamp, ) -> Result<(), SerializeError>

Serialize string representing time/date combination represented in UTC (Universal Time Coordinated) in either YYYYMMDD-HH:MM:SS (whole seconds) or YYYYMMDD-HH:MM:SS.sss* format, colons, dash, and period required.

§Valid values:
  • YYYY = 0000-9999,
  • MM = 01-12,
  • DD = 01-31,
  • HH = 00-23,
  • MM = 0059,
  • SS = 00-60 (60 only if UTC leap second),
  • sss* fractions of seconds. The fractions of seconds may be empty when no fractions of seconds are conveyed (in such a case the period is not conveyed), it may include 3 digits to convey milliseconds, 6 digits to convey microseconds, 9 digits to convey nanoseconds, 12 digits to convey picoseconds;
Source

pub fn serialize_utc_time_only( &mut self, input: &UtcTimeOnly, ) -> Result<(), SerializeError>

Serialize string representing time-only represented in UTC (Universal Time Coordinated) in either HH:MM:SS (whole seconds) or HH:MM:SS.sss* (milliseconds) format, colons, and period required.

This special-purpose field is paired with UTCDateOnly to form a proper UTCTimestamp for bandwidth-sensitive messages.

§Valid values:
  • HH = 00-23,
  • MM = 00-59,
  • SS = 00-60 (60 only if UTC leap second),
  • sss* fractions of seconds. The fractions of seconds may be empty when no fractions of seconds are conveyed (in such a case the period is not conveyed), it may include 3 digits to convey milliseconds, 6 digits to convey microseconds, 9 digits to convey nanoseconds, 12 digits to convey picoseconds;
Source

pub fn serialize_utc_date_only( &mut self, input: &UtcDateOnly, ) -> Result<(), SerializeError>

Serialize date represented in UTC (Universal Time Coordinated) in YYYYMMDD format.

§Valid values:
  • YYYY = 0000-9999,
  • MM = 01-12,
  • DD = 01-31.
Source

pub fn serialize_local_mkt_time( &mut self, input: &LocalMktTime, ) -> Result<(), SerializeError>

Serialize time local to a market center. Used where offset to UTC varies throughout the year and the defining market center is identified in a corresponding field.

Format is HH:MM:SS where:

  • HH = 00-23 hours,
  • MM = 00-59 minutes,
  • SS = 00-59 seconds.

In general only the hour token is non-zero.

Source

pub fn serialize_local_mkt_date( &mut self, input: &LocalMktDate, ) -> Result<(), SerializeError>

Serialize date of local market (as opposed to UTC) in YYYYMMDD format.

§Valid values:
  • YYYY = 0000-9999,
  • MM = 01-12,
  • DD = 01-31.
Source

pub fn serialize_tz_timestamp( &mut self, input: &TzTimestamp, ) -> Result<(), SerializeError>

Serialize string representing a time/date combination representing local time with an offset to UTC to allow identification of local time and time zone offset of that time.

The representation is based on ISO 8601.

Format is YYYYMMDD-HH:MM:SS.sss*[Z | [ + | - hh[:mm]]] where:

  • YYYY = 0000 to 9999,
  • MM = 01-12,
  • DD = 01-31 HH = 00-23 hours,
  • MM = 00-59 minutes,
  • SS = 00-59 seconds,
  • hh = 01-12 offset hours,
  • mm = 00-59 offset minutes,
  • sss* fractions of seconds. The fractions of seconds may be empty when no fractions of seconds are conveyed (in such a case the period is not conveyed), it may include 3 digits to convey milliseconds, 6 digits to convey microseconds, 9 digits to convey nanoseconds, 12 digits to convey picoseconds;
Source

pub fn serialize_tz_timeonly( &mut self, input: &TzTimeOnly, ) -> Result<(), SerializeError>

Serialize time of day with timezone. Time represented based on ISO 8601. This is the time with a UTC offset to allow identification of local time and time zone of that time.

Format is HH:MM[:SS][Z | [ + | - hh[:mm]]] where:

  • HH = 00-23 hours,
  • MM = 00-59 minutes,
  • SS = 00-59 seconds,
  • hh = 01-12 offset hours,
  • mm = 00-59 offset minutes.
Source

pub fn serialize_length( &mut self, length: &Length, ) -> Result<(), SerializeError>

Serialize sequence of character digits without commas or decimals.

Source

pub fn serialize_data(&mut self, data: &Data) -> Result<(), SerializeError>

Serialize raw data with no format or content restrictions, or a character string encoded as specified by MessageEncoding(347).

Source

pub fn serialize_xml( &mut self, xml_data: &XmlData, ) -> Result<(), SerializeError>

Serialize XML document.

Source

pub fn serialize_tenor(&mut self, input: &Tenor) -> Result<(), SerializeError>

Serialize a tenor: the unit code (D, M, W or Y) followed by its value, e.g. M3 for three months.

Source

pub fn serialize_enum<T>(&mut self, value: &T) -> Result<(), SerializeError>
where T: Copy + Into<&'static [u8]>,

Serialize an enum as its FIX wire value.

The value is a &'static [u8] held in read-only memory, so this is a single copy with nothing allocated.

Source

pub fn serialize_enum_collection<T>( &mut self, values: &[T], ) -> Result<(), SerializeError>
where T: Copy + Into<&'static [u8]>,

Serialize enums as a space-delimited list of their FIX wire values.

An empty slice is SerializeError::EmptyValue - a FIX field must carry at least one byte.

Trait Implementations§

Source§

impl<'a> Write for Serializer<'a>

Source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more

Auto Trait Implementations§

§

impl<'a> !UnwindSafe for Serializer<'a>

§

impl<'a> Forget for Serializer<'a>

§

impl<'a> Freeze for Serializer<'a>

§

impl<'a> RefUnwindSafe for Serializer<'a>

§

impl<'a> Send for Serializer<'a>

§

impl<'a> Sync for Serializer<'a>

§

impl<'a> Unpin for Serializer<'a>

§

impl<'a> UnsafeUnpin for Serializer<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.