-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15cf63b
commit fb15a92
Showing
5 changed files
with
113 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::str::FromStr; | ||
|
||
/// An enum representing the different breeds of camel. | ||
/// | ||
/// Initial data from: <https://breeds.okstate.edu/other-breeds-of-livestock/camels/> | ||
/// | ||
/// # Examples | ||
/// ``` rust | ||
/// use livestock_rs::camel::CamelBreed; | ||
/// | ||
/// let breed = CamelBreed::AfarDromedary; | ||
/// println!("{:?}", breed); | ||
/// ``` | ||
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] | ||
pub enum CamelBreed { | ||
AfarDromedary, | ||
AlxaBactrian, | ||
ArvanaDromedary, | ||
KalmykBactrian, | ||
SomaliDromedary | ||
} | ||
|
||
impl ToString for CamelBreed { | ||
/// Converts the CamelBreed enum to a human readable string. | ||
/// | ||
/// # Examples | ||
/// ``` rust | ||
/// use livestock_rs::camel::CamelBreed; | ||
/// | ||
/// let alxa = CamelBreed::AlxaBactrian; | ||
/// println!("{}", alxa.to_string()); | ||
/// ``` | ||
fn to_string(&self) -> String { | ||
match self { | ||
CamelBreed::AfarDromedary => "Afar Dromedary".to_string(), | ||
CamelBreed::AlxaBactrian => "Alxa Bactrian".to_string(), | ||
CamelBreed::ArvanaDromedary => "Arvana Dromedary".to_string(), | ||
CamelBreed::KalmykBactrian => "Kalmyk Bactrian".to_string(), | ||
CamelBreed::SomaliDromedary => "Somali Dromedary".to_string(), | ||
} | ||
} | ||
} | ||
|
||
/// Converts a string to a CamelBreed enum. | ||
/// | ||
/// # Examples | ||
/// ``` rust | ||
/// use livestock_rs::camel::CamelBreed; | ||
/// use std::str::FromStr; | ||
/// | ||
/// let breed = CamelBreed::from_str("Somali Dromedary").unwrap(); | ||
/// println!("{:?}", breed); | ||
/// ``` | ||
impl FromStr for CamelBreed { | ||
type Err = std::io::Error; | ||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s.to_lowercase().as_str() { | ||
"afar dromedary" | "afar" => Ok(CamelBreed::AfarDromedary), | ||
"alxa bactrian" | "alxa" => Ok(CamelBreed::AlxaBactrian), | ||
"arvana dromedary" | "arvana" => Ok(CamelBreed::ArvanaDromedary), | ||
"kalmyk bactrian" | "kalmyk" => Ok(CamelBreed::KalmykBactrian), | ||
"somali dromedary" | "somali" => Ok(CamelBreed::SomaliDromedary), | ||
_ => Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid camel breed")), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_to_string() { | ||
let breeds = [ | ||
(CamelBreed::AfarDromedary, "Afar Dromedary"), | ||
(CamelBreed::AlxaBactrian, "Alxa Bactrian"), | ||
(CamelBreed::ArvanaDromedary, "Arvana Dromedary"), | ||
(CamelBreed::KalmykBactrian, "Kalmyk Bactrian"), | ||
(CamelBreed::SomaliDromedary, "Somali Dromedary"), | ||
]; | ||
|
||
for (breed, expected) in breeds.iter() { | ||
assert_eq!(breed.to_string(), *expected); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_from_str() { | ||
let breeds = [ | ||
("afar dromedary", CamelBreed::AfarDromedary), | ||
("alxa bactrian", CamelBreed::AlxaBactrian), | ||
("arvana dromedary", CamelBreed::ArvanaDromedary), | ||
("kalmyk bactrian", CamelBreed::KalmykBactrian), | ||
("somali dromedary", CamelBreed::SomaliDromedary), | ||
]; | ||
|
||
for (breed, expected) in breeds.iter() { | ||
assert_eq!(CamelBreed::from_str(*breed).unwrap(), *expected); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod camel; | ||
pub mod cattle; | ||
pub mod donkey; | ||
pub mod goat; | ||
|