use std::{
fmt::{Debug, Display, Error, Formatter},
ops::Range,
path::PathBuf,
sync::Arc,
};
use derive_builder::Builder;
#[derive(Builder, Debug, Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Location {
#[builder(default)]
pub start_line: u32,
#[builder(default)]
pub start_column: u32,
#[builder(default)]
pub end_line: u32,
#[builder(default)]
pub end_column: u32,
#[builder(default)]
pub start: u32,
#[builder(default)]
pub end: u32,
pub file_name: Arc<PathBuf>,
}
impl Location {
#[inline]
pub fn len(&self) -> usize {
self.end.saturating_sub(self.start) as usize
}
#[inline]
pub fn is_empty(&self) -> bool {
self.start >= self.end
}
#[inline]
pub fn range(&self) -> Range<usize> {
self.start as usize..self.end as usize
}
#[inline]
pub fn start(&self) -> usize {
self.start as usize
}
#[inline]
pub fn end(&self) -> usize {
self.end as usize
}
}
impl Display for Location {
fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
if self.start_line == self.end_line {
write!(
f,
"{}:{}:{}-{}",
self.file_name.display(),
self.start_line,
self.start_column,
self.end_column
)
} else {
write!(
f,
"{}:{}:{}-{}:{}",
self.file_name.display(),
self.start_line,
self.start_column,
self.end_line,
self.end_column
)
}
}
}
impl From<&Location> for Range<usize> {
fn from(location: &Location) -> Self {
location.range()
}
}