mikrotik_rs/protocol/error.rs
1use super::{sentence::SentenceError, word::Word, TrapCategoryError};
2
3/// Possible errors while parsing a [`CommandResponse`] from a [`Sentence`].
4///
5/// This enum provides more detailed information about issues that can arise while parsing
6/// command responses, such as missing tags, missing attributes, or unexpected attributes.
7#[derive(Debug)]
8pub enum ProtocolError {
9 /// Error related to the [`Sentence`].
10 ///
11 /// This variant encapsulates errors that occur due to issues in parsing a
12 /// [`Sentence`] from bytes.
13 Sentence(SentenceError),
14 /// Error related to the length of a response.
15 ///
16 /// Indicates that the response is missing some words to be a valid response.
17 Incomplete(MissingWord),
18 /// The received sequence of words is not a valid response.
19 WordSequence {
20 /// The unexpected [`WordType`] that was encountered.
21 word: WordType,
22 /// The expected [`WordType`].
23 expected: Vec<WordType>,
24 },
25
26 /// Error related to identifying or parsing a [`Trap`] response category.
27 ///
28 /// Indicates that an invalid category was encountered during parsing,
29 /// which likely points to either a malformed response.
30 TrapCategory(TrapCategoryError),
31 // Error involving attributes in a response.
32 //
33 // Indicates issues related to the unexpected presence of attributes within a response.
34 //UnexpectedWord(Word<'a>),
35}
36
37impl From<SentenceError> for ProtocolError {
38 fn from(e: SentenceError) -> Self {
39 ProtocolError::Sentence(e)
40 }
41}
42
43impl From<MissingWord> for ProtocolError {
44 fn from(e: MissingWord) -> Self {
45 ProtocolError::Incomplete(e)
46 }
47}
48
49impl From<TrapCategoryError> for ProtocolError {
50 fn from(e: TrapCategoryError) -> Self {
51 ProtocolError::TrapCategory(e)
52 }
53}
54
55/// Types of words that can be missing from a response.
56#[derive(Debug)]
57pub enum MissingWord {
58 /// Missing `.tag` in the response. All responses must have a tag.
59 Tag,
60 /// Missing category (`!done`, `!repl`, `!trap`, `!fatal`) in the response.
61 Category,
62 /// Missing message in a [`CommandResponse::FatalResponse`]
63 Message,
64}
65
66/// Represents the type of a word in a response.
67#[derive(Debug)]
68pub enum WordType {
69 /// Tag word.
70 Tag,
71 /// Category word.
72 Category,
73 /// Attribute word.
74 Attribute,
75 /// Message word.
76 Message,
77}
78
79impl From<Word<'_>> for WordType {
80 fn from(word: Word) -> Self {
81 match word {
82 Word::Tag(_) => WordType::Tag,
83 Word::Category(_) => WordType::Category,
84 Word::Attribute(_) => WordType::Attribute,
85 Word::Message(_) => WordType::Message,
86 }
87 }
88}