Skip to main content

refix_dictionary/quickfix/
error.rs

1use crate::{MemberContext, dictionary, transport};
2use std::fmt;
3
4/// A defect that prevents producing a dictionary at all.
5#[derive(Debug)]
6pub enum Error {
7    Xml(roxmltree::Error),
8    UnexpectedRoot(String),
9    UnknownProtocol(String),
10    MissingAttribute {
11        element: String,
12        attribute: String,
13    },
14    InvalidNumber {
15        element: String,
16        attribute: String,
17        value: String,
18    },
19    UnknownField {
20        context: MemberContext,
21        field: String,
22    },
23    DuplicateField {
24        field: String,
25    },
26    InvalidAttribute {
27        element: String,
28        attribute: String,
29        value: String,
30    },
31    /// A data field listed after different LENGTH fields.
32    ConflictingLengthFields {
33        field: String,
34        first: String,
35        second: String,
36    },
37    Invalid(dictionary::Error),
38    /// A defect in the transport dictionary.
39    Transport(Box<Error>),
40    /// An application dictionary and a transport dictionary that cannot be
41    /// joined.
42    Unjoinable(transport::Error),
43}
44
45impl fmt::Display for Error {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match self {
48            Error::Xml(error) => write!(f, "malformed xml: {error}"),
49            Error::UnexpectedRoot(root) => {
50                write!(f, "unexpected root element <{root}>, expected <fix>")
51            }
52            Error::UnknownProtocol(protocol) => {
53                write!(
54                    f,
55                    "unknown protocol type '{protocol}', expected FIX or FIXT"
56                )
57            }
58            Error::MissingAttribute { element, attribute } => {
59                write!(f, "missing attribute '{attribute}' on <{element}>")
60            }
61            Error::InvalidNumber {
62                element,
63                attribute,
64                value,
65            } => {
66                write!(
67                    f,
68                    "invalid number '{value}' in attribute '{attribute}' of <{element}>"
69                )
70            }
71            Error::UnknownField { context, field } => {
72                write!(f, "{context} references unknown field '{field}'")
73            }
74            Error::DuplicateField { field } => {
75                write!(f, "field '{field}' is defined more than once")
76            }
77            Error::InvalidAttribute {
78                element,
79                attribute,
80                value,
81            } => {
82                write!(
83                    f,
84                    "invalid value '{value}' in attribute '{attribute}' of <{element}>"
85                )
86            }
87            Error::ConflictingLengthFields {
88                field,
89                first,
90                second,
91            } => {
92                write!(
93                    f,
94                    "field '{field}' takes its length from '{first}' in one place and '{second}' in another"
95                )
96            }
97            Error::Invalid(error) => {
98                write!(f, "invalid dictionary: {error}")
99            }
100            Error::Transport(error) => write!(f, "in the transport dictionary: {error}"),
101            Error::Unjoinable(error) => write!(f, "{error}"),
102        }
103    }
104}
105
106impl std::error::Error for Error {
107    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
108        match self {
109            Error::Xml(error) => Some(error),
110            Error::Invalid(error) => Some(error),
111            Error::Transport(error) => Some(error),
112            Error::Unjoinable(error) => Some(error),
113            _ => None,
114        }
115    }
116}