juniper_codegen/common/
mod.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
//! Common functions, definitions and extensions for code generation, used by this crate.

pub(crate) mod default;
pub(crate) mod deprecation;
mod description;
pub(crate) mod diagnostic;
pub(crate) mod field;
pub(crate) mod gen;
pub(crate) mod parse;
pub(crate) mod rename;
pub(crate) mod scalar;
mod span_container;

use std::slice;

pub(crate) use self::{description::Description, span_container::SpanContainer};

/// Checks whether the specified [`syn::Path`] equals to one of specified one-segment
/// [`AttrNames::values`].
pub(crate) fn path_eq_single(path: &syn::Path, names: impl AttrNames) -> bool {
    path.segments.len() == 1
        && names
            .values()
            .iter()
            .any(|name| path.segments[0].ident == name)
}

/// Filters the provided [`syn::Attribute`] to contain only ones with the
/// specified `name`.
pub(crate) fn filter_attrs<'a>(
    names: impl AttrNames + 'a,
    attrs: &'a [syn::Attribute],
) -> impl Iterator<Item = &'a syn::Attribute> + 'a {
    attrs
        .iter()
        .filter(move |attr| path_eq_single(attr.path(), names))
}

/// Input-type polymorphism helper for checking names of multiple attribute names.
pub(crate) trait AttrNames: Copy {
    /// Returns values to be checked.
    fn values(&self) -> &[&str];
}

impl AttrNames for &str {
    fn values(&self) -> &[&str] {
        slice::from_ref(self)
    }
}

impl AttrNames for &[&str] {
    fn values(&self) -> &[&str] {
        self
    }
}

impl<const N: usize> AttrNames for [&str; N] {
    fn values(&self) -> &[&str] {
        self
    }
}