feat(errors): Using SerializeDisplay for better error management with Result
This commit is contained in:
@@ -3,10 +3,12 @@ use std::{
|
||||
io,
|
||||
};
|
||||
|
||||
use serde_with::SerializeDisplay;
|
||||
|
||||
use super::{remote_access_error::RemoteAccessError, setup_error::SetupError};
|
||||
|
||||
// TODO: Rename / separate from downloads
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, SerializeDisplay)]
|
||||
pub enum ApplicationDownloadError {
|
||||
Communication(RemoteAccessError),
|
||||
Checksum,
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use serde_with::SerializeDisplay;
|
||||
|
||||
#[derive(SerializeDisplay)]
|
||||
pub enum LibraryError {
|
||||
MetaNotFound(String),
|
||||
}
|
||||
|
||||
@@ -4,4 +4,3 @@ pub mod library_error;
|
||||
pub mod process_error;
|
||||
pub mod remote_access_error;
|
||||
pub mod setup_error;
|
||||
pub mod user_error;
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
use std::{fmt::Display, io::Error};
|
||||
|
||||
use serde_with::SerializeDisplay;
|
||||
|
||||
#[derive(SerializeDisplay)]
|
||||
pub enum ProcessError {
|
||||
SetupRequired,
|
||||
NotInstalled,
|
||||
|
||||
@@ -5,11 +5,12 @@ use std::{
|
||||
};
|
||||
|
||||
use http::StatusCode;
|
||||
use serde_with::SerializeDisplay;
|
||||
use url::ParseError;
|
||||
|
||||
use super::drop_server_error::DropServerError;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, SerializeDisplay)]
|
||||
pub enum RemoteAccessError {
|
||||
FetchError(Arc<reqwest::Error>),
|
||||
ParsingError(ParseError),
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
use std::{
|
||||
fmt::Display,
|
||||
ops::{FromResidual, Try},
|
||||
};
|
||||
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum UserValue<T, D>
|
||||
where
|
||||
T: Serialize,
|
||||
D: Display,
|
||||
{
|
||||
Ok(T),
|
||||
Err(D),
|
||||
}
|
||||
impl<T: Serialize, D: Display> Serialize for UserValue<T, D> {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
match self {
|
||||
UserValue::Ok(data) => data.serialize(serializer),
|
||||
UserValue::Err(err) => serializer.serialize_str(err.to_string().as_ref()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Serialize, D: Display> From<Result<T, D>> for UserValue<T, D> {
|
||||
fn from(value: Result<T, D>) -> Self {
|
||||
match value {
|
||||
Ok(data) => UserValue::Ok(data),
|
||||
Err(data) => UserValue::Err(data),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: Serialize, D: Display> Try for UserValue<T, D> {
|
||||
type Output = T;
|
||||
|
||||
type Residual = D;
|
||||
|
||||
fn from_output(output: Self::Output) -> Self {
|
||||
Self::Ok(output)
|
||||
}
|
||||
|
||||
fn branch(self) -> std::ops::ControlFlow<Self::Residual, Self::Output> {
|
||||
match self {
|
||||
UserValue::Ok(data) => std::ops::ControlFlow::Continue(data),
|
||||
UserValue::Err(e) => std::ops::ControlFlow::Break(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: Serialize, D: Display> FromResidual for UserValue<T, D> {
|
||||
fn from_residual(residual: <Self as std::ops::Try>::Residual) -> Self {
|
||||
UserValue::Err(residual)
|
||||
}
|
||||
}
|
||||
impl<T: Serialize, D: Display, U> FromResidual<Result<U, D>> for UserValue<T, D> {
|
||||
fn from_residual(residual: Result<U, D>) -> Self {
|
||||
match residual {
|
||||
Ok(_) => unreachable!(),
|
||||
Err(e) => UserValue::Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user