-
-
Save RandyMcMillan/e3669d0347cd5313fca10b226d435a34 to your computer and use it in GitHub Desktop.
get_library_reports.rs
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(()) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f12490bea06d9d7516096a9f33b28f43