1use std::path::Path;
2
3use serde::Serialize;
4
5#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
6pub struct Map {
7 pub repo: String,
8 pub languages: Vec<String>,
9 pub groups: Vec<Group>,
10 pub units: Vec<Unit>,
11 pub edges: Vec<Edge>,
12 pub structures: Vec<Structure>,
13 pub unparsed: Vec<Unparsed>,
14 pub unresolved: Vec<Unresolved>,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
18pub struct Group {
19 pub id: String,
20 pub kind: String,
21 pub path: String,
22 pub lang: String,
23}
24
25#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
26pub struct Unit {
27 pub id: String,
28 pub group: String,
29 pub file: String,
30 pub lang: String,
31}
32
33#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
34pub struct Edge {
35 pub from: String,
36 pub to: String,
37 pub level: String,
38}
39
40#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
41pub struct Structure {
42 pub id: String,
43 pub name: String,
44 pub kind: String,
45 pub unit: String,
46 pub file: String,
47 pub line: u32,
48 pub serialisable: bool,
49 pub fields: Vec<Field>,
50 pub referenced_by: Vec<String>,
51}
52
53#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
54pub struct Field {
55 pub name: String,
56 #[serde(rename = "type")]
57 pub ty: String,
58}
59
60#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
61pub struct Unparsed {
62 pub file: String,
63 pub reason: String,
64}
65
66#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
67pub struct Unresolved {
68 pub file: String,
69 pub line: u32,
70 pub reference: String,
71}
72
73#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
74pub struct Part {
75 pub groups: Vec<Group>,
76 pub units: Vec<Unit>,
77 pub edges: Vec<Edge>,
78 pub structures: Vec<Structure>,
79 pub unparsed: Vec<Unparsed>,
80 pub unresolved: Vec<Unresolved>,
81}
82
83pub fn build(root: &Path, repo: &str) -> Result<Map, String> {
84 let mut languages = Vec::new();
85 let mut parts = Vec::new();
86
87 if root.join("Cargo.toml").exists() {
88 let part = crate::map_rust::extract(root)?;
89 languages.push("rust".to_string());
90 parts.push(part);
91 }
92
93 let py_part = crate::map_python::extract(root)?;
94 if !py_part.units.is_empty() {
95 languages.push("python".to_string());
96 parts.push(py_part);
97 }
98
99 if languages.is_empty() {
100 return Err(format!(
101 "no supported language in {}: no Cargo.toml at the root and no Python modules",
102 root.display()
103 ));
104 }
105
106 Ok(finish(repo, languages, parts))
107}
108
109pub fn finish(repo: &str, languages: Vec<String>, parts: Vec<Part>) -> Map {
110 let mut groups = Vec::new();
111 let mut units = Vec::new();
112 let mut edges = Vec::new();
113 let mut structures = Vec::new();
114 let mut unparsed = Vec::new();
115 let mut unresolved = Vec::new();
116
117 for part in parts {
118 groups.extend(part.groups);
119 units.extend(part.units);
120 edges.extend(part.edges);
121 structures.extend(part.structures);
122 unparsed.extend(part.unparsed);
123 unresolved.extend(part.unresolved);
124 }
125
126 groups.sort_by(|a, b| {
127 a.id.cmp(&b.id)
128 .then_with(|| a.kind.cmp(&b.kind))
129 .then_with(|| a.path.cmp(&b.path))
130 .then_with(|| a.lang.cmp(&b.lang))
131 });
132 groups.dedup();
133
134 units.sort_by(|a, b| {
135 a.id.cmp(&b.id)
136 .then_with(|| a.group.cmp(&b.group))
137 .then_with(|| a.file.cmp(&b.file))
138 .then_with(|| a.lang.cmp(&b.lang))
139 });
140 units.dedup();
141
142 edges.retain(|e| e.from != e.to);
143 edges.sort_by(|a, b| {
144 a.level
145 .cmp(&b.level)
146 .then_with(|| a.from.cmp(&b.from))
147 .then_with(|| a.to.cmp(&b.to))
148 });
149 edges.dedup();
150
151 for structure in &mut structures {
152 structure.referenced_by.retain(|u| u != &structure.unit);
153 structure.referenced_by.sort();
154 structure.referenced_by.dedup();
155 }
156 structures.sort_by(|a, b| {
157 a.id.cmp(&b.id)
158 .then_with(|| a.file.cmp(&b.file))
159 .then_with(|| a.line.cmp(&b.line))
160 });
161 structures.dedup();
162
163 unparsed.sort_by(|a, b| a.file.cmp(&b.file).then_with(|| a.reason.cmp(&b.reason)));
164 unparsed.dedup();
165
166 unresolved.sort_by(|a, b| {
167 a.file
168 .cmp(&b.file)
169 .then_with(|| a.line.cmp(&b.line))
170 .then_with(|| a.reference.cmp(&b.reference))
171 });
172 unresolved.dedup();
173
174 Map {
175 repo: repo.to_string(),
176 languages,
177 groups,
178 units,
179 edges,
180 structures,
181 unparsed,
182 unresolved,
183 }
184}