Skip to content

Commit

Permalink
Added CamelBreed enum (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakenHerman authored Jan 21, 2025
1 parent 15cf63b commit fb15a92
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "livestock-rs"
version = "0.6.1"
version = "0.7.0"
edition = "2021"
authors = ["Jaken Herman <jaken@rowanranch.com>"]
license = "MIT"
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -20,7 +21,7 @@ Add the crate to your `Cargo.toml`:

```
[dependencies]
livestock_rs = "0.6.1"
livestock_rs = "0.7.0"
```

or
Expand Down Expand Up @@ -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.

Expand Down
103 changes: 103 additions & 0 deletions src/camel.rs
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);
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
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;
Expand Down

0 comments on commit fb15a92

Please sign in to comment.