bluejay_parser/ast/definition/
directive.rs

1use crate::ast::{
2    self,
3    definition::{Context, DirectiveDefinition},
4};
5use crate::{HasSpan, Span};
6use bluejay_core::definition::SchemaDefinition;
7use std::marker::PhantomData;
8
9#[derive(Debug)]
10pub struct Directive<'a, C: Context + 'a> {
11    inner: ast::Directive<'a, true>,
12    context: PhantomData<C>,
13}
14
15impl<'a, C: Context> bluejay_core::Directive<true> for Directive<'a, C> {
16    type Arguments = ast::Arguments<'a, true>;
17
18    fn name(&self) -> &str {
19        self.inner.name().as_str()
20    }
21
22    fn arguments(&self) -> Option<&Self::Arguments> {
23        self.inner.arguments()
24    }
25}
26
27impl<'a, C: Context> bluejay_core::definition::Directive for Directive<'a, C> {
28    type DirectiveDefinition = DirectiveDefinition<'a, C>;
29
30    fn definition<'b, S: SchemaDefinition<DirectiveDefinition = Self::DirectiveDefinition>>(
31        &'b self,
32        schema_definition: &'b S,
33    ) -> &'b Self::DirectiveDefinition {
34        schema_definition
35            .get_directive_definition(self.inner.name().as_str())
36            .unwrap()
37    }
38}
39
40impl<'a, C: Context> From<ast::Directive<'a, true>> for Directive<'a, C> {
41    fn from(value: ast::Directive<'a, true>) -> Self {
42        Self {
43            inner: value,
44            context: PhantomData,
45        }
46    }
47}
48
49impl<C: Context> HasSpan for Directive<'_, C> {
50    fn span(&self) -> &Span {
51        self.inner.span()
52    }
53}
54
55#[derive(Debug)]
56pub struct Directives<'a, C: Context> {
57    directives: Vec<Directive<'a, C>>,
58}
59
60impl<'a, C: Context> bluejay_core::AsIter for Directives<'a, C> {
61    type Item = Directive<'a, C>;
62    type Iterator<'b>
63        = std::slice::Iter<'b, Self::Item>
64    where
65        'a: 'b;
66
67    fn iter(&self) -> Self::Iterator<'_> {
68        self.directives.iter()
69    }
70}
71
72impl<'a, C: Context> bluejay_core::Directives<true> for Directives<'a, C> {
73    type Directive = Directive<'a, C>;
74}
75
76impl<'a, C: Context> bluejay_core::definition::Directives for Directives<'a, C> {
77    type Directive = Directive<'a, C>;
78}
79
80impl<'a, C: Context> From<ast::Directives<'a, true>> for Directives<'a, C> {
81    fn from(value: ast::Directives<'a, true>) -> Self {
82        Self {
83            directives: value.into_iter().map(Directive::from).collect(),
84        }
85    }
86}