shape/location.rs
1use std::fmt::Debug;
2use std::hash::Hash;
3use std::ops::Range;
4use std::sync::Arc;
5
6use apollo_compiler::parser::FileId;
7
8/// Some identifier of the source text that a [`Location`] refers to.
9/// The content only matters to the caller, who will need to be able to use this to give
10/// diagnostics to users.
11///
12/// This might be a file name, reference to an editor panel, or a unique identifier for a
13/// sub-slice of a larger text. It could also be the string content itself.
14#[derive(Clone, Debug, Hash, PartialEq, Eq)]
15pub enum SourceId {
16 /// A key into [`apollo_compiler::Schema`] sources.
17 GraphQL(FileId),
18 /// An arbitrary source identifier from a consumer.
19 Other(Arc<str>),
20}
21
22impl SourceId {
23 pub fn new(id: impl Into<Arc<str>>) -> Self {
24 Self::Other(id.into())
25 }
26
27 /// Create a new [`Location`] within this source text.
28 #[must_use]
29 pub fn location(&self, span: Range<usize>) -> Location {
30 Location {
31 source_id: self.clone(),
32 span,
33 }
34 }
35}
36
37/// The location of a [`crate::Shape`] within the source text that produced it.
38#[derive(Clone, Debug, Hash, PartialEq, Eq)]
39pub struct Location {
40 /// Some identifier correlating `span` to a string the caller controls
41 pub source_id: SourceId,
42 /// The character offset within the source text. 0-indexed, end is exclusive.
43 pub span: Range<usize>,
44}