-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmod.rs
More file actions
121 lines (106 loc) · 3.18 KB
/
Copy pathmod.rs
File metadata and controls
121 lines (106 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
use std::fmt;
#[cfg(feature = "quine")]
use {polyquine::Quine, proc_macro2::TokenStream};
use self::pipeline::stage::{Match, Stage};
pub use self::{
pipeline::{stage, Pipeline},
schema::SchemaQuery,
};
use crate::{
common::{error::TypeQLError::InvalidCasting, Span},
token,
};
pub mod pipeline;
pub mod schema;
#[derive(Debug, Eq, PartialEq)]
#[cfg_attr(feature = "quine", derive(Quine))]
pub struct Query {
pub span: Option<Span>,
pub structure: QueryStructure,
pub has_explicit_end: bool,
}
impl Query {
pub(crate) fn new(span: Option<Span>, structure: QueryStructure, has_explicit_end: bool) -> Self {
Self { span, structure, has_explicit_end }
}
pub fn has_explicit_end(&self) -> bool {
self.has_explicit_end
}
pub fn into_structure(self) -> QueryStructure {
self.structure
}
}
impl fmt::Display for Query {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.structure, f)?;
if self.has_explicit_end {
if f.alternate() {
write!(f, "\n{}", token::Clause::End)
} else {
write!(f, "{}", token::Clause::End)
}
} else {
Ok(())
}
}
}
#[derive(Debug, Eq, PartialEq)]
#[cfg_attr(feature = "quine", derive(Quine))]
pub enum QueryStructure {
Schema(SchemaQuery),
Pipeline(Pipeline),
}
impl QueryStructure {
fn variant_name(&self) -> &'static str {
match self {
Self::Schema(_) => "Schema",
Self::Pipeline(_) => "Pipeline",
}
}
pub fn into_schema(self) -> SchemaQuery {
match self {
Self::Schema(schema) => schema,
_ => panic!(
"{}",
InvalidCasting {
enum_name: stringify!(QueryStructure),
variant: self.variant_name(),
expected_variant: stringify!(Schema),
typename: stringify!(SchemaQuery),
}
),
}
}
pub fn into_pipeline(self) -> Pipeline {
match self {
QueryStructure::Pipeline(pipeline) => pipeline,
_ => panic!(
"{}",
InvalidCasting {
enum_name: stringify!(QueryStructure),
variant: self.variant_name(),
expected_variant: stringify!(Pipeline),
typename: stringify!(Pipeline),
}
),
}
}
}
impl fmt::Display for QueryStructure {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Schema(schema_query) => fmt::Display::fmt(schema_query, f),
Self::Pipeline(data_query) => fmt::Display::fmt(data_query, f),
}
}
}
impl From<Match> for Query {
fn from(value: Match) -> Self {
Self::new(None, QueryStructure::Pipeline(Pipeline::new(None, Vec::new(), vec![Stage::Match(value)])), false)
}
}