1use std::fmt::{Display, Formatter};
2
3use super::Position;
4
5impl Position {
6 pub fn new(line: usize, column: usize) -> Self {
8 Self { line, column }
9 }
10}
11
12impl From<&[u8]> for Position {
13 fn from(code: &[u8]) -> Self {
14 let mut pointer: usize = 0;
15 let mut line: usize = 0;
16 for c in code {
17 if *c == b'\n' {
18 line += 1;
19 }
20 pointer += 1;
21 }
22 let s = unsafe { std::str::from_utf8_unchecked(&code[pointer..]) };
23 Position::new(line + 1, s.len() + 1)
24 }
25}
26
27impl Display for Position {
28 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29 f.debug_struct("")
30 .field("line", &self.line)
31 .field("column", &self.column)
32 .finish()
33 }
34}