Skip to main content

miden_debug_types/
location.rs

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