use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Position(pub u32, pub u32);
impl Position {
pub fn new(line: u32, column: u32) -> Self {
Self(line, column)
}
pub fn line(&self) -> u32 {
self.0
}
pub fn column(&self) -> u32 {
self.1
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Range {
pub start: Position,
pub end: Position,
}
impl Range {
pub fn new(start: Position, end: Position) -> Self {
Self { start, end }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn position_serialises_as_array() {
let p = Position::new(12, 4);
let s = serde_json::to_string(&p).unwrap();
assert_eq!(s, "[12,4]");
}
#[test]
fn position_deserialises_from_array() {
let p: Position = serde_json::from_str("[12,4]").unwrap();
assert_eq!(p, Position::new(12, 4));
}
#[test]
fn range_round_trips() {
let r = Range::new(Position::new(1, 2), Position::new(3, 4));
let s = serde_json::to_string(&r).unwrap();
assert_eq!(s, r#"{"start":[1,2],"end":[3,4]}"#);
let back: Range = serde_json::from_str(&s).unwrap();
assert_eq!(back, r);
}
}