From 01b03fe19f4f88f3601a7ee1094da4a9078cb7ee Mon Sep 17 00:00:00 2001 From: Jaken Herman Date: Tue, 21 Jan 2025 11:00:15 +0000 Subject: [PATCH] Added `CamelBreed` enum --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 9 ++++- src/camel.rs | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 5 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 src/camel.rs diff --git a/Cargo.lock b/Cargo.lock index 96c87d4..49df846 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,7 +29,7 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "livestock-rs" -version = "0.6.1" +version = "0.7.0" dependencies = [ "Inflector", "serde", diff --git a/Cargo.toml b/Cargo.toml index fabf9d7..e34e98a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "livestock-rs" -version = "0.6.1" +version = "0.7.0" edition = "2021" authors = ["Jaken Herman "] license = "MIT" diff --git a/README.md b/README.md index 06f5f54..dd82979 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Features - πŸ– Includes common swine breeds like Duroc and Hampshire. - 🫏 Includes common donkey breeds like Miniature and Standard. - 🦌 Includes common reindeer breeds like Even and Nentsi. + - πŸͺ Includes common camel breeds like Afar Dromedary and Somali Dromedary. - πŸ› οΈ Utilities for converting between enum variants and human-readable strings. - πŸ”’ Serde support for serialization and deserialization. @@ -20,7 +21,7 @@ Add the crate to your `Cargo.toml`: ``` [dependencies] -livestock_rs = "0.6.1" +livestock_rs = "0.7.0" ``` or @@ -59,8 +60,12 @@ println!("{:?}", breed); // prints "CanindΓ©" ## Reindeer Breeds - Features common reindeer breeds including Chukotka, Even, Evenk, and Nentsi. +## Camel Breeds +- Features common varieties of camel breeds like Afar Dromedary and Somali Dromedary. +- Regional specialties, including Kalmyk Bactrian and more. + ## Roadmap - - πŸ—‚οΈ Expand support for other livestock species (e.g., chickens, ducks, pigs). + - πŸ—‚οΈ Expand support for other livestock species (e.g., chickens, ducks, geese). - 🌍 Localization support for breed names in multiple languages. - πŸ“Š Add more utilities for livestock data management. diff --git a/src/camel.rs b/src/camel.rs new file mode 100644 index 0000000..778a70e --- /dev/null +++ b/src/camel.rs @@ -0,0 +1,103 @@ +use serde::{Deserialize, Serialize}; +use std::str::FromStr; + +/// An enum representing the different breeds of camel. +/// +/// Initial data from: +/// +/// # 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 { + 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); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index ae52e19..85adcc5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod camel; pub mod cattle; pub mod donkey; pub mod goat;