Skip to main content

Identifier

Struct Identifier 

Source
pub struct Identifier { /* private fields */ }
Expand description

A parsed SIN token: a player’s identity at the level of notation.

An Identifier bundles the two attributes a token encodes — a Letter abbreviation and a Side — into a single 2-byte Copy value. Construction from typed components via Identifier::new is total: every combination is a valid token, so it cannot fail.

The derived total ordering compares attributes in the order letter → side.

§Examples

use sashite_sin::{Identifier, Side};

let chinese: Identifier = "c".parse()?;
assert_eq!(chinese.letter().as_char(), 'C');
assert_eq!(chinese.side(), Side::Second);
assert_eq!(chinese.to_char(), 'c');

// Transformations are cheap and infallible; the value is `Copy`.
assert_eq!(chinese.flipped().to_char(), 'C');

Implementations§

Source§

impl Identifier

Source

pub const fn new(letter: Letter, side: Side) -> Self

Builds an identifier from its two typed components.

This is infallible: because each component type is valid by construction, every combination denotes a valid SIN token.

§Examples
use sashite_sin::{Identifier, Letter, Side};

let p = Identifier::new(Letter::try_from_char('C').unwrap(), Side::Second);
assert_eq!(p.encode().as_str(), "c");
Source

pub const fn parse(input: &str) -> Result<Self, ParseError>

Parses a string slice into an identifier.

The whole input must be the token: there is no leading or trailing slack, so surrounding whitespace, a trailing line break or any second character is a rejection rather than something to be trimmed away. The 52 tokens accepted here are the entire domain.

§Errors

Returns ParseError::Empty for an empty input, ParseError::TooLong if input is two bytes or longer, and ParseError::InvalidLetter if it is a single byte that is not an ASCII letter. Length is counted in bytes, so a lone non-ASCII character is TooLong; see ParseError for why that split is where it is.

§Examples
use sashite_sin::Identifier;

let western = Identifier::parse("W")?;
assert!(western.is_first());
assert_eq!(western.letter().as_char(), 'W');

// Nothing is trimmed, and a line break is never absorbed.
assert!(Identifier::parse(" W").is_err());
assert!(Identifier::parse("W\n").is_err());
Source

pub const fn is_valid(input: &str) -> bool

Reports whether input is a valid SIN token, without allocating or constructing an identifier on the caller’s side.

This answers exactly the question Identifier::parse answers — it is defined as that call succeeding — so the two can never disagree about what a token is.

§Examples
use sashite_sin::Identifier;

assert!(Identifier::is_valid("S"));
assert!(Identifier::is_valid("s"));
assert!(!Identifier::is_valid("SS"));
assert!(!Identifier::is_valid(""));
Source

pub const fn encode(self) -> EncodedSin

Returns the canonical, allocation-free string encoding of this token.

Source

pub const fn to_char(self) -> char

Returns the token as its single cased character: uppercase for Side::First, lowercase for Side::Second.

§Examples
use sashite_sin::Identifier;

assert_eq!(Identifier::parse("J")?.to_char(), 'J');
assert_eq!(Identifier::parse("j")?.to_char(), 'j');
Source

pub const fn letter(self) -> Letter

Returns the player-style abbreviation (always uppercase).

Source

pub const fn side(self) -> Side

Returns the side the player belongs to.

Source

pub const fn is_first(self) -> bool

Reports whether the side is Side::First.

Source

pub const fn is_second(self) -> bool

Reports whether the side is Side::Second.

Source

pub const fn with_letter(self, letter: Letter) -> Self

Returns a copy with the abbreviation replaced.

Source

pub const fn with_side(self, side: Side) -> Self

Returns a copy with the side replaced.

Source

pub const fn flipped(self) -> Self

Returns a copy belonging to the opposite Side.

Trait Implementations§

Source§

impl Clone for Identifier

Source§

fn clone(&self) -> Identifier

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 Identifier

Source§

impl Debug for Identifier

Source§

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

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

impl<'de> Deserialize<'de> for Identifier

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 Identifier

Source§

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

Writes the canonical token — the single cased abbreviation letter.

The token goes out through core::fmt::Formatter::pad rather than straight to the underlying buffer, so width, fill, alignment and precision behave exactly as they do for the equivalent str. Writing directly would silently discard those options, which matters as soon as a token is placed in a fixed-width column.

Source§

impl Eq for Identifier

Source§

impl FromStr for Identifier

Source§

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

Parses a token, so "W".parse::<Identifier>() works.

§Errors

Identical to Identifier::parse, which this defers to; the two cannot drift apart.

Source§

type Err = ParseError

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

impl Hash for Identifier

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 Ord for Identifier

Source§

fn cmp(&self, other: &Identifier) -> 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§

impl PartialEq for Identifier

Source§

fn eq(&self, other: &Identifier) -> 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 Identifier

Source§

fn partial_cmp(&self, other: &Identifier) -> 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 Identifier

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 Identifier

Source§

impl TryFrom<&[u8]> for Identifier

Source§

fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>

Parses a token straight from raw bytes, for callers holding a network or file buffer rather than a &str.

No UTF-8 validation happens, and none is needed: a token is a single ASCII byte, so ill-formed input simply fails the same length and letter checks every other entry point applies. This never disagrees with Identifier::parse on bytes that are valid UTF-8, and it accepts nothing that parse would reject.

§Errors

The same variants as Identifier::parse, decided the same way. Being ill-formed is not itself a reason and gets no variant of its own; the input is judged on its length exactly as text would be. A lone 0xFF is ParseError::InvalidLetter because it is one byte that is not a letter, and any two bytes are ParseError::TooLong whether or not they decode.

§Examples
use sashite_sin::{Identifier, ParseError};

assert_eq!(Identifier::try_from(&b"W"[..]).unwrap().to_char(), 'W');

// Not UTF-8, and rejected without a panic or a separate error kind.
assert_eq!(Identifier::try_from(&[0xFF][..]), Err(ParseError::InvalidLetter));
assert_eq!(Identifier::try_from(&[0xC3][..]), Err(ParseError::InvalidLetter));
assert_eq!(Identifier::try_from(&[0xC3, 0xA9][..]), Err(ParseError::TooLong));
Source§

type Error = ParseError

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

impl TryFrom<&str> for Identifier

Source§

fn try_from(s: &str) -> Result<Self, Self::Error>

Parses a token.

§Errors

Identical to Identifier::parse, which this defers to.

Source§

type Error = ParseError

The type returned in the event of a conversion error.

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.