1
/// This module defines types which role is to abstract the different
2
/// coordinate and indexing systems the editor has to support.
3
/// Humans deal with line numbers, starting at 1, which maps to document
4
/// rows, starting at 0. We define simple types in order to delegate the
5
/// +1/-1 conversions to them, as it has proven easy to forget these only
6
/// when dealing with usize values.
7

            
8
4
#[derive(Debug, PartialEq, Clone, Copy)]
9
pub struct RowIndex {
10
2
    pub value: usize,
11
}
12

            
13
impl From<LineNumber> for RowIndex {
14
92
    fn from(ln: LineNumber) -> Self {
15
92
        Self::new(ln.value.saturating_sub(1))
16
92
    }
17
}
18

            
19
impl RowIndex {
20
    #[must_use]
21
412
    pub fn new(value: usize) -> Self {
22
412
        Self { value }
23
824
    }
24

            
25
    #[must_use]
26
24
    pub fn next(&self) -> Self {
27
24
        Self::new(self.value.saturating_add(1))
28
24
    }
29

            
30
    #[must_use]
31
6
    pub fn previous(&self) -> Self {
32
6
        Self::new(self.value.saturating_sub(1))
33
6
    }
34
}
35

            
36
68
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
37
pub struct LineNumber {
38
34
    pub value: usize,
39
}
40

            
41
impl From<RowIndex> for LineNumber {
42
14
    fn from(ri: RowIndex) -> Self {
43
14
        Self::new(ri.value.saturating_add(1))
44
14
    }
45
}
46

            
47
impl LineNumber {
48
    #[must_use]
49
243
    pub fn new(value: usize) -> Self {
50
243
        Self { value }
51
486
    }
52

            
53
    #[must_use]
54
6
    pub fn add(&self, value: usize) -> Self {
55
6
        Self::new(self.value.saturating_add(value))
56
6
    }
57

            
58
    #[must_use]
59
21
    pub fn sub(&self, value: usize) -> Self {
60
21
        Self::new(self.value.saturating_sub(value))
61
21
    }
62

            
63
    #[must_use]
64
4
    pub fn next(&self) -> Self {
65
4
        self.add(1)
66
4
    }
67

            
68
    #[must_use]
69
8
    pub fn previous(&self) -> Self {
70
8
        self.sub(1)
71
8
    }
72
}