Compare commits

..

No commits in common. "de208a20d5ba5c4b341b2d92d62c0c99c905ede4" and "2eb651f9196345e4606c3569b3e22baa40188615" have entirely different histories.

4 changed files with 8 additions and 73 deletions

View file

@ -86,7 +86,7 @@ fn main() {
Err(e) => { Err(e) => {
Log::handle(&e); Log::handle(&e);
if let Some(p) = e.get_invalid_param() { if let shrupl::Error::InvalidParameter(p) = e {
match p { match p {
// Error 404 (File not found) // Error 404 (File not found)
shrupl::Parameter::FileID(fid) => { shrupl::Parameter::FileID(fid) => {

View file

@ -5,7 +5,7 @@ use crate::sharry;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum Parameter { pub enum Parameter {
#[error("given URI {0:?}")] #[error("given URI {0:?}")]
Uri(sharry::Uri), Uri(String),
#[error("given Alias ID {0:?}")] #[error("given Alias ID {0:?}")]
AliasID(sharry::AliasID), AliasID(sharry::AliasID),
@ -17,27 +17,6 @@ pub enum Parameter {
FileID(sharry::FileID), FileID(sharry::FileID),
} }
// a helper to generate all the `From<T> for Parameter` impls
macro_rules! impl_param_from {
// $typ: the source type; $var: the enumvariant name
( $( $typ:path => $var:ident ),* $(,)? ) => {
$(
impl From<$typ> for Parameter {
fn from(value: $typ) -> Self {
Self::$var(value)
}
}
)*
};
}
impl_param_from! {
sharry::Uri => Uri,
sharry::AliasID => AliasID,
sharry::ShareID => ShareID,
sharry::FileID => FileID,
}
impl Parameter { impl Parameter {
fn is_fatal(&self) -> bool { fn is_fatal(&self) -> bool {
matches!(self, Self::Uri(_) | Self::AliasID(_)) matches!(self, Self::Uri(_) | Self::AliasID(_))
@ -62,35 +41,6 @@ pub enum Error {
Unknown(String), Unknown(String),
} }
// a helper to generate all the `From<T> for Error` impls
macro_rules! impl_error_from {
// $typ: the source type; $var: the enumvariant name
( $( $typ:path => $var:ident ),* $(,)? ) => {
$(
// // implement for values
// impl From<$typ> for Error {
// fn from(value: $typ) -> Self {
// Self::InvalidParameter(value.into())
// }
// }
// implement for references
impl From<&$typ> for Error {
fn from(value: &$typ) -> Self {
Self::InvalidParameter(value.clone().into())
}
}
)*
};
}
impl_error_from! {
sharry::Uri => Uri,
sharry::AliasID => AliasID,
sharry::ShareID => ShareID,
sharry::FileID => FileID,
}
#[allow(clippy::needless_pass_by_value)] #[allow(clippy::needless_pass_by_value)]
fn into_string(val: impl ToString) -> String { fn into_string(val: impl ToString) -> String {
val.to_string() val.to_string()
@ -121,15 +71,6 @@ impl Error {
} }
} }
#[must_use]
pub fn get_invalid_param(&self) -> Option<&Parameter> {
if let Self::InvalidParameter(p) = self {
Some(p)
} else {
None
}
}
#[must_use] #[must_use]
pub fn is_fatal(&self) -> bool { pub fn is_fatal(&self) -> bool {
match self { match self {

View file

@ -15,17 +15,17 @@ fn find_cause(
ureq::Error::StatusCode(403) => { ureq::Error::StatusCode(403) => {
trace!("HTTP Error 403: Alias not found!"); trace!("HTTP Error 403: Alias not found!");
alias_id.into() crate::Error::InvalidParameter(crate::Parameter::AliasID(alias_id.to_owned()))
} }
ureq::Error::StatusCode(404) => { ureq::Error::StatusCode(404) => {
trace!("HTTP Error 404: Share and/or file may have been deleted!"); trace!("HTTP Error 404: Share and/or file may have been deleted!");
if let Some(file_id) = file_id { if let Some(file_id) = file_id {
file_id.into() crate::Error::InvalidParameter(crate::Parameter::FileID(file_id.to_owned()))
} else if let Some(share_id) = share_id { } else if let Some(share_id) = share_id {
share_id.into() crate::Error::InvalidParameter(crate::Parameter::ShareID(share_id.to_owned()))
} else { } else {
uri.into() crate::Error::InvalidParameter(crate::Parameter::Uri(uri.to_string()))
} }
} }
ureq::Error::Io(error) => { ureq::Error::Io(error) => {
@ -33,7 +33,7 @@ fn find_cause(
if let Some(msg) = error.get_ref().map(ToString::to_string) { if let Some(msg) = error.get_ref().map(ToString::to_string) {
if msg.starts_with("failed to lookup address information") { if msg.starts_with("failed to lookup address information") {
uri.into() crate::Error::InvalidParameter(crate::Parameter::Uri(uri.to_string()))
} else { } else {
error.into() error.into()
} }

View file

@ -3,13 +3,7 @@ use std::fmt;
use log::trace; use log::trace;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// ID of a file in a Sharry share #[derive(Serialize, Deserialize, Debug)]
///
/// - impl `Clone` as this is just a String
/// - impl `serde` for cachefile handling
/// - impl `Display` for formatting compatibility
/// - impl `AsRef<[u8]>` for hashing with `blake2b_simd`
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Uri(String); pub struct Uri(String);
impl fmt::Display for Uri { impl fmt::Display for Uri {