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
mod validation_db;

mod directive;
mod enum_;
mod input_object;
mod interface;
mod object;
mod operation;
mod scalar;
mod schema;
mod union_;
mod unused_variable;

pub use validation_db::{ValidationDatabase, ValidationStorage};

use crate::{hir::HirNodeLocation, FileId};
use apollo_parser::ast::AstNode;

#[derive(Debug, Eq)]
struct ValidationSet {
    name: String,
    loc: HirNodeLocation,
}

impl std::hash::Hash for ValidationSet {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.name.hash(state);
    }
}

impl PartialEq for ValidationSet {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

/// Finds top-level AST nodes of a given type in type definition files.
///
/// This ignores pre-computed inputs.
pub(crate) fn ast_type_definitions<'db, AstType>(
    db: &'db dyn ValidationDatabase,
) -> impl Iterator<Item = (FileId, AstType)> + 'db
where
    AstType: 'db + AstNode,
{
    db.type_definition_files()
        .into_iter()
        .flat_map(move |file_id| {
            db.ast(file_id)
                .document()
                .syntax()
                .children()
                .filter_map(AstNode::cast)
                .map(move |def| (file_id, def))
        })
}