1use thiserror::Error;
2
3pub type Result<T> = std::result::Result<T, TelloError>;
4
5#[derive(Error, Debug)]
6pub enum TelloError {
7 #[error("{msg}")]
8 Generic { msg: String },
9
10 #[error("WiFi not connected")]
11 WiFiNotConnected,
12
13 #[error("IO error: {msg}")]
14 IOError { msg: String },
15
16 #[error("Failed to decode data from the drone: {msg} ")]
17 DecodeError { msg: String },
18
19 #[error("Failed to parse data from the drone: {msg} ")]
20 ParseError { msg: String },
21
22 #[error("Expected response \"ok\", but received \"{response}\"")]
23 NotOkResponse { response: String },
24
25 #[error("Value out of range")]
26 OutOfRange,
27
28 #[error("Non-specific error response")]
29 NonSpecificError
30}
31
32impl From<std::io::Error> for TelloError {
33 fn from(err: std::io::Error) -> TelloError {
34 TelloError::IOError { msg: err.to_string() }
35 }
36}
37
38impl From<std::string::FromUtf8Error> for TelloError {
39 fn from(err: std::string::FromUtf8Error) -> TelloError {
40 TelloError::DecodeError { msg: err.to_string() }
41 }
42}
43
44impl TelloError {
45 pub fn from_not_ok_response(response: String) -> TelloError {
46 match response.as_str() {
47 "error" => TelloError::NonSpecificError,
48 "out of range" => TelloError::OutOfRange,
49 _ => TelloError::NotOkResponse { response }
50 }
51 }
52}