Generate the facade crate's lib.rs file.
(entries: &[CrateEntry], facade_dir: &Path)
| 135 | |
| 136 | /// Generate the facade crate's lib.rs file. |
| 137 | fn write_facade_lib(entries: &[CrateEntry], facade_dir: &Path) -> Result<()> { |
| 138 | let src_dir = facade_dir.join("src"); |
| 139 | std::fs::create_dir_all(&src_dir)?; |
| 140 | |
| 141 | let mut contents = String::from(FACADE_HEADER); |
| 142 | |
| 143 | // Top-level re-exports |
| 144 | for entry in entries { |
| 145 | if entry.submodule.is_none() { |
| 146 | let rust_ident = to_rust_ident(&entry.crate_name); |
| 147 | writeln!(contents, "pub use {rust_ident} as {};", entry.contract_name)?; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Group submodule contracts, preserving insertion order |
| 152 | let mut submodule_names: Vec<String> = Vec::new(); |
| 153 | let mut submodule_map: HashMap<String, Vec<&CrateEntry>> = HashMap::new(); |
| 154 | for entry in entries { |
| 155 | if let Some(submod) = &entry.submodule { |
| 156 | if !submodule_map.contains_key(submod) { |
| 157 | submodule_names.push(submod.clone()); |
| 158 | } |
| 159 | submodule_map.entry(submod.clone()).or_default().push(entry); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Write submodule blocks in insertion order |
| 164 | for submod_name in &submodule_names { |
| 165 | let contracts = &submodule_map[submod_name]; |
| 166 | writeln!(contents, "pub mod {submod_name} {{")?; |
| 167 | for entry in contracts { |
| 168 | let rust_ident = to_rust_ident(&entry.crate_name); |
| 169 | writeln!( |
| 170 | contents, |
| 171 | " pub use {rust_ident} as {};", |
| 172 | entry.contract_name |
| 173 | )?; |
| 174 | } |
| 175 | writeln!(contents, "}}")?; |
| 176 | } |
| 177 | |
| 178 | let file: syn::File = syn::parse_file(&contents)?; |
| 179 | let formatted = prettyplease::unparse(&file); |
| 180 | std::fs::write(src_dir.join("lib.rs"), formatted)?; |
| 181 | |
| 182 | Ok(()) |
| 183 | } |
| 184 | |
| 185 | /// Generate the facade crate's Cargo.toml. |
| 186 | fn write_facade_cargo_toml(entries: &[CrateEntry], facade_dir: &Path) -> Result<()> { |
no test coverage detected