Skip to content

Commit

Permalink
Merge pull request #216 from JaminMartin/lang_constructor
Browse files Browse the repository at this point in the history
Added new constructor for languages + tests
  • Loading branch information
a1ien authored Feb 3, 2025
2 parents cd95bb7 + bfac2ec commit a9e3c6c
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ impl Language {
pub fn sub_language(self) -> SubLanguage {
SubLanguage::from_raw(self.primary_language(), self.raw)
}
/// Creates a new `Language` from a raw 16-bit `LANGID`.
///
/// The `LANGID` should follow the USB forum's language identifier specification, where
/// the lower 10 bits represent the sub language and the upper 6 bits represent the primary language.
///
/// # Examples
/// ```
/// use rusb::{Language,SubLanguage,PrimaryLanguage};
/// // Create English (United States) language
/// let lang = Language::new(0x0409);
/// assert_eq!(lang.primary_language(), PrimaryLanguage::English);
/// assert_eq!(lang.sub_language(), SubLanguage::UnitedStates);
/// ```
pub fn new(raw: u16) -> Self {
Language { raw }
}
}

#[doc(hidden)]
Expand Down Expand Up @@ -2555,4 +2571,20 @@ mod test {
SubLanguage::Other(SUB_LANGUAGE_MASK)
);
}

#[test]
fn it_creates_arabic_from_egypt_with_correct_language_id() {
let arabic_egypt = super::Language::new(ARABIC_EGYPT);
assert_eq!(arabic_egypt.primary_language(), PrimaryLanguage::Arabic);
assert_eq!(arabic_egypt.lang_id(), ARABIC_EGYPT);
assert_eq!(arabic_egypt.sub_language(), SubLanguage::Egypt);
}

#[test]
fn it_creates_language_from_hex_english() {
let english = super::Language::new(0x0409); // English (United States)
assert_eq!(english.primary_language(), PrimaryLanguage::English);
assert_eq!(english.lang_id(), 0x0409);
assert_eq!(english.sub_language(), SubLanguage::UnitedStates);
}
}

0 comments on commit a9e3c6c

Please sign in to comment.