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
62
63
64
65
66
67
68
69
70
71
72
73
74
use std::sync::Arc;

use faststr::FastStr;
use fxhash::FxHashMap;

use crate::{
    resolver::rir::{Field, Graph, Node},
    symbol::{DefId, Ident, IdentName, TagId},
    tags::Tags,
};

pub struct Context {
    graphs: FxHashMap<DefId, Arc<Graph>>,
    nodes: FxHashMap<DefId, Arc<Node>>,
    fields: FxHashMap<DefId, Arc<Field>>,
    tags: FxHashMap<TagId, Arc<Tags>>,
}

impl Default for Context {
    fn default() -> Self {
        Self::new()
    }
}

impl Context {
    pub fn new() -> Self {
        Self {
            graphs: Default::default(),
            nodes: Default::default(),
            fields: Default::default(),
            tags: Default::default(),
        }
    }

    pub fn set_graphs(&mut self, graphs: FxHashMap<DefId, Arc<Graph>>) {
        self.graphs = graphs;
    }

    pub fn set_nodes(&mut self, nodes: FxHashMap<DefId, Arc<Node>>) {
        self.nodes = nodes;
    }

    pub fn set_fields(&mut self, fields: FxHashMap<DefId, Arc<Field>>) {
        self.fields = fields;
    }

    pub fn set_tags(&mut self, tags: FxHashMap<TagId, Arc<Tags>>) {
        self.tags = tags;
    }

    pub fn graph(&self, graph_id: DefId) -> Option<Arc<Graph>> {
        self.graphs.get(&graph_id).cloned()
    }

    pub fn node(&self, node_id: DefId) -> Option<Arc<Node>> {
        self.nodes.get(&node_id).cloned()
    }

    pub fn field(&self, field_id: DefId) -> Option<Arc<Field>> {
        self.fields.get(&field_id).cloned()
    }

    pub fn tag(&self, tag_id: TagId) -> Option<Arc<Tags>> {
        self.tags.get(&tag_id).cloned()
    }

    pub fn snake_name(&self, ident: &Ident) -> FastStr {
        (&***ident).snake_ident()
    }

    pub fn upper_camel_name(&self, ident: &Ident) -> FastStr {
        (&***ident).upper_camel_ident()
    }
}