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
use crate::ast::definition::{
    ArgumentsDefinition, BaseInputTypeReference, BaseOutputTypeReference,
    CustomScalarTypeDefinition, DirectiveDefinition, EnumTypeDefinition, EnumValueDefinition,
    EnumValueDefinitions, FieldDefinition, FieldsDefinition, InputFieldsDefinition,
    InputObjectTypeDefinition, InputTypeReference, InputValueDefinition, InterfaceImplementation,
    InterfaceImplementations, InterfaceTypeDefinition, ObjectTypeDefinition, OutputTypeReference,
    TypeDefinitionReference, UnionMemberType, UnionMemberTypes, UnionTypeDefinition,
};
use crate::ast::ConstDirectives;
use crate::lexical_token::StringValue;
use bluejay_core::definition::SchemaDefinition as CoreSchemaDefinition;
use std::collections::{btree_map::Values, BTreeMap};

#[derive(Debug)]
pub struct SchemaDefinition<'a> {
    type_definitions: BTreeMap<&'a str, &'a TypeDefinitionReference<'a>>,
    directive_definitions: BTreeMap<&'a str, &'a DirectiveDefinition<'a>>,
    description: Option<&'a StringValue>,
    query: &'a ObjectTypeDefinition<'a>,
    mutation: Option<&'a ObjectTypeDefinition<'a>>,
    subscription: Option<&'a ObjectTypeDefinition<'a>>,
    schema_directives: Option<&'a ConstDirectives<'a>>,
}

impl<'a> SchemaDefinition<'a> {
    pub(crate) fn new(
        type_definitions: BTreeMap<&'a str, &'a TypeDefinitionReference<'a>>,
        directive_definitions: BTreeMap<&'a str, &'a DirectiveDefinition<'a>>,
        description: Option<&'a StringValue>,
        query: &'a ObjectTypeDefinition<'a>,
        mutation: Option<&'a ObjectTypeDefinition<'a>>,
        subscription: Option<&'a ObjectTypeDefinition<'a>>,
        schema_directives: Option<&'a ConstDirectives<'a>>,
    ) -> Self {
        Self {
            type_definitions,
            directive_definitions,
            description,
            query,
            mutation,
            subscription,
            schema_directives,
        }
    }
}

impl<'a> CoreSchemaDefinition for SchemaDefinition<'a> {
    type Directives = ConstDirectives<'a>;
    type InputValueDefinition = InputValueDefinition<'a>;
    type InputFieldsDefinition = InputFieldsDefinition<'a>;
    type ArgumentsDefinition = ArgumentsDefinition<'a>;
    type EnumValueDefinition = EnumValueDefinition<'a>;
    type EnumValueDefinitions = EnumValueDefinitions<'a>;
    type FieldDefinition = FieldDefinition<'a>;
    type FieldsDefinition = FieldsDefinition<'a>;
    type InterfaceImplementation = InterfaceImplementation<'a>;
    type InterfaceImplementations = InterfaceImplementations<'a>;
    type UnionMemberType = UnionMemberType<'a>;
    type UnionMemberTypes = UnionMemberTypes<'a>;
    type BaseInputTypeReference = BaseInputTypeReference<'a>;
    type InputTypeReference = InputTypeReference<'a>;
    type BaseOutputTypeReference = BaseOutputTypeReference<'a>;
    type OutputTypeReference = OutputTypeReference<'a>;
    type CustomScalarTypeDefinition = CustomScalarTypeDefinition<'a>;
    type ObjectTypeDefinition = ObjectTypeDefinition<'a>;
    type InterfaceTypeDefinition = InterfaceTypeDefinition<'a>;
    type UnionTypeDefinition = UnionTypeDefinition<'a>;
    type InputObjectTypeDefinition = InputObjectTypeDefinition<'a>;
    type EnumTypeDefinition = EnumTypeDefinition<'a>;
    type TypeDefinitionReference = TypeDefinitionReference<'a>;
    type DirectiveDefinition = DirectiveDefinition<'a>;
    type TypeDefinitionReferences<'b> =
        std::iter::Copied<Values<'b, &'b str, &'b TypeDefinitionReference<'a>>> where 'a: 'b;
    type DirectiveDefinitions<'b> =
        std::iter::Copied<Values<'b, &'b str, &'b DirectiveDefinition<'a>>> where 'a: 'b;

    fn description(&self) -> Option<&str> {
        self.description.as_ref().map(AsRef::as_ref)
    }

    fn query(&self) -> &Self::ObjectTypeDefinition {
        self.query
    }

    fn mutation(&self) -> Option<&Self::ObjectTypeDefinition> {
        self.mutation
    }

    fn subscription(&self) -> Option<&Self::ObjectTypeDefinition> {
        self.subscription
    }

    fn schema_directives(&self) -> Option<&Self::Directives> {
        self.schema_directives
    }

    fn get_type_definition(&self, name: &str) -> Option<&Self::TypeDefinitionReference> {
        self.type_definitions.get(name).copied()
    }

    fn type_definitions(&self) -> Self::TypeDefinitionReferences<'_> {
        self.type_definitions.values().copied()
    }

    fn get_directive_definition(&self, name: &str) -> Option<&Self::DirectiveDefinition> {
        self.directive_definitions.get(name).copied()
    }

    fn directive_definitions(&self) -> Self::DirectiveDefinitions<'_> {
        self.directive_definitions.values().copied()
    }
}