Skip to main content

gaia_types/
source.rs

1use serde::{Deserialize, Serialize};
2use std::fmt::{Display, Formatter};
3use url::Url;
4
5/// Source position in a file.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
7pub struct SourcePosition {
8    /// Line number (0-indexed)
9    pub line: u32,
10    /// Column number (0-indexed)
11    pub column: u32,
12    /// Byte offset (0-indexed)
13    pub offset: u64,
14}
15
16impl SourcePosition {
17    /// Creates a new source position.
18    ///
19    /// # Arguments
20    /// * `line` - Line number (0-indexed)
21    /// * `column` - Column number (0-indexed)
22    /// * `offset` - Byte offset (0-indexed)
23    pub fn new(line: u32, column: u32, offset: u64) -> Self {
24        Self { line, column, offset }
25    }
26}
27
28impl Display for SourcePosition {
29    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30        write!(f, "{}:{}", self.line + 1, self.column + 1)
31    }
32}
33
34/// Source location in a file.
35#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
36pub struct SourceLocation {
37    /// URL of the source file
38    pub url: Option<Url>,
39    /// Start position
40    pub start: SourcePosition,
41    /// End position
42    pub end: SourcePosition,
43}
44
45impl SourceLocation {
46    /// Creates a new source location.
47    ///
48    /// # Arguments
49    /// * `url` - URL of the source file
50    /// * `start` - Start position
51    /// * `end` - End position
52    pub fn new(url: Option<Url>, start: SourcePosition, end: SourcePosition) -> Self {
53        Self { url, start, end }
54    }
55}
56
57impl Display for SourceLocation {
58    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
59        if let Some(url) = &self.url {
60            write!(f, "{} at {}", url, self.start)
61        }
62        else {
63            write!(f, "at {}", self.start)
64        }
65    }
66}
67
68/// A qualified name for symbols.
69#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
70pub struct QualifiedName {
71    /// Namespace components
72    pub namespace: Vec<String>,
73    /// Symbol name
74    pub name: String,
75}
76
77impl QualifiedName {
78    /// Creates a new qualified name.
79    ///
80    /// # Arguments
81    /// * `namespace` - Namespace components
82    /// * `name` - Symbol name
83    pub fn new(namespace: Vec<String>, name: String) -> Self {
84        Self { namespace, name }
85    }
86}
87
88impl Display for QualifiedName {
89    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
90        for ns in &self.namespace {
91            write!(f, "{}::", ns)?;
92        }
93        write!(f, "{}", self.name)
94    }
95}