fixer 0.11.0

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

// FIXFloat is a FIX Float Value, implements FieldValue
pub type FIXFloat = f64;

pub trait FIXFloatTrait {
    fn float64(&self) -> f64;
}

impl FIXFloatTrait for FIXFloat {
    fn float64(&self) -> f64 {
        *self
    }
}

impl FieldValueReader for FIXFloat {
    fn read(&mut self, input: &[u8]) -> SimpleResult<()> {
        let f = fast_float2::parse(input).map_err(|_| {
            simple_error!("invalid value {}", std::str::from_utf8(input).unwrap_or("<invalid utf8>"))
        })?;

        for chr in input {
            if *chr != b'.' && *chr != b'-' && !chr.is_ascii_digit() {
                return Err(simple_error!(
                    "invalid value {}",
                    std::str::from_utf8(input).unwrap_or("<invalid utf8>")
                ));
            }
        }

        *self = f;

        Ok(())
    }
}

impl FieldValueWriter for FIXFloat {
    fn write_to(&self, buf: &mut Vec<u8>) {
        use std::io::Write;
        write!(buf, "{self}").unwrap();
    }
}

impl FieldValue for FIXFloat {}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_float_write() {
        struct TestCase<'a> {
            field: FIXFloat,
            val: &'a str,
        }
        let tests = [TestCase {
            field: 5.0,
            val: "5",
        }];
        for test in &tests {
            let b = test.field.write();
            assert_eq!(b, test.val.as_bytes(), "got {:?}; want {}", b, test.val);
        }
    }

    #[test]
    fn test_float_read() {
        struct TestCase<'a> {
            bytes: &'a [u8],
            value: f64,
            expect_error: bool,
        }
        let tests = [
            TestCase {
                bytes: "15".as_bytes(),
                value: 15.0,
                expect_error: false,
            },
            TestCase {
                bytes: "99.9".as_bytes(),
                value: 99.9,
                expect_error: false,
            },
            TestCase {
                bytes: "0.00".as_bytes(),
                value: 0.0,
                expect_error: false,
            },
            TestCase {
                bytes: "-99.9".as_bytes(),
                value: -99.9,
                expect_error: false,
            },
            TestCase {
                bytes: "-99.9.9".as_bytes(),
                value: 0.0,
                expect_error: true,
            },
            TestCase {
                bytes: "blah".as_bytes(),
                value: 0.0,
                expect_error: true,
            },
            TestCase {
                bytes: "1.a1".as_bytes(),
                value: 0.0,
                expect_error: true,
            },
            TestCase {
                bytes: "+200.00".as_bytes(),
                value: 0.0,
                expect_error: true,
            },
        ];
        for test in &tests {
            let mut field = FIXFloat::default();
            let err = field.read(test.bytes);
            assert_eq!(test.expect_error, err.is_err());
            assert!(
                (test.value - field.float64()).abs() < f64::EPSILON,
                "expected {}, got {}",
                test.value,
                field.float64()
            );
        }
    }
}