tinkerforge_async/
converting_receiver.rs

1//! A wrapper for [`Receiver`](std::sync::mpsc::Receiver), which converts received byte vectors to structured data.
2/// Error type for interactions with Tinkerforge bricks or bricklets.
3#[derive(Debug, Copy, Clone)]
4pub enum BrickletError {
5    /// A parameter was invalid or had an unexpected length
6    InvalidParameter,
7    /// The brick or bricklet does not support the requested function.
8    FunctionNotSupported,
9    /// Currently unused
10    UnknownError,
11    /// The request can not be fulfulled, as there is currently no connection to a brick daemon.
12    NotConnected,
13    /// The request was sent, but response expected is disabled, so no response can be received. This is not an error.
14    SuccessButResponseExpectedIsDisabled,
15}
16
17impl From<u8> for BrickletError {
18    fn from(byte: u8) -> BrickletError {
19        match byte {
20            1 => BrickletError::InvalidParameter,
21            2 => BrickletError::FunctionNotSupported,
22            _ => BrickletError::UnknownError,
23        }
24    }
25}
26
27impl std::fmt::Display for BrickletError {
28    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
29        write!(
30            f,
31            "{}",
32            match &self {
33                BrickletError::InvalidParameter => "A parameter was invalid or had an unexpected length.",
34                BrickletError::FunctionNotSupported => "The brick or bricklet does not support the requested function.",
35                BrickletError::UnknownError => "UnknownError, Currently unused",
36                BrickletError::NotConnected => "The request can not be fulfulled, as there is currently no connection to a brick daemon.",
37                BrickletError::SuccessButResponseExpectedIsDisabled =>
38                    "The request was sent, but response expected is disabled, so no response can be received. This is not an error.",
39            }
40        )
41    }
42}
43
44impl std::error::Error for BrickletError {}
45
46/// Error type which is returned if a ConvertingReceiver::recv call fails.
47#[derive(Copy, Clone, Debug, PartialEq)]
48pub enum BrickletRecvTimeoutError {
49    /// The queue was disconnected. This usually happens if the ip connection is destroyed.
50    QueueDisconnected,
51    /// The request could not be responded to before the timeout was reached.
52    QueueTimeout,
53    /// A parameter was invalid or had an unexpected length.
54    InvalidParameter,
55    /// The brick or bricklet does not support the requested function.
56    FunctionNotSupported,
57    /// Currently unused
58    UnknownError,
59    /// The received packet had an unexpected length. Maybe a function was called on a wrong brick or bricklet?
60    MalformedPacket,
61    /// The request can not be fulfulled, as there is currently no connection to a brick daemon.
62    NotConnected,
63    /// The request was sent, but response expected is disabled, so no response can be received. This is not an error.
64    SuccessButResponseExpectedIsDisabled,
65}
66
67impl std::fmt::Display for BrickletRecvTimeoutError {
68    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
69        write!(
70            f,
71            "{}",
72            match &self {
73                BrickletRecvTimeoutError::QueueDisconnected =>
74                    "The queue was disconnected. This usually happens if the ip connection is destroyed.",
75                BrickletRecvTimeoutError::QueueTimeout => "The request could not be responded to before the timeout was reached.",
76                BrickletRecvTimeoutError::InvalidParameter => "A parameter was invalid or had an unexpected length.",
77                BrickletRecvTimeoutError::FunctionNotSupported => "The brick or bricklet does not support the requested function.",
78                BrickletRecvTimeoutError::UnknownError => "UnknownError, Currently unused",
79                BrickletRecvTimeoutError::MalformedPacket =>
80                    "The received packet had an unexpected length. Maybe a function was called on a wrong brick or bricklet?",
81                BrickletRecvTimeoutError::NotConnected =>
82                    "The request can not be fulfulled, as there is currently no connection to a brick daemon.",
83                BrickletRecvTimeoutError::SuccessButResponseExpectedIsDisabled =>
84                    "The request was sent, but response expected is disabled, so no response can be received. This is not an error.",
85            }
86        )
87    }
88}
89
90impl std::error::Error for BrickletRecvTimeoutError {}
91
92/// Error type which is returned if a [`try_recv`](crate::converting_receiver::ConvertingReceiver::try_recv) call fails.
93#[derive(Copy, Clone, Debug, PartialEq)]
94pub enum BrickletTryRecvError {
95    /// The queue was disconnected. This usually happens if the ip connection is destroyed.
96    QueueDisconnected,
97    /// There are currently no responses available.
98    QueueEmpty,
99    /// A parameter was invalid or had an unexpected length.
100    InvalidParameter,
101    /// The brick or bricklet does not support the requested function.
102    FunctionNotSupported,
103    /// Currently unused
104    UnknownError,
105    /// The received packet had an unexpected length. Maybe a function was called on a wrong brick or bricklet?
106    MalformedPacket,
107    /// The request can not be fulfulled, as there is currently no connection to a brick daemon.
108    NotConnected,
109    /// The request was sent, but response expected is disabled, so no response can be received. This is not an error.
110    SuccessButResponseExpectedIsDisabled,
111}
112
113impl std::fmt::Display for BrickletTryRecvError {
114    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
115        write!(
116            f,
117            "{}",
118            match &self {
119                BrickletTryRecvError::QueueDisconnected =>
120                    "The queue was disconnected. This usually happens if the ip connection is destroyed.",
121                BrickletTryRecvError::QueueEmpty => "There are currently no responses available.",
122                BrickletTryRecvError::InvalidParameter => "A parameter was invalid or had an unexpected length.",
123                BrickletTryRecvError::FunctionNotSupported => "The brick or bricklet does not support the requested function.",
124                BrickletTryRecvError::UnknownError => "UnknownError, Currently unused",
125                BrickletTryRecvError::MalformedPacket =>
126                    "The received packet had an unexpected length. Maybe a function was called on a wrong brick or bricklet?",
127                BrickletTryRecvError::NotConnected =>
128                    "The request can not be fulfulled, as there is currently no connection to a brick daemon.",
129                BrickletTryRecvError::SuccessButResponseExpectedIsDisabled =>
130                    "The request was sent, but response expected is disabled, so no response can be received. This is not an error.",
131            }
132        )
133    }
134}
135
136impl std::error::Error for BrickletTryRecvError {}