diff --git a/src/checksum.rs b/src/checksum.rs index 8097bf5..556e3b7 100644 --- a/src/checksum.rs +++ b/src/checksum.rs @@ -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 { + 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 { diff --git a/src/error.rs b/src/error.rs index 6fbfce3..64e2d71 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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"),