use sashite_pin::{Letter, Side, State};
use crate::encode::EncodedEpin;
use crate::error::ParseError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Identifier {
pin: sashite_pin::Identifier,
derived: bool,
}
impl Identifier {
#[must_use]
pub const fn new(pin: sashite_pin::Identifier, derived: bool) -> Self {
Self { pin, derived }
}
pub fn parse(input: &str) -> Result<Self, ParseError> {
crate::parse::parse(input)
}
#[must_use]
pub fn is_valid(input: &str) -> bool {
Self::parse(input).is_ok()
}
#[must_use]
pub fn encode(self) -> EncodedEpin {
EncodedEpin::from_identifier(self)
}
#[must_use]
pub const fn pin(self) -> sashite_pin::Identifier {
self.pin
}
#[must_use]
pub const fn letter(self) -> Letter {
self.pin.letter()
}
#[must_use]
pub const fn side(self) -> Side {
self.pin.side()
}
#[must_use]
pub const fn state(self) -> State {
self.pin.state()
}
#[must_use]
pub const fn is_terminal(self) -> bool {
self.pin.is_terminal()
}
#[must_use]
pub const fn is_first(self) -> bool {
self.pin.is_first()
}
#[must_use]
pub const fn is_second(self) -> bool {
self.pin.is_second()
}
#[must_use]
pub const fn is_native(self) -> bool {
!self.derived
}
#[must_use]
pub const fn is_derived(self) -> bool {
self.derived
}
#[must_use]
pub const fn native(self) -> Self {
Self::new(self.pin, false)
}
#[must_use]
pub const fn derive(self) -> Self {
Self::new(self.pin, true)
}
#[must_use]
pub const fn with_pin(self, pin: sashite_pin::Identifier) -> Self {
Self::new(pin, self.derived)
}
}
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)
}
}