static_graph/
context.rs

1use std::sync::Arc;
2
3use faststr::FastStr;
4use fxhash::FxHashMap;
5
6use crate::{
7    resolver::rir::{Field, Graph, Node},
8    symbol::{DefId, Ident, IdentName, TagId},
9    tags::Tags,
10};
11
12pub struct Context {
13    graphs: FxHashMap<DefId, Arc<Graph>>,
14    nodes: FxHashMap<DefId, Arc<Node>>,
15    fields: FxHashMap<DefId, Arc<Field>>,
16    tags: FxHashMap<TagId, Arc<Tags>>,
17}
18
19impl Default for Context {
20    fn default() -> Self {
21        Self::new()
22    }
23}
24
25impl Context {
26    pub fn new() -> Self {
27        Self {
28            graphs: Default::default(),
29            nodes: Default::default(),
30            fields: Default::default(),
31            tags: Default::default(),
32        }
33    }
34
35    pub fn set_graphs(&mut self, graphs: FxHashMap<DefId, Arc<Graph>>) {
36        self.graphs = graphs;
37    }
38
39    pub fn set_nodes(&mut self, nodes: FxHashMap<DefId, Arc<Node>>) {
40        self.nodes = nodes;
41    }
42
43    pub fn set_fields(&mut self, fields: FxHashMap<DefId, Arc<Field>>) {
44        self.fields = fields;
45    }
46
47    pub fn set_tags(&mut self, tags: FxHashMap<TagId, Arc<Tags>>) {
48        self.tags = tags;
49    }
50
51    pub fn graph(&self, graph_id: DefId) -> Option<Arc<Graph>> {
52        self.graphs.get(&graph_id).cloned()
53    }
54
55    pub fn node(&self, node_id: DefId) -> Option<Arc<Node>> {
56        self.nodes.get(&node_id).cloned()
57    }
58
59    pub fn field(&self, field_id: DefId) -> Option<Arc<Field>> {
60        self.fields.get(&field_id).cloned()
61    }
62
63    pub fn tag(&self, tag_id: TagId) -> Option<Arc<Tags>> {
64        self.tags.get(&tag_id).cloned()
65    }
66
67    pub fn snake_name(&self, ident: &Ident) -> FastStr {
68        (&***ident).snake_ident()
69    }
70
71    pub fn upper_camel_name(&self, ident: &Ident) -> FastStr {
72        (&***ident).upper_camel_ident()
73    }
74}