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    pub fn new(line: u32, column: u32, offset: u64) -> Self {
18        Self { line, column, offset }
19    }
20}
21
22impl Display for SourcePosition {
23    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
24        write!(f, "{}:{}", self.line + 1, self.column + 1)
25    }
26}
27
28/// Source location in a file.
29#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
30pub struct SourceLocation {
31    /// URL of the source file
32    pub url: Option<Url>,
33    /// Start position
34    pub start: SourcePosition,
35    /// End position
36    pub end: SourcePosition,
37}
38
39impl SourceLocation {
40    pub fn new(url: Option<Url>, start: SourcePosition, end: SourcePosition) -> Self {
41        Self { url, start, end }
42    }
43}
44
45impl Display for SourceLocation {
46    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47        if let Some(url) = &self.url {
48            write!(f, "{} at {}", url, self.start)
49        }
50        else {
51            write!(f, "at {}", self.start)
52        }
53    }
54}
55
56/// A qualified name for symbols.
57#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
58pub struct QualifiedName {
59    pub namespace: Vec<String>,
60    pub name: String,
61}
62
63impl QualifiedName {
64    pub fn new(namespace: Vec<String>, name: String) -> Self {
65        Self { namespace, name }
66    }
67}
68
69impl Display for QualifiedName {
70    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
71        for ns in &self.namespace {
72            write!(f, "{}::", ns)?;
73        }
74        write!(f, "{}", self.name)
75    }
76}