use std::hash::Hash;
use std::hash::Hasher;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Serialize, Deserialize, Clone, Copy, Eq)]
pub struct Location {
pub line: u32,
pub column: u32,
}
impl Default for Location {
fn default() -> Self {
Self { line: 1, column: 1 }
}
}
impl Location {
pub fn new(line: u32, column: u32) -> Self {
Self { line, column }
}
pub fn shift_down(&mut self, lines: u32, column: u32) {
if lines == 0 {
self.shift_right(column);
return;
}
self.line += lines;
self.column = column;
}
pub fn shift_right(&mut self, columns: u32) {
self.column += columns;
}
}
impl PartialEq for Location {
fn eq(&self, other: &Self) -> bool {
self.line == other.line && self.column == other.column
}
}
impl Hash for Location {
fn hash<H: Hasher>(&self, state: &mut H) {
self.line.hash(state);
self.column.hash(state);
}
}
impl std::fmt::Display for Location {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}