From d303eb0390d96007f2679416ccd037766a68f4fe Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Thu, 6 Jul 2023 18:31:49 -0400 Subject: [PATCH] Impl FromStr for Algorithm enum --- src/checksum.rs | 16 +++++++++++++++- src/error.rs | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) 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"),