-
-
Save rust-play/1313e2bdddfc5f58c1423f1c1d492772 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use anyhow::Context; | |
| use serde::Serialize; | |
| use std::hash::{Hash, Hasher}; | |
| use tokio::fs::File; | |
| use tokio::io::AsyncWriteExt; | |
| #[derive(Serialize)] | |
| struct LibReport { | |
| name: String, | |
| fingerprint: String, | |
| status: String, | |
| } | |
| macro_rules! get_library_reports { | |
| ($($lib:ident),*) => { | |
| { | |
| let mut reports = Vec::new(); | |
| $( | |
| let name = stringify!($lib); | |
| let mut hasher = fxhash::FxHasher::default(); | |
| name.hash(&mut hasher); | |
| let hash_val = hasher.finish(); | |
| reports.push(LibReport { | |
| name: name.to_string(), | |
| fingerprint: format!("{:016x}", hash_val), | |
| status: "Linked".to_string(), | |
| }); | |
| )* | |
| reports | |
| } | |
| }; | |
| } | |
| #[tokio::main] | |
| async fn main() -> anyhow::Result<()> { | |
| // 1. Generate the data | |
| let libraries = get_library_reports!( | |
| anyhow, chrono, serde, serde_json, tokio, | |
| reqwest, regex, once_cell, base64, bytes, | |
| itertools, log, thiserror, uuid, url | |
| ); | |
| // 2. Serialize to Pretty JSON | |
| let json_report = serde_json::to_string_pretty(&libraries) | |
| .context("Failed to serialize library report")?; | |
| // 3. Async File Write | |
| let filename = "library_report.json"; | |
| let mut file = File::create(filename) | |
| .await | |
| .with_context(|| format!("Failed to create file: {}", filename))?; | |
| file.write_all(json_report.as_bytes()) | |
| .await | |
| .with_context(|| format!("Failed to write data to {}", filename))?; | |
| println!("✅ Report generated and saved to {}", filename); | |
| println!("Preview of JSON structure:\n{}", &json_report[..1399]); // Print snippet | |
| Ok(()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment