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<'a, C: Context> HasSpan for Directive<'a, 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> = std::slice::Iter<'b, Self::Item> where 'a: 'b;
63
64    fn iter(&self) -> Self::Iterator<'_> {
65        self.directives.iter()
66    }
67}
68
69impl<'a, C: Context> bluejay_core::Directives<true> for Directives<'a, C> {
70    type Directive = Directive<'a, C>;
71}
72
73impl<'a, C: Context> bluejay_core::definition::Directives for Directives<'a, C> {
74    type Directive = Directive<'a, C>;
75}
76
77impl<'a, C: Context> From<ast::Directives<'a, true>> for Directives<'a, C> {
78    fn from(value: ast::Directives<'a, true>) -> Self {
79        Self {
80            directives: value.into_iter().map(Directive::from).collect(),
81        }
82    }
83}