1use std::{
2 collections::{BTreeMap, BTreeSet, HashMap, HashSet},
3 fs,
4 path::{Path, PathBuf},
5};
6
7use anyhow::Context;
8use itertools::Itertools;
9use typeshare_model::prelude::*;
10
11use crate::{args::OutputLocation, parser::ParsedData, topsort::topsort};
12
13pub fn write_output<'c>(
14 lang: &impl Language<'c>,
15 crate_parsed_data: HashMap<Option<CrateName>, ParsedData>,
16 dest: &OutputLocation<'_>,
17) -> anyhow::Result<()> {
18 match dest {
19 OutputLocation::File(file) => {
20 let mut parsed_data = crate_parsed_data
22 .into_values()
23 .reduce(|mut data, new_data| {
24 data.merge(new_data);
25 data
26 })
27 .context("called `write_output` with no data")?;
28
29 parsed_data.sort_contents();
30 write_single_file(lang, file, &parsed_data)
31 }
32 OutputLocation::Folder(directory) => {
33 let crate_parsed_data = crate_parsed_data
36 .into_iter()
37 .map(|(crate_name, mut data)| match crate_name {
38 Some(crate_name) => {
39 data.sort_contents();
40 Ok((crate_name, data))
41 }
42 None => anyhow::bail!(
43 "got files with unknown crates; all files \
44 must be in crates in multi-file mode"
45 ),
46 })
47 .try_collect()?;
48
49 write_multiple_files(lang, directory, &crate_parsed_data)
50 }
51 }
52}
53
54pub fn write_multiple_files<'c>(
56 lang: &impl Language<'c>,
57 output_folder: &Path,
58 crate_parsed_data: &HashMap<CrateName, ParsedData>,
59) -> anyhow::Result<()> {
60 let mut output_files = Vec::with_capacity(crate_parsed_data.len());
61
62 for (crate_name, parsed_data) in crate_parsed_data {
64 let file_path = output_folder.join(&lang.output_filename_for_crate(&crate_name));
65
66 let mut output = Vec::new();
67
68 generate_types(
69 lang,
70 &mut output,
71 parsed_data,
72 FilesMode::Multi(&crate_name),
73 )
74 .with_context(|| format!("error generating typeshare types for crate {crate_name}"))?;
75
76 check_write_file(&file_path, output).with_context(|| {
77 format!(
78 "error writing generated typeshare types for crate {crate_name} to '{}'",
79 file_path.display()
80 )
81 })?;
82
83 output_files.push((crate_name, file_path));
84 }
85
86 output_files.sort_by_key(|&(crate_name, _)| crate_name);
87
88 lang.write_additional_files(
89 output_folder,
90 output_files
91 .iter()
92 .map(|(crate_name, file_path)| (*crate_name, file_path.as_path())),
93 )
94 .context("failed to write additional files")?;
95
96 Ok(())
97}
98
99pub fn write_single_file<'c>(
101 lang: &impl Language<'c>,
102 file_name: &Path,
103 parsed_data: &ParsedData,
104) -> Result<(), anyhow::Error> {
105 let mut output = Vec::new();
106
107 generate_types(lang, &mut output, parsed_data, FilesMode::Single)
108 .context("error generating typeshare types")?;
109
110 let outfile = Path::new(file_name).to_path_buf();
111 check_write_file(&outfile, output)
112 .context("error writing generated typeshare types to file")?;
113 Ok(())
114}
115
116fn check_write_file(outfile: &PathBuf, output: Vec<u8>) -> anyhow::Result<()> {
118 match fs::read(outfile) {
119 Ok(buf) if buf == output => {
120 eprintln!("Skipping writing to {outfile:?} no changes");
124 return Ok(());
125 }
126 _ => {}
127 }
128
129 if !output.is_empty() {
130 let out_dir = outfile
131 .parent()
132 .context(format!("Could not get parent for {outfile:?}"))?;
133 if !out_dir.exists() {
135 fs::create_dir_all(out_dir).context("failed to create output directory")?;
136 }
137
138 fs::write(outfile, output).context("failed to write output")?;
139 }
140 Ok(())
141}
142
143#[non_exhaustive]
146#[derive(Debug, Clone, Copy)]
147pub enum BorrowedRustItem<'a> {
148 Struct(&'a RustStruct),
150 Enum(&'a RustEnum),
152 Alias(&'a RustTypeAlias),
154 Const(&'a RustConst),
156}
157
158impl BorrowedRustItem<'_> {
159 pub fn id(&self) -> &TypeName {
160 &match *self {
161 BorrowedRustItem::Struct(item) => &item.id,
162 BorrowedRustItem::Enum(item) => &item.shared().id,
163 BorrowedRustItem::Alias(item) => &item.id,
164 BorrowedRustItem::Const(item) => &item.id,
165 }
166 .original
167 }
168}
169
170fn generate_types<'c>(
173 lang: &impl Language<'c>,
174 out: &mut Vec<u8>,
175 data: &ParsedData,
176 mode: FilesMode<&CrateName>,
177) -> anyhow::Result<()> {
178 lang.begin_file(out, mode)
179 .context("error writing file header")?;
180
181 if let FilesMode::Multi(crate_name) = mode {
182 let all_types = HashMap::new();
183 lang.write_imports(out, crate_name, used_imports(&data, crate_name, &all_types))
184 .context("error writing imports")?;
185 }
186
187 let ParsedData {
188 structs,
189 enums,
190 aliases,
191 consts,
192 ..
193 } = data;
194
195 let mut items = Vec::from_iter(
196 aliases
197 .iter()
198 .map(BorrowedRustItem::Alias)
199 .chain(structs.iter().map(BorrowedRustItem::Struct))
200 .chain(enums.iter().map(BorrowedRustItem::Enum))
201 .chain(consts.iter().map(BorrowedRustItem::Const)),
202 );
203
204 topsort(&mut items);
205
206 for thing in &items {
207 let name = thing.id();
208
209 match thing {
210 BorrowedRustItem::Enum(e) => lang
211 .write_enum(out, e)
212 .with_context(|| format!("error writing enum {name}"))?,
213 BorrowedRustItem::Struct(s) => lang
214 .write_struct(out, s)
215 .with_context(|| format!("error writing struct {name}"))?,
216 BorrowedRustItem::Alias(a) => lang
217 .write_type_alias(out, a)
218 .with_context(|| format!("error writing type alias {name}"))?,
219 BorrowedRustItem::Const(c) => lang
220 .write_const(out, c)
221 .with_context(|| format!("error writing const {name}"))?,
222 }
223 }
224
225 lang.end_file(out, mode)
226 .context("error writing file trailer")
227}
228
229fn used_imports<'a, 'b: 'a>(
232 data: &'b ParsedData,
233 crate_name: &CrateName,
234 all_types: &'a HashMap<CrateName, HashSet<TypeName>>,
235) -> BTreeMap<&'a CrateName, BTreeSet<&'a TypeName>> {
236 let mut used_imports: BTreeMap<&'a CrateName, BTreeSet<&'a TypeName>> = BTreeMap::new();
237
238 let fallback = |referenced_import: &'a ImportedType,
241 used: &mut BTreeMap<&'a CrateName, BTreeSet<&'a TypeName>>| {
242 if let Some((crate_name, ty)) = all_types
244 .iter()
245 .flat_map(|(k, v)| {
246 v.iter()
247 .find(|&t| *t == referenced_import.type_name && k != crate_name)
248 .map(|t| (k, t))
249 })
250 .next()
251 {
252 println!("Warning: Using {crate_name} as module for {ty} which is not in referenced crate {}", referenced_import.base_crate);
253 used.entry(crate_name).or_default().insert(ty);
254 } else {
255 }
257 };
258
259 for referenced_import in data
260 .import_types
261 .iter()
262 .filter(|imp| imp.base_crate != *crate_name)
265 {
266 if let Some(type_names) = all_types.get(&referenced_import.base_crate) {
268 if referenced_import.type_name == "*" {
269 used_imports
271 .entry(&referenced_import.base_crate)
272 .and_modify(|names| names.extend(type_names.iter()));
273 } else if let Some(ty_name) = type_names.get(&referenced_import.type_name) {
274 used_imports
276 .entry(&referenced_import.base_crate)
277 .or_default()
278 .insert(ty_name);
279 } else {
280 fallback(referenced_import, &mut used_imports);
281 }
282 } else {
283 fallback(referenced_import, &mut used_imports);
285 }
286 }
287 used_imports
288}