Skip to main content

GivenName

Struct GivenName 

Source
pub struct GivenName(/* private fields */);
Expand description

One given name, as the work spells it: Mary, or M. where that is all anybody recorded.

A validating newtype for the same reason Day is one, and the defect that produced it is the sharpest example in this module of the rule the module exists for. The formatter used to reduce given names to initials with a filter_map, so a fragment it could not turn into an initial was droppedgiven = ["Mary", ""] rendered Smith, M. and "Jean--Paul" rendered J.-P.. Both are plausible authors with part of the recorded name missing, and a citation that looks authoritative and is partly invented is exactly what this module refuses to emit. Handling it at the one call site would have left the spelling available; making the value unconstructible means no later renderer can reintroduce it, and serde refuses one on the way in from a file as well.

What is accepted is stated as an allowlist — see is_given_name_char — rather than as a list of the malformed shapes to reject. The first attempt at this was the latter, and a space walked through it: "Mary Ann" was one legal value that rendered Smith, M., dropping Ann by exactly the mechanism the newtype was introduced to close. A rule that enumerates the bad separators is a rule waiting for the next separator.

Accepted: Mary, M., Jean-Paul, Ibáñez, N'Golo. Rejected: the empty string, whitespace alone, Jean--Paul, -Paul, Jean-, "Mary Ann", and anything carrying a digit, a separator or an invisible character.

Also rejected: a generational suffix (Jr., Sr., II, III, IV). Those are not given names, and APA prints them in a position this record has no field for — Smith, J., Jr.. Recorded among the given names they render as an invented middle initial, Smith, J. J., which is the same fabrication by another route. Refusing means a real name is unrecordable until the record grows a field for it, which is the trade this module already makes for a mononym author: a visible refusal beats a quiet wrong answer.

Implementations§

Source§

impl GivenName

Source

pub fn new(text: &str) -> Result<Self, NotAGivenName>

Parse a given name.

Outer whitespace is trimmed and the trimmed form is what is stored: the renderer trims, and a leading space no reader can see must not be able to decide where an entry lands in a list somebody reads. Nothing inside the name is rewritten.

Each hyphen-separated part must begin with a letter and otherwise contain only given-name characters (is_given_name_char). Beginning with a letter is what makes GivenName::initial total: every part has a letter to reduce to, so there is no part it can fail on and therefore none it could be tempted to skip.

§One value is one name

A given name holding internal whitespace is refused, and this is a decision rather than a side effect. "Mary Ann" has two readings — two given names that belong in two elements of given, rendering M. A., or one compound name rendering M. — and a formatter picking between them is guessing at what somebody meant. The caller knows; this type does not. So the ambiguous spelling is not recordable, and the unambiguous one is: given = [GivenName::new("Mary")?, GivenName::new("Ann")?].

The alternative — accept it and render M. A. by splitting on whitespace — was rejected because it makes the compound reading unspellable instead, and a compound given name is a real thing. Refusing leaves both readings expressible, one of them per element; accepting would silently pick one.

§Errors

Returns NotAGivenName for a blank name, a part not starting with a letter, any character outside the allowlist (including whitespace), or a generational suffix.

Source

pub fn as_str(&self) -> &str

The name as recorded.

Source

pub fn parts(&self) -> impl Iterator<Item = &str>

The hyphen-separated parts. Each begins with a letter, by construction, and a part carries no whitespace — so a part is exactly one name and there is exactly one initial to take from it.

Source

pub fn initial(&self) -> String

The initial APA prints for this name: M. for Mary and for M., and J.-P. for Jean-Paul.

It lives on the record rather than in the formatter because two callers need it and they must not be allowed to disagree: the formatter prints it, and Reference::list_order alphabetises on it. APA alphabetises a list by what the list prints, so ordering on the recorded name instead lets Mary and M. — one rendered author, two spellings — come out in an order no reader of the page can account for. One derivation, both callers; two copies of it is how they came apart in the first place.

map rather than filter_map, deliberately: a part is non-blank by construction, and if that ever stopped being true this should produce a visibly wrong initial rather than quietly one part short.

Trait Implementations§

Source§

impl Clone for GivenName

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 Debug for GivenName

Source§

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

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

impl<'de> Deserialize<'de> for GivenName

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for GivenName

Source§

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

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

impl Eq for GivenName

Source§

impl From<GivenName> for String

Source§

fn from(name: GivenName) -> Self

Converts to this type from the input type.
Source§

impl Ord for GivenName

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for GivenName

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 PartialOrd for GivenName

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for GivenName

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for GivenName

Source§

impl TryFrom<String> for GivenName

Source§

type Error = NotAGivenName

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

fn try_from(text: String) -> Result<Self, Self::Error>

Performs the conversion.

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> Same for T

Source§

type Output = T

Should always be Self
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.