use crate::encode::EncodedSin;
use crate::error::ParseError;
use crate::letter::Letter;
use crate::side::Side;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Identifier {
letter: Letter,
side: Side,
}
impl Identifier {
#[must_use]
pub const fn new(letter: Letter, side: Side) -> Self {
Self { letter, side }
}
pub const fn parse(input: &str) -> Result<Self, ParseError> {
crate::parse::parse(input)
}
#[must_use]
pub const fn is_valid(input: &str) -> bool {
Self::parse(input).is_ok()
}
#[must_use]
pub fn encode(self) -> EncodedSin {
EncodedSin::from_identifier(self)
}
#[must_use]
pub const fn to_char(self) -> char {
self.letter.to_ascii(self.side) as char
}
#[must_use]
pub const fn letter(self) -> Letter {
self.letter
}
#[must_use]
pub const fn side(self) -> Side {
self.side
}
#[must_use]
pub const fn is_first(self) -> bool {
matches!(self.side, Side::First)
}
#[must_use]
pub const fn is_second(self) -> bool {
matches!(self.side, Side::Second)
}
#[must_use]
pub const fn with_letter(self, letter: Letter) -> Self {
Self::new(letter, self.side)
}
#[must_use]
pub const fn with_side(self, side: Side) -> Self {
Self::new(self.letter, side)
}
#[must_use]
pub const fn flipped(self) -> Self {
self.with_side(self.side.flip())
}
}
impl core::fmt::Display for Identifier {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.encode().as_str())
}
}
impl core::str::FromStr for Identifier {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}
impl TryFrom<&str> for Identifier {
type Error = ParseError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::parse(s)
}
}
impl TryFrom<&[u8]> for Identifier {
type Error = ParseError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
crate::parse::parse_bytes(bytes)
}
}