fixer 0.11.0

A full featured messaging engine for the FIX protocol implemented in Rust
Documentation
use crate::field::{FieldValue, FieldValueReader, FieldValueWriter};
use rust_decimal::Decimal;
use simple_error::{SimpleError, SimpleResult};

// FIXDecimal is a FIX Float Value that implements an arbitrary precision fixed-point decimal.  Implements FieldValue
pub struct FIXDecimal {
    pub decimal: Decimal,
    pub scale: i32,
}

impl FieldValueReader for FIXDecimal {
    fn read(&mut self, input: &[u8]) -> SimpleResult<()> {
        let input_str = std::str::from_utf8(input).map_err(SimpleError::from)?;
        let fix_decimal = Decimal::from_str_exact(input_str).map_err(SimpleError::from)?;
        self.decimal = fix_decimal;
        Ok(())
    }
}

impl FieldValueWriter for FIXDecimal {
    fn write_to(&self, buf: &mut Vec<u8>) {
        let scale: u32 = self.scale.try_into().unwrap();
        let mut rounded = self.decimal.round_dp(scale);
        // Use rescale to ensure exactly `scale` decimal places (matching Go's StringFixed),
        // which pads trailing zeros when scale > actual precision.
        rounded.rescale(scale);
        buf.extend_from_slice(rounded.to_string().as_bytes());
    }
}

impl FieldValue for FIXDecimal {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fix_decimal_write() {
        struct TestCase {
            decimal: FIXDecimal,
            expected: String,
        }
        let tests = vec![
            TestCase {
                decimal: FIXDecimal {
                    decimal: Decimal::new(-1_243_456, 4),
                    scale: 4,
                },
                expected: String::from("-124.3456"),
            },
            TestCase {
                decimal: FIXDecimal {
                    decimal: Decimal::new(-1_243_456, 4),
                    scale: 5,
                },
                expected: String::from("-124.34560"),
            },
            TestCase {
                decimal: FIXDecimal {
                    decimal: Decimal::new(-1_243_456, 4),
                    scale: 0,
                },
                expected: String::from("-124"),
            },
        ];

        for test in &tests {
            let b = test.decimal.write();
            assert_eq!(test.expected.as_bytes(), b);
        }
    }

    #[test]
    fn test_fix_decimal_read() {
        struct TestCase {
            bytes: String,
            expected: Decimal,
            expect_error: bool,
        }
        let tests = [TestCase {
                bytes: String::from("15"),
                expected: Decimal::new(15, 0),
                expect_error: false,
            },
            TestCase {
                bytes: String::from("15.000"),
                expected: Decimal::new(15, 0),
                expect_error: false,
            },
            TestCase {
                bytes: String::from("15.001"),
                expected: Decimal::new(15001, 3),
                expect_error: false,
            },
            TestCase {
                bytes: String::from("-15.001"),
                expected: Decimal::new(-15001, 3),
                expect_error: false,
            },
            TestCase {
                bytes: String::from("blah"),
                expected: Decimal::default(),
                expect_error: true,
            },
            TestCase {
                bytes: String::from("+200.00"),
                expected: Decimal::new(200, 0),
                expect_error: false,
            }];

        for test in &tests {
            let decimal_result = Decimal::from_str_exact(&test.bytes);
            if test.expect_error {
                assert!(decimal_result.is_err());
            } else {
                let decimal = decimal_result.unwrap();
                assert_eq!(
                    test.expected, decimal,
                    "Expected {} got {}",
                    test.expected, decimal,
                );
            }
        }
    }
}