78 lines
1.6 KiB
Rust
78 lines
1.6 KiB
Rust
use std::fmt;
|
|
|
|
use crate::sharry;
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum Parameter {
|
|
#[error("given URI {0:?}")]
|
|
Uri(String),
|
|
|
|
#[error("given Alias ID {0:?}")]
|
|
AliasID(String),
|
|
|
|
#[error("stored Share ID {0:?}")]
|
|
ShareID(String),
|
|
|
|
#[error("stored {0:?}")]
|
|
FileID(sharry::FileID),
|
|
}
|
|
|
|
impl Parameter {
|
|
fn is_fatal(&self) -> bool {
|
|
matches!(self, Self::Uri(_) | Self::AliasID(_))
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum Error {
|
|
#[error(transparent)]
|
|
StdIo(#[from] std::io::Error),
|
|
|
|
#[error("response error: {0}")]
|
|
Response(String),
|
|
|
|
#[error("Invalid {0}")]
|
|
InvalidParameter(Parameter),
|
|
|
|
#[error("Unknown error: {0}")]
|
|
Unknown(String),
|
|
}
|
|
|
|
#[allow(clippy::needless_pass_by_value)]
|
|
fn into_string(val: impl ToString) -> String {
|
|
val.to_string()
|
|
}
|
|
|
|
impl Error {
|
|
pub fn res_status_check<T>(actual: T, expected: T) -> Result<()>
|
|
where
|
|
T: PartialEq + fmt::Display + Copy,
|
|
{
|
|
if actual == expected {
|
|
Ok(())
|
|
} else {
|
|
Err(Self::Response(format!(
|
|
"unexpected status: {actual} (expected {expected})"
|
|
)))
|
|
}
|
|
}
|
|
|
|
pub fn response(e: impl ToString) -> Self {
|
|
Self::Response(into_string(e))
|
|
}
|
|
|
|
pub fn unknown(e: impl ToString) -> Self {
|
|
Self::Unknown(into_string(e))
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn is_fatal(&self) -> bool {
|
|
match self {
|
|
Self::InvalidParameter(p) => p.is_fatal(),
|
|
Self::Unknown(_) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|