reading_liner/
location.rs1#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
3pub struct Offset(usize);
4
5impl Offset {
6 pub fn new(raw: usize) -> Self {
7 Self(raw)
8 }
9
10 pub fn raw(&self) -> usize {
11 self.0
12 }
13}
14
15impl From<usize> for Offset {
16 fn from(value: usize) -> Self {
17 Offset::new(value)
18 }
19}
20
21pub mod line_column {
22 use std::num::NonZeroUsize;
23
24 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
26 pub struct ZeroBased {
27 pub line: usize,
28 pub column: usize,
29 }
30
31 impl ZeroBased {
32 pub fn new(line: usize, column: usize) -> Self {
33 Self { line, column }
34 }
35
36 pub fn raw(&self) -> (usize, usize) {
37 (self.line, self.column)
38 }
39
40 pub fn one_based(&self) -> OneBased {
42 unsafe {
43 OneBased {
44 line: NonZeroUsize::new_unchecked(self.line + 1),
45 column: NonZeroUsize::new_unchecked(self.column + 1),
46 }
47 }
48 }
49 }
50
51 impl From<(usize, usize)> for ZeroBased {
52 fn from((line, column): (usize, usize)) -> Self {
53 ZeroBased { line, column }
54 }
55 }
56
57 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
59 pub struct OneBased {
60 pub line: NonZeroUsize,
61 pub column: NonZeroUsize,
62 }
63
64 impl OneBased {
65 pub fn new(line: usize, column: usize) -> Option<Self> {
66 let line = NonZeroUsize::new(line)?;
67 let column = NonZeroUsize::new(column)?;
68 Some(Self { line, column })
69 }
70
71 pub fn raw(&self) -> (usize, usize) {
72 (self.line.get(), self.column.get())
73 }
74
75 pub fn zero_based(&self) -> ZeroBased {
77 ZeroBased {
78 line: self.line.get() - 1,
79 column: self.column.get() - 1,
80 }
81 }
82 }
83
84 impl From<(NonZeroUsize, NonZeroUsize)> for OneBased {
85 fn from((line, column): (NonZeroUsize, NonZeroUsize)) -> Self {
86 OneBased { line, column }
87 }
88 }
89}