|
1 | 1 | use serde::{Deserialize, Serialize};
|
2 | 2 | use std::collections::BTreeMap;
|
| 3 | +use std::convert::Infallible; |
3 | 4 | use std::fmt::Write;
|
4 | 5 | use std::path::PathBuf;
|
5 | 6 |
|
@@ -41,6 +42,42 @@ pub struct GenericCompileResult<T> {
|
41 | 42 | }
|
42 | 43 |
|
43 | 44 | impl<T> GenericCompileResult<T> {
|
| 45 | + pub fn try_map<R, E>( |
| 46 | + &self, |
| 47 | + mut map_entry_point: impl FnMut(&String) -> Result<String, E>, |
| 48 | + mut map_module: impl FnMut(&T) -> Result<R, E>, |
| 49 | + ) -> Result<GenericCompileResult<R>, E> { |
| 50 | + Ok(match &self.module { |
| 51 | + GenericModuleResult::SingleModule(t) => GenericCompileResult { |
| 52 | + entry_points: self |
| 53 | + .entry_points |
| 54 | + .iter() |
| 55 | + .map(map_entry_point) |
| 56 | + .collect::<Result<_, _>>()?, |
| 57 | + module: GenericModuleResult::SingleModule(map_module(t)?), |
| 58 | + }, |
| 59 | + GenericModuleResult::MultiModule(map) => { |
| 60 | + let new_map: BTreeMap<String, R> = map |
| 61 | + .iter() |
| 62 | + .map(|(entry_point, t)| Ok((map_entry_point(entry_point)?, map_module(t)?))) |
| 63 | + .collect::<Result<_, _>>()?; |
| 64 | + GenericCompileResult { |
| 65 | + entry_points: new_map.keys().cloned().collect(), |
| 66 | + module: GenericModuleResult::MultiModule(new_map), |
| 67 | + } |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + pub fn map<R>( |
| 73 | + &self, |
| 74 | + mut map_entry_point: impl FnMut(&String) -> String, |
| 75 | + mut map_module: impl FnMut(&T) -> R, |
| 76 | + ) -> GenericCompileResult<R> { |
| 77 | + self.try_map::<_, Infallible>(|e| Ok(map_entry_point(e)), |e| Ok(map_module(e))) |
| 78 | + .unwrap() |
| 79 | + } |
| 80 | + |
44 | 81 | pub fn codegen_entry_point_strings(&self) -> String {
|
45 | 82 | let trie = Trie::create_from(self.entry_points.iter().map(|x| x as &str));
|
46 | 83 | let mut builder = String::new();
|
|
0 commit comments