Skip to main content

refix_dictionary/quickfix/
error.rs

1use crate::{MemberContext, dictionary};
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}
39
40impl fmt::Display for Error {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            Error::Xml(error) => write!(f, "malformed xml: {error}"),
44            Error::UnexpectedRoot(root) => {
45                write!(f, "unexpected root element <{root}>, expected <fix>")
46            }
47            Error::UnknownProtocol(protocol) => {
48                write!(
49                    f,
50                    "unknown protocol type '{protocol}', expected FIX or FIXT"
51                )
52            }
53            Error::MissingAttribute { element, attribute } => {
54                write!(f, "missing attribute '{attribute}' on <{element}>")
55            }
56            Error::InvalidNumber {
57                element,
58                attribute,
59                value,
60            } => {
61                write!(
62                    f,
63                    "invalid number '{value}' in attribute '{attribute}' of <{element}>"
64                )
65            }
66            Error::UnknownField { context, field } => {
67                write!(f, "{context} references unknown field '{field}'")
68            }
69            Error::DuplicateField { field } => {
70                write!(f, "field '{field}' is defined more than once")
71            }
72            Error::InvalidAttribute {
73                element,
74                attribute,
75                value,
76            } => {
77                write!(
78                    f,
79                    "invalid value '{value}' in attribute '{attribute}' of <{element}>"
80                )
81            }
82            Error::ConflictingLengthFields {
83                field,
84                first,
85                second,
86            } => {
87                write!(
88                    f,
89                    "field '{field}' takes its length from '{first}' in one place and '{second}' in another"
90                )
91            }
92            Error::Invalid(error) => {
93                write!(f, "invalid dictionary: {error}")
94            }
95        }
96    }
97}
98
99impl std::error::Error for Error {
100    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
101        match self {
102            Error::Xml(error) => Some(error),
103            Error::Invalid(error) => Some(error),
104            _ => None,
105        }
106    }
107}