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
// use std::collections::HashMap;
// use std::ops::Deref;
// use crate::error::IndexError;
// use crate::Schema;
// use petgraph::algo::has_path_connecting;
// use petgraph::graph::NodeIndex;
// use petgraph::Graph as PetGraph;
// /// Builds a graph of schemas whose edges correspond to references.
// /// The goal is to determine compilation sequences and to recognize when
// /// schemas are recursively referenced.
// pub(crate) struct Graph {
// index: HashMap<String, NodeIndex>,
// graph: PetGraph<String, ()>,
// }
// impl Graph {
// pub fn new(schemas: &[Schema]) -> Result<Graph, IndexError> {
// let mut g = Graph {
// index: HashMap::new(),
// graph: PetGraph::new(),
// };
// for schema in schemas.iter().cloned() {
// g.add(schema)?;
// }
// Ok(g)
// }
// fn index(&mut self, id: impl ToString) -> NodeIndex {
// let Graph {
// ref mut index,
// ref mut graph,
// } = *self;
// let id = id.to_string();
// *index
// .entry(id.clone())
// .or_insert_with(|| graph.add_node(id))
// }
// pub fn add(&mut self, schema: Schema) -> Result<(), IndexError> {
// if schema.id().is_none() {
// return Err(IndexError::NotIdentified);
// }
// let schema_index = self.index(schema.id.as_ref().unwrap().clone());
// for r in schema.references().iter() {
// let ref_index = self.index(r.as_str());
// self.graph.add_edge(schema_index, ref_index, ());
// }
// Ok(())
// }
// /// Returns `true` if `reference` is referenced by `source`.
// pub fn is_referenced(
// &self,
// reference: impl Deref<Target = str>,
// source: impl Deref<Target = str>,
// ) -> bool {
// let outer = match self.index.get(reference.deref()) {
// Some(outer) => *outer,
// None => return false,
// };
// let inner = match self.index.get(source.deref()) {
// Some(inner) => *inner,
// None => return false,
// };
// has_path_connecting(&self.graph, outer, inner, None)
// }
// }