mikrotik_rs/protocol/
error.rs

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use super::{sentence::SentenceError, word::Word, TrapCategoryError};

/// Possible errors while parsing a [`CommandResponse`] from a [`Sentence`].
///
/// This enum provides more detailed information about issues that can arise while parsing
/// command responses, such as missing tags, missing attributes, or unexpected attributes.
#[derive(Debug)]
pub enum ProtocolError {
    /// Error related to the [`Sentence`].
    ///
    /// This variant encapsulates errors that occur due to issues in parsing a
    /// [`Sentence`] from bytes.
    Sentence(SentenceError),
    /// Error related to the length of a response.
    ///
    /// Indicates that the response is missing some words to be a valid response.
    Incomplete(MissingWord),
    /// The received sequence of words is not a valid response.
    WordSequence {
        /// The unexpected [`WordType`] that was encountered.
        word: WordType,
        /// The expected [`WordType`].
        expected: Vec<WordType>,
    },

    /// Error related to identifying or parsing a [`Trap`] response category.
    ///
    /// Indicates that an invalid category was encountered during parsing,
    /// which likely points to either a malformed response.
    TrapCategory(TrapCategoryError),
    // Error involving attributes in a response.
    //
    // Indicates issues related to the unexpected presence of attributes within a response.
    //UnexpectedWord(Word<'a>),
}

impl From<SentenceError> for ProtocolError {
    fn from(e: SentenceError) -> Self {
        ProtocolError::Sentence(e)
    }
}

impl From<MissingWord> for ProtocolError {
    fn from(e: MissingWord) -> Self {
        ProtocolError::Incomplete(e)
    }
}

impl From<TrapCategoryError> for ProtocolError {
    fn from(e: TrapCategoryError) -> Self {
        ProtocolError::TrapCategory(e)
    }
}

/// Types of words that can be missing from a response.
#[derive(Debug)]
pub enum MissingWord {
    /// Missing `.tag` in the response. All responses must have a tag.
    Tag,
    /// Missing category (`!done`, `!repl`, `!trap`, `!fatal`) in the response.
    Category,
    /// Missing message in a [`CommandResponse::FatalResponse`]
    Message,
}

/// Represents the type of a word in a response.
#[derive(Debug)]
pub enum WordType {
    /// Tag word.
    Tag,
    /// Category word.
    Category,
    /// Attribute word.
    Attribute,
    /// Message word.
    Message,
}

impl From<Word<'_>> for WordType {
    fn from(word: Word) -> Self {
        match word {
            Word::Tag(_) => WordType::Tag,
            Word::Category(_) => WordType::Category,
            Word::Attribute(_) => WordType::Attribute,
            Word::Message(_) => WordType::Message,
        }
    }
}