luna_lib/luna_impl/
position.rs

1use std::{
2    fmt::{Debug, Display},
3    ops::Range,
4};
5
6#[derive(Debug, Clone, PartialEq, Default)]
7pub struct Position {
8    pub ln: Range<usize>,
9    pub col: Range<usize>,
10}
11pub struct Located<T> {
12    pub value: T,
13    pub pos: Position,
14}
15pub struct PathLocated<T> {
16    pub value: T,
17    pub path: String,
18    pub pos: Position,
19}
20
21impl Position {
22    pub fn new(ln: Range<usize>, col: Range<usize>) -> Self {
23        Self { ln, col }
24    }
25    pub fn single(ln: usize, col: usize) -> Self {
26        Self {
27            ln: ln..ln + 1,
28            col: col..col + 1,
29        }
30    }
31    pub fn extend(&mut self, other: &Self) {
32        self.ln.end = other.ln.end;
33        self.col.end = other.col.end;
34    }
35}
36impl<T> Located<T> {
37    pub fn new(value: T, pos: Position) -> Self {
38        Self { value, pos }
39    }
40    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Located<U> {
41        Located {
42            value: f(self.value),
43            pos: self.pos,
44        }
45    }
46    pub fn with_path<S: ToString>(self, path: S) -> PathLocated<T> {
47        PathLocated { value: self.value, path: path.to_string(), pos: self.pos }
48    }
49}
50impl<T: Clone> Clone for Located<T> {
51    fn clone(&self) -> Self {
52        Self {
53            value: self.value.clone(),
54            pos: self.pos.clone(),
55        }
56    }
57}
58impl<T: PartialEq> PartialEq for Located<T> {
59    fn eq(&self, other: &Self) -> bool {
60        self.value == other.value
61    }
62}
63impl<T: Default> Default for Located<T> {
64    fn default() -> Self {
65        Self {
66            value: T::default(),
67            pos: Position::default(),
68        }
69    }
70}
71impl<T: Debug> Debug for Located<T> {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        self.value.fmt(f)
74    }
75}
76impl<T: Display> Display for Located<T> {
77    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
78        self.value.fmt(f)
79    }
80}
81
82impl<T> PathLocated<T> {
83    pub fn new(value: T, path: String, pos: Position) -> Self {
84        Self { value, path, pos }
85    }
86    pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Located<U> {
87        Located {
88            value: f(self.value),
89            pos: self.pos,
90        }
91    }
92}
93impl<T: Clone> Clone for PathLocated<T> {
94    fn clone(&self) -> Self {
95        Self {
96            value: self.value.clone(),
97            path: self.path.clone(),
98            pos: self.pos.clone(),
99        }
100    }
101}
102impl<T: PartialEq> PartialEq for PathLocated<T> {
103    fn eq(&self, other: &Self) -> bool {
104        self.value == other.value
105    }
106}
107impl<T: Default> Default for PathLocated<T> {
108    fn default() -> Self {
109        Self {
110            value: T::default(),
111            path: "<input.luna>".to_string(),
112            pos: Position::default(),
113        }
114    }
115}
116impl<T: Debug> Debug for PathLocated<T> {
117    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118        self.value.fmt(f)
119    }
120}
121impl<T: Display> Display for PathLocated<T> {
122    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123        self.value.fmt(f)
124    }
125}