Skip to main content

fixer/
field.rs

1use crate::errors::MessageRejectErrorEnum;
2use crate::{field_map::LocalField, tag::Tag};
3use simple_error::SimpleResult;
4
5pub trait FieldTag {
6    fn tag(&self) -> Tag;
7}
8
9// FieldValueWriter is an interface for writing field values
10pub trait FieldValueWriter {
11    // write_to appends the field value bytes to buf
12    fn write_to(&self, buf: &mut Vec<u8>);
13
14    // write returns the field value as a new Vec<u8>
15    fn write(&self) -> Vec<u8> {
16        let mut buf = Vec::new();
17        self.write_to(&mut buf);
18        buf
19    }
20}
21
22// FieldValueReader is an interface for reading field values
23pub trait FieldValueReader {
24    // read reads the contents of the []byte into FieldValue.
25    // returns an error if there are issues in the data processing
26    fn read(&mut self, input: &[u8]) -> SimpleResult<()>;
27}
28
29// The FieldValue interface is used to write/extract typed field values to/from raw bytes
30pub trait FieldValue: FieldValueWriter + FieldValueReader {}
31
32// FieldWriter is an interface for a writing a field
33pub trait FieldWriter: FieldValueWriter + FieldTag {}
34
35// Field is the interface implemented by all typed Fields in a Message
36pub trait Field: FieldWriter + FieldValueReader {}
37
38// FieldGroupWriter is an interface for writing a FieldGroup
39pub trait FieldGroupWriter: FieldTag {
40    fn write(&self) -> LocalField;
41}
42
43// FieldGroupReader is an interface for reading a FieldGroup
44pub trait FieldGroupReader: FieldTag {
45    fn read(&mut self, tag_value: LocalField) -> Result<LocalField, MessageRejectErrorEnum>;
46}
47
48// FieldGroup is the interface implemented by all typed Groups in a Message
49pub trait FieldGroup: FieldTag + FieldGroupWriter + FieldGroupReader {}