use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct Position {
pub line: usize,
pub column: usize,
}
impl Position {
pub fn new(line: usize, column: usize) -> Self {
debug_assert!(line > 0, "line number must be greater than 0");
debug_assert!(column > 0, "column number must be greater than 0");
Self { line, column }
}
#[inline]
pub fn line(&self) -> usize {
self.line
}
#[inline]
pub fn column(&self) -> usize {
self.column
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "line: {}, column: {}", self.line, self.column)
}
}
pub trait PositionProvider {
fn position(&self, offset: usize) -> Position;
fn set_offset(&mut self, offset: usize);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_position() {
let pos = Position::new(1, 1);
assert_eq!(pos.line(), 1);
assert_eq!(pos.column(), 1);
assert_eq!(format!("{}", pos), "line: 1, column: 1");
}
}