miden_debug_types/
location.rs

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