use crate::{Indexer, LineIndexer};
use alloc::sync::Arc;
use core::{fmt, ops::Range};
pub trait Span: Clone {
type Uri: PartialEq + Clone + fmt::Display;
type Source: AsRef<str> + Clone;
type Index: Indexer + ?Sized;
fn start(&self) -> usize;
fn end(&self) -> usize;
fn range(&self) -> Range<usize> {
self.start()..self.end()
}
fn source_text(&self) -> &Self::Source;
fn source_index(&self) -> &Self::Index;
fn uri(&self) -> &Self::Uri;
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SimpleSpan {
uri: Arc<str>,
source: Arc<str>,
indexer: Arc<LineIndexer>,
start: usize,
end: usize,
}
impl SimpleSpan {
pub fn new(
uri: impl Into<Arc<str>>,
source: impl Into<Arc<str>>,
start: usize,
end: usize,
) -> Self {
let uri = uri.into();
let source = source.into();
let indexer = LineIndexer::new(&source).into();
Self {
uri,
source,
indexer,
start,
end,
}
}
}
impl Span for SimpleSpan {
type Uri = Arc<str>;
type Source = Arc<str>;
type Index = Arc<LineIndexer>;
fn start(&self) -> usize {
self.start
}
fn end(&self) -> usize {
self.end
}
fn source_text(&self) -> &Self::Source {
&self.source
}
fn source_index(&self) -> &Self::Index {
&self.indexer
}
fn uri(&self) -> &Self::Uri {
&self.uri
}
}
impl Default for SimpleSpan {
fn default() -> Self {
Self::new("", "", 0, 0)
}
}
impl From<&SimpleSpan> for SimpleSpan {
fn from(value: &SimpleSpan) -> Self {
value.clone()
}
}