mod sequence_index;
pub use self::sequence_index::SequenceIndex;
use std::{
fmt,
num::{self, NonZero},
str::FromStr,
};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Position(NonZero<usize>);
impl Position {
pub const MIN: Self = Self(NonZero::<usize>::MIN);
pub const MAX: Self = Self(NonZero::<usize>::MAX);
pub const fn new(n: usize) -> Option<Self> {
if let Some(m) = NonZero::new(n) {
Some(Self(m))
} else {
None
}
}
pub const fn get(&self) -> usize {
self.0.get()
}
pub const fn checked_add(self, other: usize) -> Option<Self> {
if let Some(n) = self.0.checked_add(other) {
Some(Self(n))
} else {
None
}
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
pub type ParseError = num::ParseIntError;
impl FromStr for Position {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse().map(Self)
}
}
pub type TryFromIntError = num::TryFromIntError;
impl TryFrom<usize> for Position {
type Error = TryFromIntError;
fn try_from(n: usize) -> Result<Self, Self::Error> {
NonZero::try_from(n).map(Position)
}
}
impl From<Position> for usize {
fn from(position: Position) -> Self {
position.0.get()
}
}