use crate::lexicon::ExternalLexicons;
use crate::locale;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SmallWordPolicy {
Mla,
AlwaysLowercase,
NeverLowercase,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AllCapsPolicy {
Normalize,
Preserve,
NormalizeKnownWords {
unknown: UnknownWordCasing,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum UnknownWordCasing {
Preserve,
TitleCase,
PreserveShortAcronyms {
max_acronym_len: usize,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HyphenStyle {
MlaLike,
CapitalizeBoth,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NameParticlePolicy {
Disabled,
Heuristic,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum LocaleProfile {
English,
Dutch,
French,
German,
Italian,
Spanish,
Turkish,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct TitleCaseOptions<'a> {
pub preserve_existing_caps: bool,
pub capitalize_after_subtitle_boundary: bool,
pub capitalize_phrasal_particles: bool,
pub all_caps_policy: AllCapsPolicy,
pub small_word_policy: SmallWordPolicy,
pub hyphen_style: HyphenStyle,
pub protected_words: &'a [&'a str],
pub external_lexicons: Option<&'a ExternalLexicons>,
pub name_particle_policy: NameParticlePolicy,
pub locale: LocaleProfile,
}
impl<'a> Default for TitleCaseOptions<'a> {
fn default() -> Self {
Self {
preserve_existing_caps: true,
capitalize_after_subtitle_boundary: true,
capitalize_phrasal_particles: true,
all_caps_policy: AllCapsPolicy::Normalize,
small_word_policy: SmallWordPolicy::Mla,
hyphen_style: HyphenStyle::MlaLike,
protected_words: &[],
external_lexicons: None,
name_particle_policy: NameParticlePolicy::Disabled,
locale: LocaleProfile::English,
}
}
}
impl<'a> TitleCaseOptions<'a> {
#[must_use]
pub fn with_protected_words(protected_words: &'a [&'a str]) -> Self {
Self { protected_words, ..Self::default() }
}
#[must_use]
pub fn with_external_lexicons(external_lexicons: &'a ExternalLexicons) -> Self {
Self { external_lexicons: Some(external_lexicons), ..Self::default() }
}
#[must_use]
pub fn with_locale(locale: LocaleProfile) -> Self {
Self {
locale,
name_particle_policy: locale.default_name_particle_policy(),
..Self::default()
}
}
}
impl LocaleProfile {
#[must_use]
pub fn from_bcp47(tag: &str) -> Self {
locale::resolve_locale_profile(tag)
}
#[must_use]
pub const fn bcp47_tag(self) -> &'static str {
locale::locale_tag(self)
}
pub(crate) const fn default_name_particle_policy(self) -> NameParticlePolicy {
locale::default_name_particle_policy(self)
}
}
#[cfg(test)]
mod tests {
use super::{LocaleProfile, NameParticlePolicy, TitleCaseOptions};
#[test]
fn builds_locale_specific_defaults() {
let options = TitleCaseOptions::with_locale(LocaleProfile::Dutch);
assert_eq!(options.locale, LocaleProfile::Dutch);
assert_eq!(options.name_particle_policy, NameParticlePolicy::Heuristic);
}
#[test]
fn resolves_profiles_from_bcp47_tags() {
assert_eq!(LocaleProfile::from_bcp47("fr-FR"), LocaleProfile::French);
assert_eq!(LocaleProfile::from_bcp47("tr"), LocaleProfile::Turkish);
assert_eq!(LocaleProfile::from_bcp47("en-US"), LocaleProfile::English);
}
}