|
| 1 | +use rustdoc_types::Crate; |
| 2 | +use std::env; |
| 3 | +use std::error::Error; |
| 4 | +use std::fs::File; |
| 5 | +use std::io::prelude::*; |
| 6 | +use std::path::Path; |
| 7 | +use std::process::Command; |
| 8 | + |
| 9 | +fn main() -> Result<(), Box<dyn Error>> { |
| 10 | + // Get the package name from command-line arguments |
| 11 | + let args: Vec<String> = env::args().collect(); |
| 12 | + if args.len() != 3 || args[1] != "--package" { |
| 13 | + eprintln!("Usage: {} --package <package_name>", args[0]); |
| 14 | + std::process::exit(1); |
| 15 | + } |
| 16 | + let package_name = &args[2]; |
| 17 | + let path_str = format!("./target/doc/{}.json", package_name); |
| 18 | + let path = Path::new(&path_str); |
| 19 | + |
| 20 | + // Call cargo +nightly rustdoc to generate the JSON file |
| 21 | + let output = Command::new("cargo") |
| 22 | + .arg("+nightly") |
| 23 | + .arg("rustdoc") |
| 24 | + .arg("-Z") |
| 25 | + .arg("unstable-options") |
| 26 | + .arg("--output-format") |
| 27 | + .arg("json") |
| 28 | + .arg("--package") |
| 29 | + .arg(package_name) |
| 30 | + .arg("--all-features") |
| 31 | + .output()?; |
| 32 | + |
| 33 | + if !output.status.success() { |
| 34 | + eprintln!( |
| 35 | + "Failed to generate JSON file: {}", |
| 36 | + String::from_utf8_lossy(&output.stderr) |
| 37 | + ); |
| 38 | + std::process::exit(1); |
| 39 | + } |
| 40 | + |
| 41 | + let mut file = File::open(path)?; |
| 42 | + let mut contents = String::new(); |
| 43 | + file.read_to_string(&mut contents)?; |
| 44 | + |
| 45 | + let mut root: Crate = serde_json::from_str(&contents)?; |
| 46 | + |
| 47 | + // Remove items |
| 48 | + // 1. with item.inner.impl.is_synthetic set to true - [auto traits] |
| 49 | + // 2. with item.inner.impl.blanket_impl is not null - [blanket impls] |
| 50 | + root.index.retain(|_id, item| { |
| 51 | + if let rustdoc_types::ItemEnum::Impl(impl_item) = &item.inner { |
| 52 | + // Filter out auto traits |
| 53 | + if impl_item.is_synthetic { |
| 54 | + return false; |
| 55 | + } |
| 56 | + // Filter out blanket implementations |
| 57 | + if impl_item.blanket_impl.is_some() { |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | + true |
| 62 | + }); |
| 63 | + |
| 64 | + // Clear unnecessary fields in the Crate structure |
| 65 | + // 1. paths |
| 66 | + // 2. external_crates |
| 67 | + // 3. span in all items |
| 68 | + root.paths.clear(); |
| 69 | + root.external_crates.clear(); |
| 70 | + for (_id, item) in root.index.iter_mut() { |
| 71 | + // Reset span to default empty value |
| 72 | + item.span = Default::default(); |
| 73 | + } |
| 74 | + |
| 75 | + // Navigate to Cargo.toml and get the path for the package |
| 76 | + let cargo_toml_path = Path::new("Cargo.toml"); |
| 77 | + let cargo_toml_content = std::fs::read_to_string(cargo_toml_path)?; |
| 78 | + let cargo_toml: toml::Value = toml::from_str(&cargo_toml_content)?; |
| 79 | + |
| 80 | + let package_path = cargo_toml |
| 81 | + .get("workspace") |
| 82 | + .and_then(|ws| ws.get("members")) |
| 83 | + .and_then(|members| members.as_array()) |
| 84 | + .and_then(|members| { |
| 85 | + members.iter().find_map(|member| { |
| 86 | + if member.as_str()?.ends_with(package_name) { |
| 87 | + Some(member.as_str()?.to_string()) |
| 88 | + } else { |
| 89 | + None |
| 90 | + } |
| 91 | + }) |
| 92 | + }) |
| 93 | + .ok_or("Package path not found in Cargo.toml")?; |
| 94 | + |
| 95 | + // Create the review/ folder under the obtained path if it doesn't exist |
| 96 | + let review_folder_path = Path::new(&package_path).join("review"); |
| 97 | + if !review_folder_path.exists() { |
| 98 | + std::fs::create_dir_all(&review_folder_path)?; |
| 99 | + } |
| 100 | + |
| 101 | + // Create the package_name.rust.json in the review/ folder |
| 102 | + let output_path_str = review_folder_path.join(format!("{}.rust.json", package_name)); |
| 103 | + let output_path = Path::new(&output_path_str); |
| 104 | + let mut output_file = File::create(output_path)?; |
| 105 | + serde_json::to_writer(&mut output_file, &root)?; |
| 106 | + |
| 107 | + println!("File has been generated at: {}", output_path_str.display()); |
| 108 | + |
| 109 | + Ok(()) |
| 110 | +} |
0 commit comments