use core::{fmt, ops::Range};
use super::{
ByteIndex, FileName,
source_file::{ColumnNumber, LineNumber},
};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Location {
pub uri: FileName,
pub start: ByteIndex,
pub end: ByteIndex,
}
impl Location {
pub const fn new(uri: FileName, start: ByteIndex, end: ByteIndex) -> Self {
Self { uri, start, end }
}
pub fn uri(&self) -> &FileName {
&self.uri
}
pub const fn range(&self) -> Range<ByteIndex> {
self.start..self.end
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FileLineCol {
pub uri: FileName,
pub line: LineNumber,
pub column: ColumnNumber,
}
impl FileLineCol {
pub fn new(
uri: FileName,
line: impl Into<LineNumber>,
column: impl Into<ColumnNumber>,
) -> Self {
Self {
uri,
line: line.into(),
column: column.into(),
}
}
pub fn uri(&self) -> &FileName {
&self.uri
}
pub const fn line(&self) -> LineNumber {
self.line
}
pub fn move_column(&mut self, offset: i32) {
self.column += offset;
}
}
impl fmt::Display for FileLineCol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}@{}:{}]", &self.uri, self.line, self.column)
}
}