libgraphql_parser/ast/
type_extension.rs1use crate::ast::AstNode;
2use crate::ast::DirectiveAnnotation;
3use crate::ast::EnumTypeExtension;
4use crate::ast::InputObjectTypeExtension;
5use crate::ast::InterfaceTypeExtension;
6use crate::ast::Name;
7use crate::ast::ObjectTypeExtension;
8use crate::ast::ScalarTypeExtension;
9use crate::ast::UnionTypeExtension;
10use crate::ByteSpan;
11use crate::SourceMap;
12use crate::SourceSpan;
13use inherent::inherent;
14
15#[derive(Clone, Debug, PartialEq)]
21pub enum TypeExtension<'src> {
22 Enum(EnumTypeExtension<'src>),
23 InputObject(InputObjectTypeExtension<'src>),
24 Interface(InterfaceTypeExtension<'src>),
25 Object(ObjectTypeExtension<'src>),
26 Scalar(ScalarTypeExtension<'src>),
27 Union(UnionTypeExtension<'src>),
28}
29
30impl<'src> TypeExtension<'src> {
31 pub fn directive_annotations(
33 &self,
34 ) -> &[DirectiveAnnotation<'src>] {
35 match self {
36 Self::Enum(ext) => &ext.directives,
37 Self::InputObject(ext) => &ext.directives,
38 Self::Interface(ext) => &ext.directives,
39 Self::Object(ext) => &ext.directives,
40 Self::Scalar(ext) => &ext.directives,
41 Self::Union(ext) => &ext.directives,
42 }
43 }
44
45 pub fn name(&self) -> &Name<'src> {
47 match self {
48 Self::Enum(ext) => &ext.name,
49 Self::InputObject(ext) => &ext.name,
50 Self::Interface(ext) => &ext.name,
51 Self::Object(ext) => &ext.name,
52 Self::Scalar(ext) => &ext.name,
53 Self::Union(ext) => &ext.name,
54 }
55 }
56
57 pub fn name_value(&self) -> &str {
62 self.name().value.as_ref()
63 }
64}
65
66#[inherent]
67impl AstNode for TypeExtension<'_> {
68 pub fn append_source(
70 &self,
71 sink: &mut String,
72 source: Option<&str>,
73 ) {
74 match self {
75 TypeExtension::Enum(d) => {
76 d.append_source(sink, source)
77 },
78 TypeExtension::InputObject(d) => {
79 d.append_source(sink, source)
80 },
81 TypeExtension::Interface(d) => {
82 d.append_source(sink, source)
83 },
84 TypeExtension::Object(d) => {
85 d.append_source(sink, source)
86 },
87 TypeExtension::Scalar(d) => {
88 d.append_source(sink, source)
89 },
90 TypeExtension::Union(d) => {
91 d.append_source(sink, source)
92 },
93 }
94 }
95
96 pub fn byte_span(&self) -> ByteSpan {
103 match self {
104 Self::Enum(ext) => ext.span,
105 Self::InputObject(ext) => ext.span,
106 Self::Interface(ext) => ext.span,
107 Self::Object(ext) => ext.span,
108 Self::Scalar(ext) => ext.span,
109 Self::Union(ext) => ext.span,
110 }
111 }
112
113 pub fn source_span(
120 &self,
121 source_map: &SourceMap,
122 ) -> Option<SourceSpan> {
123 self.byte_span().resolve(source_map)
124 }
125}