chisel_json/
coords.rs

1//! Coordinate structure used to reference specific locations within parser input
2#![allow(clippy::len_without_is_empty)]
3
4use std::cmp::Ordering;
5use std::fmt::{Display, Formatter};
6
7/// A [Coords] represents a single location within the parser input
8#[derive(Debug, Copy, Clone, PartialEq)]
9pub struct Coords {
10    /// The absolute character position
11    pub absolute: usize,
12    /// The row position
13    pub line: usize,
14    /// The column position
15    pub column: usize,
16}
17
18impl Display for Coords {
19    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
20        write!(
21            f,
22            "[abs: {}, line: {}, column: {}]",
23            self.absolute, self.line, self.column
24        )
25    }
26}
27
28impl Default for Coords {
29    /// The default set of coordinates are positioned at the start of the first row
30    fn default() -> Self {
31        Coords {
32            absolute: 0,
33            line: 0,
34            column: 0,
35        }
36    }
37}
38
39impl Eq for Coords {}
40
41impl PartialOrd<Self> for Coords {
42    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
43        match self.absolute.cmp(&other.absolute) {
44            Ordering::Less => Some(Ordering::Less),
45            Ordering::Equal => Some(Ordering::Equal),
46            Ordering::Greater => Some(Ordering::Greater),
47        }
48    }
49}
50
51impl Ord for Coords {
52    fn cmp(&self, other: &Self) -> Ordering {
53        self.absolute.cmp(&other.absolute)
54    }
55}
56
57/// A [Span] represents a linear interval within the parser input, between to different [Coords]
58#[derive(Debug, Default, Copy, Clone, PartialEq, PartialOrd)]
59pub struct Span {
60    /// Start [Coords] for the span
61    pub start: Coords,
62    /// End [Coords] for the span
63    pub end: Coords,
64}
65
66impl Span {}
67
68impl Display for Span {
69    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
70        write!(f, "start: {}, end: {}", self.start, self.end,)
71    }
72}