Skip to main content

Language

Enum Language 

Source
#[non_exhaustive]
pub enum Language {
Show 25 variants Afrikaans, Breton, Catalan, Welsh, Danish, German, English, Esperanto, Spanish, French, Irish, Galician, Hungarian, Icelandic, Italian, Latin, Luxembourgish, Maltese, Dutch, Norwegian, Occitan, Portuguese, Romanian, Albanian, Swedish,
}
Expand description

The language of a work, for csan’s language-dependent filing rules.

Parse one from an ISO 639 code, a BCP 47 tag or a POSIX locale with FromStr ("pt-BR".parse()), or from an optional user input with Language::from_optional. It displays as its canonical code. Language::strip_initial_article removes a title’s initial article in that language; the table gives each language’s codes and articles.

LanguageCodesArticles
Afrikaansaf, afrdie, 'n
Bretonbr, brean, al, ar, un, ul, ur, eun, eul, eur
Catalan (Valencian)ca, catel, els, la, les, l', un, una
Welshcy, cym, wely, yr
Danishda, dande, den, det, en, et
Germande, deu, gerder, die, das, ein, eine
Englishen, enga, an, the
Esperantoeo, epola
Spanishes, spael, la, los, las, lo, un, una
Frenchfr, fra, frele, la, les, l', un, une
Irishga, glean, na
Galiciangl, glgo, a, os, as, un, unha
Hungarianhu, huna, az, egy
Icelandicis, isl, icehinn, hin, hið, hinir, hinar
Italianit, itail, lo, la, l', i, gli, gl', le, un, uno, una, un'
Latinla, lat
Luxembourgishlb, ltzden, de, d', deen, dat, déi, en, e, eng
Maltesemt, mltil-, l-, iċ-, id-, in-, ir-, is-, it-, ix-, iż-, iz-, ic-
Dutch (Flemish)nl, nld, dutde, het, een, eene, 't, 'n
Norwegianno, nor, nb, nob, nn, nnode, dei, den, det, e, ei, ein, eit, en, et
Occitan (Provençal)oc, oci, proil, l', la, las, le, les, lh, lhi, li, lis, lo, los, lou, lu, lei, leis, un, una, uno, uns, us
Portuguesept, poro, a, os, as, um, uma
Romanian (Moldavian)ro, ron, rum, mo, molun, o
Albaniansq, sqi, alb, als, aln, aae, aat
Swedishsv, swede, den, det, en, ett

An empty Articles cell means the language has no initial articles: its codes are accepted, and nothing is stripped.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Afrikaans

Afrikaans, code af.

§

Breton

Breton, code br.

§

Catalan

Catalan (Valencian), code ca.

§

Welsh

Welsh, code cy.

§

Danish

Danish, code da.

§

German

German, code de.

§

English

English, code en.

§

Esperanto

Esperanto, code eo.

§

Spanish

Spanish, code es.

§

French

French, code fr.

§

Irish

Irish, code ga. Its articles may be followed by a t or h mutation prefix, which is disregarded with them.

§

Galician

Galician, code gl.

§

Hungarian

Hungarian, code hu.

§

Icelandic

Icelandic, code is.

§

Italian

Italian, code it.

§

Latin

Latin, code la. It has no articles (LC G 100 §9): its codes are accepted, and nothing is stripped.

§

Luxembourgish

Luxembourgish, code lb.

§

Maltese

Maltese, code mt. Its article is joined to the noun by a hyphen.

§

Dutch

Dutch (Flemish), code nl.

§

Norwegian

Norwegian, code no. Bokmål and Nynorsk, in one list.

§

Occitan

Occitan (Provençal), code oc.

§

Portuguese

Portuguese, code pt.

§

Romanian

Romanian (Moldavian), code ro.

§

Albanian

Albanian, code sq. It has no initial articles (LC G 100 §9): its codes are accepted, and nothing is stripped.

§

Swedish

Swedish, code sv.

Implementations§

Source§

impl Language

Source

pub const ALL: &'static [Self]

Every supported language, ordered by code.

Source

pub const fn name(self) -> &'static str

The English name, e.g. "Afrikaans", "Catalan (Valencian)".

Source

pub const fn code(self) -> &'static str

The canonical code: the first of codes, the ISO 639-1 code (e.g. "en"). It is also what Display prints.

Source

pub const fn codes(self) -> &'static [&'static str]

Every accepted code, lowercase, canonical first, e.g. ["nl", "nld", "dut"].

Source

pub const fn articles(self) -> &'static [&'static str]

The articles recognized at the start of a title: lowercase, precomposed (NFC), as written. Elided forms end in an apostrophe (l') and joined forms in a hyphen (Maltese il-). Irish returns ["an", "na"]; the t/h prefixes that follow them are a rule, not list entries. Albanian and Latin return [].

Source§

impl Language

Source

pub fn strip_initial_article(self, title: &str) -> Option<&str>

title without its initial article, when it starts with one that this language recognizes and a letter or ASCII digit follows; otherwise None, including for a title that is only an article. The result is a subslice of title, so nothing is allocated.

The article may follow anything but a letter or ASCII digit: spaces, punctuation, symbols ("¿El otoño?", "'t Hooft" in quotes). A word article must then be followed by whitespace ("The, Bear" keeps its The), an elided one by anything ("L'amour", "L' amour"), and a joined one by a non-space ("il-Kantilena"). The result starts at the first letter or ASCII digit after it, since MARC counts the spaces and punctuation around an initial article as nonfiling too. Letters match regardless of case but with their diacritics, an apostrophe matches its typographic variants, and a hyphen its Unicode forms. Irish also loses the t/h mutation prefix after its article. Only one article is removed.

Three limits follow from these rules. A digit other than an ASCII one doesn’t end the leading run, so "1 The Bear" (a fullwidth one) loses its The. An invisible format character between an article and its space (U+200B, U+00AD, U+FEFF) hides the article, so nothing is stripped. And an article in another Unicode form (decomposed, or fullwidth "The") doesn’t match: there is no normalization.

§Examples
use csan_core::Language;

let english = Language::English;
assert_eq!(english.strip_initial_article("The Bear"), Some("Bear"));
assert_eq!(english.strip_initial_article("\"The 100 best\""), Some("100 best\""));
assert_eq!(english.strip_initial_article("Theory"), None);
assert_eq!(english.strip_initial_article("The"), None);

let french = Language::French;
assert_eq!(french.strip_initial_article("L’Étranger"), Some("Étranger"));
let irish = Language::Irish;
assert_eq!(irish.strip_initial_article("An tUasal"), Some("Uasal"));
Source

pub fn from_optional( code: Option<&str>, ) -> Result<Option<Self>, UnknownLanguage>

The front door for frontends, like Attribution::from_parts: None and blank strings mean no language (Ok(None)), and anything else parses with FromStr.

§Errors

Returns UnknownLanguage for a non-blank code that no supported language uses.

§Examples
use csan_core::Language;

assert_eq!(Language::from_optional(None), Ok(None));
assert_eq!(Language::from_optional(Some("  ")), Ok(None));
assert_eq!(
    Language::from_optional(Some("pt-BR")),
    Ok(Some(Language::Portuguese))
);
assert!(Language::from_optional(Some("xx")).is_err());
Source

pub const fn for_rule( language: Option<Self>, enabled: bool, ) -> Result<Option<Self>, MissingLanguage>

The language that a rule applies with, for frontends that take a language and an on/off switch per rule (the CLI’s --lang and --strip-article): Ok(None) when the switch is off, and the language when it is on.

A language with the switch off is not an error, so that a frontend can set the language once (Typst’s cutter-sanborn.with(lang: "pt"), a shell alias) and turn rules on per call.

§Errors

Returns MissingLanguage when the switch is on and no language is given.

§Examples
use csan_core::{Language, MissingLanguage};

let english = Some(Language::English);
assert_eq!(Language::for_rule(english, true), Ok(english));
assert_eq!(Language::for_rule(english, false), Ok(None));
assert_eq!(Language::for_rule(None, false), Ok(None));
assert_eq!(Language::for_rule(None, true), Err(MissingLanguage));

Trait Implementations§

Source§

impl Clone for Language

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Language

Source§

impl Debug for Language

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Language

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Writes the canonical code (e.g. en), honouring width and fill.

Source§

impl Eq for Language

Source§

impl FromStr for Language

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses an ISO 639 code, a BCP 47 tag or a POSIX locale, in any case.

The input is trimmed and cut at its first -, _, . or @, and that primary subtag is looked up among every language’s codes: "pt-BR", "ca-ES-valencia", "ca@valencia" and "pt_BR.UTF-8" all resolve. BCP 47 tags and POSIX locales are ASCII, so non-ASCII input is an error anywhere after trimming ("en-é").

§Examples
use csan_core::Language;

assert_eq!("EN".parse(), Ok(Language::English));
assert_eq!("dut".parse(), Ok(Language::Dutch));
assert_eq!("ca-valencia".parse(), Ok(Language::Catalan));
assert!("val".parse::<Language>().is_err()); // ISO 639-3: Vehes
Source§

type Err = UnknownLanguage

The associated error which can be returned from parsing.
Source§

impl Hash for Language

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Language

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Language

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.