From ec803c3bd03985ea8cdd2f2b6281ce7e4f132241 Mon Sep 17 00:00:00 2001 From: Folyd Date: Tue, 19 Nov 2019 18:59:07 +0800 Subject: [PATCH] Optimize format to reduce crates-index.js file size --- rust/src/main.rs | 54 ++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index ed792cf..d2df85f 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -3,8 +3,6 @@ use std::fs; use std::path::Path; use futures::future::join_all; -use serde::ser::Serialize; -use serde::Serializer; use serde_derive::{Deserialize, Serialize}; use serde_json; use tokio; @@ -12,19 +10,23 @@ use tokio; const API: &'static str = "https://crates.io/api/v1/crates?page={}&per_page=100&sort=downloads"; const CRATES_INDEX_PATH: &'static str = "../extension/crates-index.js"; -#[derive(Deserialize, Debug)] -struct MinifiedUrl(String); +trait MinifiedUrl: Sized { + fn minify_url(&self) -> Self; +} -impl Serialize for MinifiedUrl { - fn serialize(&self, serializer: S) -> Result where S: Serializer { - let url = self.0 - .replace("http://", "") - .replace("https://", "") - .replace("docs.rs", "D") - .replace("crates.io", "C") - .replace("github.io", "O") - .replace("github.com", "G"); - serializer.serialize_str(&url) +impl MinifiedUrl for Option { + fn minify_url(&self) -> Self { + match self { + Some(value) => Some(value + .replace("http://", "") + .replace("https://", "") + .replace("docs.rs", "D") + .replace("crates.io", "C") + .replace("github.io", "O") + .replace("github.com", "G") + .replace("index.html", "I")), + None => None + } } } @@ -35,14 +37,10 @@ struct CrateApiResponse { #[derive(Serialize, Deserialize, Debug)] struct Crate { - #[serde(rename(serialize = "i"))] id: String, - #[serde(rename(serialize = "d"))] - description: String, - #[serde(rename(serialize = "o"))] - documentation: Option, - #[serde(rename(serialize = "v"))] - max_version: String, + description: Option, + documentation: Option, + max_version: Option, } async fn fetch_crates(page: u32) -> Result, Box> { @@ -53,9 +51,15 @@ async fn fetch_crates(page: u32) -> Result, Box) -> std::io::Result<()> { let mut contents = String::from("var N=null;"); - let crates_map: HashMap = crates.into_iter() - .map(|item| (item.id.to_lowercase(), item)) - .collect(); + let crates_map: HashMap; 3]> = crates.into_iter() + .map(|item| (item.id.to_lowercase(), [ + item.description.map(|mut value| { + value.truncate(100); + value + }), + item.documentation.minify_url(), + item.max_version + ])).collect(); let mut crate_index = format!("var crateIndex={};", serde_json::to_string(&crates_map).unwrap()); crate_index = crate_index.replace("null", "N"); contents.push_str(&crate_index); @@ -69,7 +73,7 @@ async fn generate_javascript_crates_index(crates: Vec) -> std::io::Result #[tokio::main] async fn main() { let mut futures = vec![]; - for page in 1..=10 { + for page in 1..=100 { futures.push(fetch_crates(page)); } let crates: Vec = join_all(futures).await