use std::{cmp::Ordering, fmt};
#[derive(Clone, Copy, Eq, PartialEq, Default)]
pub struct Span {
pub start: Position,
pub end: Position,
}
impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Span({:?}, {:?})", self.start, self.end)
}
}
impl Ord for Span {
fn cmp(&self, other: &Self) -> Ordering {
(&self.start, &self.end).cmp(&(&other.start, &other.end))
}
}
impl PartialOrd for Span {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Position {
pub offset: usize,
pub line: usize,
pub column: usize,
}
impl Default for Position {
fn default() -> Self {
Self { offset: usize::default(), line: 1, column: 1 }
}
}
impl fmt::Debug for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Position(o: {:?}, l: {:?}, c: {:?})",
self.offset, self.line, self.column
)
}
}
impl Ord for Position {
fn cmp(&self, other: &Self) -> Ordering {
self.offset.cmp(&other.offset)
}
}
impl PartialOrd for Position {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Span {
pub const fn new(start: Position, end: Position) -> Self {
Self { start, end }
}
pub const fn splat(pos: Position) -> Self {
Self::new(pos, pos)
}
pub const fn with_start(self, pos: Position) -> Self {
Self { start: pos, ..self }
}
pub const fn with_end(self, pos: Position) -> Self {
Self { end: pos, ..self }
}
}
impl Position {
pub const fn new(offset: usize, line: usize, column: usize) -> Self {
Self { offset, line, column }
}
}