use std::fmt;
use serde::Deserialize;
use serde::Serialize;
#[derive(Debug, Default, Ord, PartialOrd, Eq, PartialEq, Clone, Copy, Hash, Deserialize, Serialize, ts_rs::TS)]
#[ts(export)]
pub struct ModuleId(u32);
impl ModuleId {
pub fn from_usize(id: usize) -> Self {
Self(u32::try_from(id).expect("module ID should fit in a u32"))
}
pub fn as_usize(&self) -> usize {
usize::try_from(self.0).expect("module ID should fit in a usize")
}
pub fn is_top_level(&self) -> bool {
*self == Self::default()
}
}
impl std::fmt::Display for ModuleId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Default, Deserialize, Serialize, PartialEq, Copy, Clone, ts_rs::TS, Hash, Eq)]
#[ts(export, type = "[number, number, number]")]
pub struct SourceRange([usize; 3]);
impl From<[usize; 3]> for SourceRange {
fn from(value: [usize; 3]) -> Self {
Self(value)
}
}
impl Ord for SourceRange {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let module_id_cmp = self.module_id().cmp(&other.module_id());
if module_id_cmp != std::cmp::Ordering::Equal {
return module_id_cmp;
}
let start_cmp = self.start().cmp(&other.start());
if start_cmp != std::cmp::Ordering::Equal {
return start_cmp;
}
self.end().cmp(&other.end())
}
}
impl PartialOrd for SourceRange {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<&SourceRange> for miette::SourceSpan {
fn from(source_range: &SourceRange) -> Self {
let length = source_range.end() - source_range.start();
let start = miette::SourceOffset::from(source_range.start());
Self::new(start, length)
}
}
impl From<SourceRange> for miette::SourceSpan {
fn from(source_range: SourceRange) -> Self {
Self::from(&source_range)
}
}
impl SourceRange {
pub fn new(start: usize, end: usize, module_id: ModuleId) -> Self {
Self([start, end, module_id.as_usize()])
}
pub fn synthetic() -> Self {
Self::default()
}
pub fn merge(mut ranges: impl Iterator<Item = SourceRange>) -> Self {
let mut result = ranges.next().unwrap_or_default();
for r in ranges {
debug_assert!(r.0[2] == result.0[2], "Merging source ranges from different files");
if r.0[0] < result.0[0] {
result.0[0] = r.0[0]
}
if r.0[1] > result.0[1] {
result.0[1] = r.0[1];
}
}
result
}
pub fn is_synthetic(&self) -> bool {
self.start() == 0 && self.end() == 0
}
pub fn start(&self) -> usize {
self.0[0]
}
pub fn start_as_range(&self) -> Self {
Self([self.0[0], self.0[0], self.0[2]])
}
pub fn end(&self) -> usize {
self.0[1]
}
pub fn module_id(&self) -> ModuleId {
ModuleId::from_usize(self.0[2])
}
pub fn is_top_level_module(&self) -> bool {
self.module_id().is_top_level()
}
pub fn contains(&self, pos: usize) -> bool {
pos >= self.start() && pos <= self.end()
}
pub fn contains_range(&self, other: &Self) -> bool {
self.module_id() == other.module_id() && self.start() <= other.start() && self.end() >= other.end()
}
}