humanize/
parser.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use language_tags::LanguageTag;
8
9/// Construct `Self` by parsing humanized text.
10pub trait Parse: Sized {
11    /// Perform the conversion.
12    fn parse(text: &str, language: &LanguageTag) -> Option<Self>;
13}
14
15/// Construct a value by parsing humanized text.
16///
17/// This uses a wild card for the language, so text in any language
18/// supported by the library should work.
19pub fn parse<T: Parse>(text: &str) -> Option<T> {
20    let language = LanguageTag::default();
21    T::parse(text, &language)
22}
23
24/// Construct a value by parsing humanized text using the specified language.
25pub fn parse_with_language<T: Parse>(text: &str, language: &LanguageTag) -> Option<T> {
26    T::parse(text, language)
27}