miden_core/debuginfo/
location.rs

1use alloc::sync::Arc;
2use core::{fmt, ops::Range};
3
4use super::ByteIndex;
5
6/// A [Location] represents file and span information for portability across source managers
7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct Location {
9    /// The path to the source file in which the relevant source code can be found
10    pub path: Arc<str>,
11    /// The starting byte index (inclusive) of this location
12    pub start: ByteIndex,
13    /// The ending byte index (exclusive) of this location
14    pub end: ByteIndex,
15}
16
17impl Location {
18    /// Creates a new [Location].
19    pub const fn new(path: Arc<str>, start: ByteIndex, end: ByteIndex) -> Self {
20        Self { path, start, end }
21    }
22
23    /// Get the name (or path) of the source file
24    pub fn path(&self) -> Arc<str> {
25        self.path.clone()
26    }
27
28    /// Returns the byte range represented by this location
29    pub const fn range(&self) -> Range<ByteIndex> {
30        self.start..self.end
31    }
32}
33
34/// A [FileLineCol] represents traditional file/line/column information for use in rendering.
35#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub struct FileLineCol {
37    /// The path to the source file in which the relevant source code can be found
38    pub path: Arc<str>,
39    /// The one-indexed number of the line to which this location refers
40    pub line: u32,
41    /// The one-indexed column of the line on which this location starts
42    pub column: u32,
43}
44
45impl FileLineCol {
46    /// Creates a new [Location].
47    pub const fn new(path: Arc<str>, line: u32, column: u32) -> Self {
48        Self { path, line, column }
49    }
50
51    /// Get the name (or path) of the source file
52    pub fn path(&self) -> Arc<str> {
53        self.path.clone()
54    }
55
56    /// Returns the line of the location.
57    pub const fn line(&self) -> u32 {
58        self.line
59    }
60
61    /// Moves the column by the given offset.
62    pub fn move_column(&mut self, offset: u32) {
63        self.column += offset;
64    }
65}
66
67impl fmt::Display for FileLineCol {
68    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69        write!(f, "[{}@{}:{}]", &self.path, self.line, self.column)
70    }
71}