1use crate::ast;
2use std::boxed::Box;
3use std::path::Path;
4use std::path::PathBuf;
5
6#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
9pub struct FilePosition {
10 pub col: usize,
11 pub file: Box<PathBuf>,
12 pub line: usize,
13}
14impl FilePosition {
15 pub fn file(&self) -> &PathBuf {
16 &self.file
17 }
18
19 #[cfg(test)]
20 pub(crate) fn into_schema_source_location(self) -> SourceLocation {
21 SourceLocation::SchemaFile(self)
22 }
23}
24impl std::convert::From<SourceLocation> for Option<FilePosition> {
25 fn from(src_loc: SourceLocation) -> Self {
26 match src_loc {
27 SourceLocation::GraphQLBuiltIn => None,
28 SourceLocation::ExecutableDocument => None,
29 SourceLocation::ExecutableDocumentFile(file_pos) => Some(file_pos),
30 SourceLocation::Schema => None,
31 SourceLocation::SchemaFile(file_pos) => Some(file_pos),
32 }
33 }
34}
35
36#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
37pub enum SourceLocation {
38 GraphQLBuiltIn,
39 ExecutableDocument,
40 ExecutableDocumentFile(FilePosition),
41 Schema,
42 SchemaFile(FilePosition),
43}
44impl SourceLocation {
45 pub(crate) fn from_execdoc_ast_position(
46 file_path: Option<&Path>,
47 ast_pos: &ast::AstPos,
48 ) -> Self {
49 if let Some(file_path) = file_path {
50 Self::ExecutableDocumentFile(FilePosition {
51 col: ast_pos.column,
52 file: Box::new(file_path.to_path_buf()),
53 line: ast_pos.line
54 })
55 } else {
56 Self::ExecutableDocument
57 }
58 }
59
60 pub(crate) fn from_schema_ast_position(
61 file_path: Option<&Path>,
62 ast_pos: &ast::AstPos,
63 ) -> Self {
64 if let Some(file_path) = file_path {
65 Self::SchemaFile(FilePosition {
66 col: ast_pos.column,
67 file: Box::new(file_path.to_path_buf()),
68 line: ast_pos.line
69 })
70 } else {
71 Self::Schema
72 }
73 }
74
75 pub(crate) fn with_ast_position(&self, ast_position: &ast::AstPos) -> Self {
76 match self {
77 Self::GraphQLBuiltIn =>
78 Self::GraphQLBuiltIn,
79
80 Self::ExecutableDocument =>
81 Self::ExecutableDocument,
82
83 Self::ExecutableDocumentFile(file_pos) =>
84 Self::ExecutableDocumentFile(FilePosition {
85 col: ast_position.column,
86 file: file_pos.file.to_owned(),
87 line: ast_position.line,
88 }),
89
90 Self::Schema =>
91 Self::Schema,
92
93 Self::SchemaFile(file_pos) =>
94 Self::SchemaFile(FilePosition {
95 col: ast_position.column,
96 file: file_pos.file.to_owned(),
97 line: ast_position.line,
98 })
99 }
100 }
101}