Skip to main content

refix_message/writer/
error.rs

1use crate::Tag;
2use std::fmt;
3
4/// A value that cannot be written as a field.
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6pub enum WriteError {
7    /// The value is empty.
8    EmptyValue { tag: Tag },
9    /// The value holds the field separator.
10    SeparatorInValue { tag: Tag },
11    /// The field is one that framing writes itself.
12    ReservedTag { tag: Tag },
13}
14
15impl fmt::Display for WriteError {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            WriteError::EmptyValue { tag } => write!(f, "empty value for field {tag}"),
19            WriteError::SeparatorInValue { tag } => {
20                write!(f, "separator in the value for field {tag}")
21            }
22            WriteError::ReservedTag { tag } => write!(f, "field {tag} is written by framing"),
23        }
24    }
25}
26
27impl std::error::Error for WriteError {}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn displays_each_error() {
35        let cases = [
36            (
37                WriteError::EmptyValue { tag: Tag(58) },
38                "empty value for field 58",
39            ),
40            (
41                WriteError::SeparatorInValue { tag: Tag(58) },
42                "separator in the value for field 58",
43            ),
44            (
45                WriteError::ReservedTag { tag: Tag(35) },
46                "field 35 is written by framing",
47            ),
48        ];
49        for (error, expected) in cases {
50            assert_eq!(error.to_string(), expected);
51        }
52    }
53}