use pest::{Span, error::LineColLocation};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Location {
pub line: usize,
pub column: usize,
}
impl From<&Span<'_>> for Location {
fn from(s: &Span<'_>) -> Self {
let (line, column) = s.start_pos().line_col();
Self { line, column }
}
}
impl From<LineColLocation> for Location {
fn from(lc: LineColLocation) -> Self {
match lc {
LineColLocation::Pos(pos) => Self {
line: pos.0,
column: pos.1,
},
LineColLocation::Span(start, _) => Self {
line: start.0,
column: start.1,
},
}
}
}
impl Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}