use crate::error::DecodeError;
use crate::field_map::FieldMap;
use crate::tags::{BEGIN_STRING, MSG_TYPE};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Message {
pub header: FieldMap,
pub body: FieldMap,
pub trailer: FieldMap,
pub(crate) fields_out_of_order: bool,
}
impl Message {
pub fn new() -> Self {
Self::default()
}
pub fn begin_string(&self) -> Option<&str> {
self.header.get(BEGIN_STRING).and_then(|f| f.as_str().ok())
}
pub fn fields_out_of_order(&self) -> bool {
self.fields_out_of_order
}
pub fn msg_type(&self) -> Option<&str> {
self.header.get(MSG_TYPE).and_then(|f| f.as_str().ok())
}
pub fn encode(&self) -> Vec<u8> {
crate::codec::encode(self)
}
pub fn decode(bytes: &[u8]) -> Result<Self, DecodeError> {
crate::codec::decode(bytes)
}
pub fn reverse_route(&mut self, original: &Message) {
for (on_behalf_of, deliver_to) in [(115u32, 128u32), (116, 129), (144, 145)] {
if let Some(f) = original.header.get(on_behalf_of) {
self.header.set(crate::field::Field::new(
deliver_to,
f.value_bytes().to_vec(),
));
}
if let Some(f) = original.header.get(deliver_to) {
self.header.set(crate::field::Field::new(
on_behalf_of,
f.value_bytes().to_vec(),
));
}
}
}
}