1mod sequence_index;
4
5pub use self::sequence_index::SequenceIndex;
6
7use std::{
8 fmt,
9 num::{self, NonZeroUsize},
10 str::FromStr,
11};
12
13#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
15pub struct Position(NonZeroUsize);
16
17impl Position {
18 pub const MIN: Self = Self(NonZeroUsize::MIN);
20
21 pub const MAX: Self = Self(NonZeroUsize::MAX);
23
24 pub const fn new(n: usize) -> Option<Self> {
34 if let Some(m) = NonZeroUsize::new(n) {
35 Some(Self(m))
36 } else {
37 None
38 }
39 }
40
41 pub const fn get(&self) -> usize {
50 self.0.get()
51 }
52
53 pub const fn checked_add(self, other: usize) -> Option<Self> {
66 if let Some(n) = self.0.checked_add(other) {
67 Some(Self(n))
68 } else {
69 None
70 }
71 }
72}
73
74impl fmt::Display for Position {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 self.0.fmt(f)
77 }
78}
79
80pub type ParseError = num::ParseIntError;
82
83impl FromStr for Position {
84 type Err = ParseError;
85
86 fn from_str(s: &str) -> Result<Self, Self::Err> {
87 s.parse().map(Self)
88 }
89}
90
91pub type TryFromIntError = num::TryFromIntError;
93
94impl TryFrom<usize> for Position {
95 type Error = TryFromIntError;
96
97 fn try_from(n: usize) -> Result<Self, Self::Error> {
98 NonZeroUsize::try_from(n).map(Position)
99 }
100}
101
102impl From<Position> for usize {
103 fn from(position: Position) -> Self {
104 position.0.get()
105 }
106}