use std::fmt;
use std::ops::Deref;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
pub start: usize,
pub end: usize,
}
impl Span {
#[must_use]
pub fn line_col(self, source: &str) -> Option<(usize, usize)> {
pest::Position::new(source, self.start).map(|p| p.line_col())
}
}
impl From<pest::Span<'_>> for Span {
fn from(span: pest::Span<'_>) -> Self {
Self {
start: span.start(),
end: span.end(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Spanned<T> {
pub node: T,
pub span: Span,
}
impl<T> Spanned<T> {
#[must_use]
pub const fn new(node: T, span: Span) -> Self {
Self { node, span }
}
}
impl<T> Deref for Spanned<T> {
type Target = T;
fn deref(&self) -> &T {
&self.node
}
}
impl<T: PartialEq> PartialEq for Spanned<T> {
fn eq(&self, other: &Self) -> bool {
self.node == other.node
}
}
impl<T: Eq> Eq for Spanned<T> {}
impl<T: PartialEq> PartialEq<T> for Spanned<T> {
fn eq(&self, other: &T) -> bool {
&self.node == other
}
}
impl<T: fmt::Display> fmt::Display for Spanned<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.node, f)
}
}