bluejay_core/executable/
operation_definition.rs

1use crate::executable::{SelectionSet, VariableDefinitions};
2use crate::{Indexable, OperationType, VariableDirectives};
3
4#[derive(Debug)]
5pub enum OperationDefinitionReference<'a, O: OperationDefinition> {
6    Explicit(&'a O::ExplicitOperationDefinition),
7    Implicit(&'a O::ImplicitOperationDefinition),
8}
9
10impl<'a, O: OperationDefinition> OperationDefinitionReference<'a, O> {
11    pub fn operation_type(&self) -> OperationType {
12        match self {
13            Self::Explicit(eod) => eod.operation_type(),
14            Self::Implicit(_) => OperationType::Query,
15        }
16    }
17
18    pub fn name(&self) -> Option<&'a str> {
19        match self {
20            Self::Explicit(eod) => eod.name(),
21            Self::Implicit(_) => None,
22        }
23    }
24
25    pub fn variable_definitions(&self) -> Option<&'a <<O as OperationDefinition>::ExplicitOperationDefinition as ExplicitOperationDefinition>::VariableDefinitions>{
26        match self {
27            Self::Explicit(eod) => eod.variable_definitions(),
28            Self::Implicit(_) => None,
29        }
30    }
31
32    pub fn selection_set(&self) -> &'a <<O as OperationDefinition>::ExplicitOperationDefinition as ExplicitOperationDefinition>::SelectionSet{
33        match self {
34            Self::Explicit(eod) => eod.selection_set(),
35            Self::Implicit(iod) => iod.selection_set(),
36        }
37    }
38
39    pub fn directives(&self) -> Option<&'a <<O as OperationDefinition>::ExplicitOperationDefinition as ExplicitOperationDefinition>::Directives>{
40        match self {
41            Self::Explicit(eod) => eod.directives(),
42            Self::Implicit(_) => None,
43        }
44    }
45}
46
47pub trait OperationDefinition: Sized + Indexable {
48    type ExplicitOperationDefinition: ExplicitOperationDefinition;
49    type ImplicitOperationDefinition: ImplicitOperationDefinition<SelectionSet=<Self::ExplicitOperationDefinition as ExplicitOperationDefinition>::SelectionSet>;
50
51    fn as_ref(&self) -> OperationDefinitionReference<'_, Self>;
52}
53
54pub trait ExplicitOperationDefinition {
55    type VariableDefinitions: VariableDefinitions;
56    type Directives: VariableDirectives;
57    type SelectionSet: SelectionSet;
58
59    fn operation_type(&self) -> OperationType;
60    fn name(&self) -> Option<&str>;
61    fn variable_definitions(&self) -> Option<&Self::VariableDefinitions>;
62    fn directives(&self) -> Option<&Self::Directives>;
63    fn selection_set(&self) -> &Self::SelectionSet;
64}
65
66pub trait ImplicitOperationDefinition {
67    type SelectionSet: SelectionSet;
68
69    fn selection_set(&self) -> &Self::SelectionSet;
70}