1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
use super::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use core::fmt;
// SOURCE LOCATION
// ================================================================================================
/// A struct containing information about the location of a source item.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SourceLocation {
// TODO add uri
line: u32,
column: u32,
}
impl Default for SourceLocation {
fn default() -> Self {
Self { line: 1, column: 1 }
}
}
impl SourceLocation {
// CONSTRUCTORS
// -------------------------------------------------------------------------------------------------
/// Creates a new instance of [SourceLocation].
pub const fn new(line: u32, column: u32) -> Self {
Self { line, column }
}
// PUBLIC ACCESSORS
// -------------------------------------------------------------------------------------------------
/// Returns the line of the location.
pub const fn line(&self) -> u32 {
self.line
}
// STATE MUTATORS
// -------------------------------------------------------------------------------------------------
/// Moves the column by the given offset.
pub fn move_column(&mut self, offset: u32) {
self.column += offset;
}
}
impl fmt::Display for SourceLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}:{}]", self.line, self.column)
}
}
impl Serializable for SourceLocation {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_u32(self.line);
target.write_u32(self.column);
}
}
impl Deserializable for SourceLocation {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let line = source.read_u32()?;
let column = source.read_u32()?;
Ok(Self { line, column })
}
}