libgraphql_parser/source_span.rs
1use crate::SourcePosition;
2use std::path::PathBuf;
3
4/// Represents a span of source text from start to end position.
5///
6/// The span is a half-open interval: `[start_inclusive, end_exclusive)`.
7/// - `start_inclusive`: Position of the first character of the source text
8/// - `end_exclusive`: Position immediately after the last character
9///
10/// Optionally includes a file path for the referenced source text.
11#[derive(Clone, Debug, Eq, PartialEq)]
12pub struct SourceSpan {
13 pub start_inclusive: SourcePosition,
14 pub end_exclusive: SourcePosition,
15 /// The file path to the source text this span refers to, if available.
16 pub file_path: Option<PathBuf>,
17}
18
19impl SourceSpan {
20 /// Returns `((start_line, start_col), (end_line, end_col))`
21 /// as 0-based `u32` tuples.
22 ///
23 /// Delegates to
24 /// [`SourcePosition::line_col()`](crate::SourcePosition::line_col)
25 /// on both endpoints.
26 pub fn line_col(&self) -> ((u32, u32), (u32, u32)) {
27 (self.start_inclusive.line_col(), self.end_exclusive.line_col())
28 }
29
30 /// Creates a span without file path information.
31 pub fn new(start: SourcePosition, end: SourcePosition) -> Self {
32 Self {
33 start_inclusive: start,
34 end_exclusive: end,
35 file_path: None,
36 }
37 }
38
39 /// Creates a span with file path information.
40 pub fn with_file(start: SourcePosition, end: SourcePosition, file_path: PathBuf) -> Self {
41 Self {
42 start_inclusive: start,
43 end_exclusive: end,
44 file_path: Some(file_path),
45 }
46 }
47
48 /// Creates a zero-position span with no file path.
49 ///
50 /// Used as a fallback when source position resolution is unavailable
51 /// (e.g. errors constructed without a `SourceMap`).
52 pub fn zero() -> Self {
53 let zero_pos = SourcePosition::new(0, 0, None, 0);
54 Self {
55 start_inclusive: zero_pos,
56 end_exclusive: zero_pos,
57 file_path: None,
58 }
59 }
60}