1use serde::{Deserialize, Serialize};
2use std::fmt::{Display, Formatter};
3use url::Url;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
7pub struct SourcePosition {
8 pub line: u32,
10 pub column: u32,
12 pub offset: u64,
14}
15
16impl SourcePosition {
17 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#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Default)]
36pub struct SourceLocation {
37 pub url: Option<Url>,
39 pub start: SourcePosition,
41 pub end: SourcePosition,
43}
44
45impl SourceLocation {
46 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#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
70pub struct QualifiedName {
71 pub namespace: Vec<String>,
73 pub name: String,
75}
76
77impl QualifiedName {
78 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}