bluejay_visibility/
directives.rs

1use crate::{Cache, Directive, Warden};
2use bluejay_core::definition::{
3    prelude::*, Directives as CoreDefinitionDirectives, SchemaDefinition,
4};
5use bluejay_core::{AsIter, Directives as CoreDirectives};
6use once_cell::unsync::OnceCell;
7
8pub struct Directives<'a, S: SchemaDefinition, W: Warden<SchemaDefinition = S>> {
9    inner: &'a <S as SchemaDefinition>::Directives,
10    cache: &'a Cache<'a, S, W>,
11    directives: OnceCell<Vec<Directive<'a, S, W>>>,
12}
13
14impl<'a, S: SchemaDefinition, W: Warden<SchemaDefinition = S>> Directives<'a, S, W> {
15    pub(crate) fn new(
16        inner: &'a <S as SchemaDefinition>::Directives,
17        cache: &'a Cache<'a, S, W>,
18    ) -> Self {
19        Self {
20            inner,
21            cache,
22            directives: OnceCell::new(),
23        }
24    }
25}
26
27impl<'a, S: SchemaDefinition + 'a, W: Warden<SchemaDefinition = S>> AsIter
28    for Directives<'a, S, W>
29{
30    type Item = Directive<'a, S, W>;
31    type Iterator<'b>
32        = std::slice::Iter<'b, Self::Item>
33    where
34        'a: 'b;
35
36    fn iter(&self) -> Self::Iterator<'_> {
37        self.directives
38            .get_or_init(|| {
39                self.inner
40                    .iter()
41                    .filter_map(|directive| {
42                        self.cache
43                            .get_or_create_directive_definition(
44                                directive.definition(self.cache.inner_schema_definition()),
45                            )
46                            .map(|definition| Directive::new(directive, definition))
47                    })
48                    .collect()
49            })
50            .iter()
51    }
52}
53
54impl<'a, S: SchemaDefinition + 'a, W: Warden<SchemaDefinition = S>> CoreDirectives<true>
55    for Directives<'a, S, W>
56{
57    type Directive = Directive<'a, S, W>;
58}
59
60impl<'a, S: SchemaDefinition + 'a, W: Warden<SchemaDefinition = S>> CoreDefinitionDirectives
61    for Directives<'a, S, W>
62{
63    type Directive = Directive<'a, S, W>;
64}