Impl FromStr for Algorithm enum

This commit is contained in:
Nathan Fisher 2023-07-06 18:31:49 -04:00
parent 05bec9ae2f
commit d303eb0390
2 changed files with 17 additions and 1 deletions

View File

@ -1,6 +1,6 @@
use {
crate::Error,
std::io::{Read, Write},
std::{io::{Read, Write}, str::FromStr},
};
#[derive(Clone, Copy, Debug)]
@ -11,6 +11,20 @@ pub enum Algorithm {
Skip,
}
impl FromStr for Algorithm {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"md5" | "Md5" => Ok(Self::Md5),
"sha1" | "Sha1" => Ok(Self::Sha1),
"sha256" | "Sha256" => Ok(Self::Sha256),
"skip" | "Skip" => Ok(Self::Skip),
_ => Err(Error::InvalidAlgorithm),
}
}
}
/// Optional checksumming for individual files
#[derive(Debug)]
pub enum Checksum {

View File

@ -9,6 +9,7 @@ pub enum Error {
Utf8(FromUtf8Error),
Slice(TryFromSliceError),
InvalidChecksum,
InvalidAlgorithm,
MissingData,
MutexError,
NulError,
@ -25,6 +26,7 @@ impl fmt::Display for Error {
Self::Utf8(e) => write!(f, "{e}"),
Self::Slice(e) => write!(f, "{e}"),
Self::Io(e) => write!(f, "{e}"),
Self::InvalidAlgorithm => write!(f, "invalid algorithm"),
Self::InvalidChecksum => write!(f, "invalid checksum"),
Self::MissingData => write!(f, "missing data"),
Self::MutexError => write!(f, "mutex error"),