smt_lang/parser/
position.rs1use line_col::LineColLookup;
2
3#[derive(Clone, Debug, PartialEq, Eq, Hash)]
4pub struct Position {
5 pub file: String,
6 pub line: usize,
7 pub column: usize,
8}
9
10impl Position {
11 pub fn new<S: Into<String>>(file: S, lookup: &LineColLookup, offset: usize) -> Self {
12 let file = file.into();
13 let (line, column) = lookup.get(offset);
14 Self { file, line, column }
15 }
16
17 pub fn to_text(&self) -> d_stuff::Text {
18 d_stuff::Text::new(
19 format!("{}", self),
20 termion::style::Reset.to_string(),
21 termion::color::Cyan.fg_str(),
22 )
23 }
24
25 pub fn to_message(&self) -> d_stuff::Message {
26 d_stuff::Message::new(
27 Some(d_stuff::Text::new(
28 "File",
29 termion::style::Reset.to_string(),
30 termion::color::White.fg_str(),
31 )),
32 self.to_text(),
33 )
34 }
35}
36
37impl std::fmt::Display for Position {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 write!(f, "{}:{}:{}", self.file, self.line, self.column)
40 }
41}