12 changed files with 196 additions and 139 deletions
@ -1,3 +1,6 @@ |
|||
/target |
|||
secret.toml |
|||
*.gpx |
|||
*.gpx |
|||
*.code-workspace |
|||
/thingy_lib/Cargo.lock |
|||
/thingy_lib/target/ |
@ -0,0 +1,11 @@ |
|||
[package] |
|||
name = "thingy-lib" |
|||
version = "0.1.0" |
|||
authors = ["Teascade <teascade@gmail.com>"] |
|||
edition = "2018" |
|||
|
|||
[dependencies] |
|||
serde = { version = "1.0", features = ["derive"] } |
|||
toml = "0.5" |
|||
minreq = { version = "2.2.0", features = ["https", "json-using-serde"] } |
|||
chrono = "0.4" |
@ -0,0 +1,88 @@ |
|||
use super::api::ErrorModel; |
|||
use std::fmt::{Display, Formatter}; |
|||
use std::io; |
|||
|
|||
#[derive(Debug)] |
|||
pub enum LibError { |
|||
TomlError(toml::de::Error), |
|||
YepzonServerError(ErrorModel), |
|||
MinreqError(minreq::Error), |
|||
ChronoParseError(chrono::ParseError), |
|||
IOError(io::Error), |
|||
FromUTF8Error(std::string::FromUtf8Error), |
|||
MessagedError(String, Option<Box<LibError>>), |
|||
} |
|||
|
|||
impl From<toml::de::Error> for LibError { |
|||
fn from(error: toml::de::Error) -> Self { |
|||
LibError::TomlError(error) |
|||
} |
|||
} |
|||
|
|||
impl From<minreq::Error> for LibError { |
|||
fn from(error: minreq::Error) -> Self { |
|||
LibError::MinreqError(error) |
|||
} |
|||
} |
|||
|
|||
impl From<chrono::ParseError> for LibError { |
|||
fn from(error: chrono::ParseError) -> Self { |
|||
LibError::ChronoParseError(error) |
|||
} |
|||
} |
|||
|
|||
impl From<ErrorModel> for LibError { |
|||
fn from(error: ErrorModel) -> Self { |
|||
LibError::YepzonServerError(error) |
|||
} |
|||
} |
|||
|
|||
impl From<io::Error> for LibError { |
|||
fn from(error: io::Error) -> Self { |
|||
LibError::IOError(error) |
|||
} |
|||
} |
|||
|
|||
impl From<std::string::FromUtf8Error> for LibError { |
|||
fn from(error: std::string::FromUtf8Error) -> Self { |
|||
LibError::FromUTF8Error(error) |
|||
} |
|||
} |
|||
|
|||
impl Display for LibError { |
|||
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { |
|||
let err = match self { |
|||
LibError::ChronoParseError(e) => format!("Date-Time value parse error: {}", e), |
|||
LibError::FromUTF8Error(e) => format!("UTF-8 error: {}", e), |
|||
LibError::IOError(e) => format!("IO error: {}", e), |
|||
LibError::MinreqError(e) => format!("Network error: {}", e), |
|||
LibError::TomlError(e) => format!("Toml error: {}", e), |
|||
LibError::YepzonServerError(e) => format!( |
|||
"Yepzon server error: {}", |
|||
e.message.as_ref().unwrap_or(&String::new()) |
|||
), |
|||
LibError::MessagedError(msg, err) => format!( |
|||
"{}\n {}", |
|||
msg, |
|||
err.as_ref().map(|e| e.to_string()).unwrap_or(String::new()) |
|||
), |
|||
}; |
|||
write!(f, "{}", err) |
|||
} |
|||
} |
|||
|
|||
pub trait MessagedError<A> { |
|||
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, LibError>; |
|||
} |
|||
|
|||
impl<A, B: Into<LibError>> MessagedError<A> for Result<A, B> { |
|||
fn with_msg<T: Into<String>>(self, text: T) -> Result<A, LibError> { |
|||
match self { |
|||
Ok(ok) => Ok(ok), |
|||
Err(e) => Err(LibError::MessagedError( |
|||
text.into(), |
|||
Some(Box::new(e.into())), |
|||
)), |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue