1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use std::str::FromStr;

use quick_xml::events::BytesStart;

use crate::{read_attribute, FixSpecError};

/// Message dest.
#[derive(Debug, Clone, Copy)]
pub enum MessageCategory {
    /// Message is targeting application level.
    App,
    /// Message is related to protocol / admin / technical task.
    Admin,
}

impl MessageCategory {
    /// Convert value to a static string.
    ///
    /// Mostly useful for debugging / display purpose.
    pub const fn as_static_str(&self) -> &'static str {
        match self {
            MessageCategory::App => "app",
            MessageCategory::Admin => "admin",
        }
    }

    pub(crate) fn parse_xml_element(element: &BytesStart) -> Result<Self, FixSpecError> {
        let item = read_attribute(element, "msgcat")?.parse()?;
        Ok(item)
    }
}

impl FromStr for MessageCategory {
    type Err = FixSpecError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        match input {
            "admin" => Ok(Self::Admin),
            "app" => Ok(Self::App),
            x => Err(FixSpecError::InvalidContent(format!("invalid msgcat: {x}"))),
        }
    }
}