Minify crate fields to reduce the crate-index.js file size

This commit is contained in:
Folyd 2019-11-15 18:41:46 +08:00
parent e95b31827a
commit 0b9a0e5c24

View file

@ -17,13 +17,19 @@ struct CrateApiResponse {
#[derive(Serialize, Deserialize, Debug)]
struct Crate {
#[serde(rename(serialize = "i"))]
id: String,
name: String,
#[serde(rename(serialize = "d"))]
description: String,
#[serde(rename(serialize = "h"))]
homepage: Option<String>,
#[serde(rename(serialize = "o"))]
documentation: Option<String>,
#[serde(rename(serialize = "r"))]
repository: Option<String>,
#[serde(rename(serialize = "x"))]
downloads: u32,
#[serde(rename(serialize = "v"))]
max_version: String,
}
@ -34,14 +40,13 @@ async fn fetch_crates(page: u32) -> Result<Vec<Crate>, Box<dyn std::error::Error
}
async fn generate_javascript_crates_index(crates: Vec<Crate>) -> std::io::Result<()> {
let mut crate_ids = HashSet::new();
for krate in crates {
crate_ids.insert(krate.id);
}
let mut contents = String::from("");
contents.push_str(&format!("var crateIndex={};\n", serde_json::to_string(&crates).unwrap()));
let crate_ids = crates.into_iter().map(|krate| krate.id).collect::<HashSet<String>>();
contents.push_str(&format!("var crateIds={};\n", serde_json::to_string(&crate_ids).unwrap()));
let path = Path::new(CRATES_INDEX_PATH);
contents.push_str(&format!("var crateIds={};", serde_json::to_string(&crate_ids).unwrap()));
fs::write(path, &contents)?;
Ok(())
}