diff --git a/Cargo.lock b/Cargo.lock index 55100a51..3c1c1189 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1435,15 +1435,15 @@ dependencies = [ [[package]] name = "strum" -version = "0.20.0" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7318c509b5ba57f18533982607f24070a55d353e90d4cae30c467cdb2ad5ac5c" +checksum = "aaf86bbcfd1fa9670b7a129f64fc0c9fcbbfe4f1bc4210e9e98fe71ffc12cde2" [[package]] name = "strum_macros" -version = "0.20.1" +version = "0.21.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee8bc6b87a5112aeeab1f4a9f7ab634fe6cbefc4850006df31267f4cfb9e3149" +checksum = "cb076d8b589fde927ec90d05920d610554ca3a4d9dddb177481cadd071a19c2e" dependencies = [ "heck", "proc-macro2", diff --git a/Cargo.toml b/Cargo.toml index 81559e8a..89894b55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,8 +47,8 @@ rayon = "1.5.1" regex = "1.5.4" serde = { version = "1.0.126", features = ["derive"] } serde_json = "1.0.64" -strum = "0.20.0" -strum_macros = "0.20.1" +strum = "0.21.0" +strum_macros = "0.21.0" zip = "0.5.12" lingua-afrikaans-language-model = { path = "language-models/af", version = "1.0.0" } lingua-albanian-language-model = { path = "language-models/sq", version = "1.0.0" } diff --git a/src/isocode.rs b/src/isocode.rs index acf7f873..ee517d81 100644 --- a/src/isocode.rs +++ b/src/isocode.rs @@ -15,12 +15,15 @@ */ use std::fmt::{Debug, Display, Formatter, Result}; +use std::str::FromStr; +use strum_macros::EnumString; /// This enum specifies the ISO 639-1 code representations for the supported languages. /// /// ISO 639 is a standardized nomenclature used to classify languages. -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, EnumString)] #[allow(clippy::upper_case_acronyms)] +#[strum(ascii_case_insensitive)] pub enum IsoCode639_1 { /// The ISO 639-1 code for [`Afrikaans`](./enum.Language.html#variant.Afrikaans) AF, @@ -251,8 +254,9 @@ pub enum IsoCode639_1 { /// This enum specifies the ISO 639-3 code representations for the supported languages. /// /// ISO 639 is a standardized nomenclature used to classify languages. -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, EnumString)] #[allow(clippy::upper_case_acronyms)] +#[strum(ascii_case_insensitive)] pub enum IsoCode639_3 { /// The ISO 639-3 code for [`Afrikaans`](./enum.Language.html#variant.Afrikaans) AFR, @@ -507,4 +511,14 @@ mod tests { fn assert_iso_code_639_3_string_representation_is_correct() { assert_eq!(IsoCode639_3::ENG.to_string(), "eng"); } + + #[test] + fn assert_string_to_iso_code_639_1_is_correct() { + assert_eq!(IsoCode639_1::from_str("en").unwrap(), IsoCode639_1::EN); + } + + #[test] + fn assert_string_to_iso_code_639_3_is_correct() { + assert_eq!(IsoCode639_3::from_str("eng").unwrap(), IsoCode639_3::ENG); + } } diff --git a/src/language.rs b/src/language.rs index f49f4f19..4a1b1cb9 100644 --- a/src/language.rs +++ b/src/language.rs @@ -18,12 +18,14 @@ use crate::alphabet::Alphabet; use crate::isocode::{IsoCode639_1, IsoCode639_3}; use serde::{Deserialize, Serialize}; use std::collections::HashSet; +use std::str::FromStr; use strum::IntoEnumIterator; -use strum_macros::EnumIter; +use strum_macros::{EnumIter, EnumString}; /// This enum specifies the so far 75 supported languages which can be detected by *Lingua*. -#[derive(Clone, Debug, Serialize, Deserialize, EnumIter, Eq, PartialEq, Hash, Ord, PartialOrd)] +#[derive(Clone, Debug, Serialize, Deserialize, EnumIter, Eq, PartialEq, Hash, Ord, PartialOrd, EnumString)] #[serde(rename_all(serialize = "UPPERCASE", deserialize = "UPPERCASE"))] +#[strum(ascii_case_insensitive)] pub enum Language { Afrikaans, Albanian, @@ -434,6 +436,12 @@ mod tests { assert_eq!(deserialized, Language::English); } + #[test] + fn test_from_str() { + let language = Language::from_str("english").unwrap(); + assert_eq!(language, Language::English); + } + #[test] fn assert_all_languages_are_available() { assert_eq!(