apollo_compiler/schema/
component.rs

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use crate::parser::SourceSpan;
use crate::Name;
use crate::Node;
use std::fmt;
use std::hash;
use std::ops::Deref;
use std::ops::DerefMut;
use triomphe::Arc;

/// A component of a type or `schema`, for example a field of an object type.
///
/// Wraps a [`Node<T>`] and additionally tracks its `origin`:
/// either a “main” definition like `schema` or `type ExampleObj`,
/// or an extension like `extend schema` or `extend type ExampleObj`.
///
/// Implements [`Deref`] and [`DerefMut`]
/// so that methods and fields of `Node<T>` and `T` can be accessed directly.
#[derive(Debug, Clone)]
pub struct Component<T: ?Sized> {
    pub origin: ComponentOrigin,
    pub node: Node<T>,
}

/// The origin of a [`Component`]: either a “main” definition like `schema` or `type ExampleObj`,
/// or an extension like `extend schema` or `extend type ExampleObj`.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum ComponentOrigin {
    Definition,
    Extension(ExtensionId),
}

/// Represents the identity of a schema extension or type extension.
///
/// Compares equal to its clones but not to other `ExtensionId`s created separately,
/// even if they contain the same source location.
#[derive(Debug, Clone, Eq)]
pub struct ExtensionId {
    arc: Arc<Option<SourceSpan>>,
}

impl ExtensionId {
    pub fn new<T>(extension: &Node<T>) -> Self {
        Self {
            arc: Arc::new(extension.location()),
        }
    }

    /// If this extension was parsed from a source file, returns the file ID and source span
    /// (start and end byte offsets) within that file.
    pub fn location(&self) -> Option<SourceSpan> {
        *self.arc
    }

    pub fn same_location<T>(&self, node: T) -> Node<T> {
        Node::new_opt_location(node, self.location())
    }
}

impl PartialEq for ExtensionId {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.arc, &other.arc)
    }
}

impl hash::Hash for ExtensionId {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        Arc::as_ptr(&self.arc).hash(state);
    }
}

impl ComponentOrigin {
    pub fn extension_id(&self) -> Option<&ExtensionId> {
        match self {
            ComponentOrigin::Definition => None,
            ComponentOrigin::Extension(id) => Some(id),
        }
    }
}

impl<T> Component<T> {
    /// Mark `node` as coming from a synthetic (no source location) definition (not an extension)
    pub fn new(node: T) -> Self {
        Self {
            origin: ComponentOrigin::Definition,
            node: Node::new(node),
        }
    }
}

impl<T: ?Sized + hash::Hash> hash::Hash for Component<T> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.node.hash(state); // ignore `origin`
    }
}

impl<T: ?Sized + Eq> Eq for Component<T> {}

impl<T: ?Sized + PartialEq> PartialEq for Component<T> {
    fn eq(&self, other: &Self) -> bool {
        self.node == other.node // ignore `origin`
    }
}

impl<T: ?Sized> Deref for Component<T> {
    type Target = Node<T>;

    fn deref(&self) -> &Self::Target {
        &self.node
    }
}

impl<T: ?Sized> DerefMut for Component<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.node
    }
}

impl<T: ?Sized> AsRef<T> for Component<T> {
    fn as_ref(&self) -> &T {
        &self.node
    }
}

impl<T> From<T> for Component<T> {
    fn from(node: T) -> Self {
        Component::new(node)
    }
}

impl<T> From<Node<T>> for Component<T> {
    fn from(node: Node<T>) -> Self {
        Self {
            origin: ComponentOrigin::Definition,
            node,
        }
    }
}

/// A GraphQL [_Name_](https://spec.graphql.org/draft/#Name)
/// that is component of a type or `schema`, for example the name of a union member type.
///
/// Wraps a [`Name`] and adds its origin:
/// either a “main” definition like `schema` or `enum ExampleEnum`,
/// or an extension like `extend schema` or `extend enum ExampleEnum`
///
/// Implements [`Deref`]
/// so that methods and fields of `Name` and [`str`] can be accessed directly.
#[derive(Debug, Clone)]
pub struct ComponentName {
    pub origin: ComponentOrigin,
    pub name: Name,
}

impl From<&Name> for ComponentName {
    fn from(value: &Name) -> Self {
        value.to_component(ComponentOrigin::Definition)
    }
}

impl From<Name> for ComponentName {
    fn from(value: Name) -> Self {
        value.to_component(ComponentOrigin::Definition)
    }
}

impl hash::Hash for ComponentName {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.name.hash(state); // ignore `origin`
    }
}

impl Eq for ComponentName {}

impl PartialEq for ComponentName {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name // ignore `origin`
    }
}

impl PartialEq<str> for ComponentName {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}
impl PartialEq<Name> for ComponentName {
    fn eq(&self, other: &Name) -> bool {
        self.name == *other
    }
}

impl Deref for ComponentName {
    type Target = Name;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.name
    }
}

impl std::borrow::Borrow<Name> for ComponentName {
    fn borrow(&self) -> &Name {
        self
    }
}

impl std::borrow::Borrow<str> for ComponentName {
    fn borrow(&self) -> &str {
        self
    }
}

impl AsRef<str> for ComponentName {
    fn as_ref(&self) -> &str {
        self
    }
}

impl fmt::Display for ComponentName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.name.fmt(f)
    }
}