mirror of
https://github.com/huhu/rust-search-extension
synced 2024-11-14 23:57:07 +00:00
Move minify function to Minifer struct
This commit is contained in:
parent
9c0e254b7e
commit
187ea4464a
2 changed files with 45 additions and 45 deletions
|
@ -13,7 +13,7 @@ use tokio;
|
|||
use tokio::time::Duration;
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use minify::MappingMinifier;
|
||||
use minify::Minifier;
|
||||
|
||||
mod minify;
|
||||
|
||||
|
@ -67,7 +67,7 @@ async fn fetch_crates(page: u32) -> Result<Vec<Crate>, Box<dyn std::error::Error
|
|||
|
||||
async fn generate_javascript_crates_index(
|
||||
crates: Vec<Crate>,
|
||||
mapping_minifier: &MappingMinifier,
|
||||
minifier: &Minifier,
|
||||
) -> std::io::Result<String> {
|
||||
let mut contents = String::from("var N=null;");
|
||||
let crates_map: HashMap<String, [Option<String>; 3]> = crates
|
||||
|
@ -76,8 +76,8 @@ async fn generate_javascript_crates_index(
|
|||
(
|
||||
item.id.to_lowercase(),
|
||||
[
|
||||
item.description.map(|value| mapping_minifier.minify(value)),
|
||||
item.documentation.map(minify::minify_url),
|
||||
item.description.map(|value| minifier.mapping_minify(value)),
|
||||
item.documentation.map(Minifier::minify_url),
|
||||
item.max_version,
|
||||
],
|
||||
)
|
||||
|
@ -87,7 +87,7 @@ async fn generate_javascript_crates_index(
|
|||
"var crateIndex={};",
|
||||
serde_json::to_string(&crates_map).unwrap()
|
||||
);
|
||||
contents.push_str(&minify::minify_json(crate_index));
|
||||
contents.push_str(&Minifier::minify_json(crate_index));
|
||||
Ok(contents)
|
||||
}
|
||||
|
||||
|
@ -115,10 +115,10 @@ async fn main() -> std::io::Result<()> {
|
|||
})
|
||||
.collect();
|
||||
// Extract frequency word mapping
|
||||
let mapping_minifier = MappingMinifier::new(&STRING_VEC.read().unwrap(), 25);
|
||||
let mapping = mapping_minifier.get_mapping();
|
||||
let minifier = Minifier::new(&STRING_VEC.read().unwrap(), 25);
|
||||
let mapping = minifier.get_mapping();
|
||||
let mut contents = format!("var M={};", serde_json::to_string(&mapping).unwrap());
|
||||
contents.push_str(&generate_javascript_crates_index(crates, &mapping_minifier).await?);
|
||||
contents.push_str(&generate_javascript_crates_index(crates, &minifier).await?);
|
||||
fs::write(path, &contents)?;
|
||||
println!("\nGenerate javascript crates index successful!");
|
||||
Ok(())
|
||||
|
|
|
@ -13,14 +13,14 @@ struct FrequencyWord {
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct MappingMinifier {
|
||||
pub(crate) struct Minifier {
|
||||
mapping: HashMap<String, String>,
|
||||
}
|
||||
|
||||
impl MappingMinifier {
|
||||
impl Minifier {
|
||||
const UPPERCASE_LETTERS: &'static str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
pub fn new(words: &Vec<String>, top: usize) -> MappingMinifier {
|
||||
pub fn new(words: &Vec<String>, top: usize) -> Minifier {
|
||||
assert!(top < Self::UPPERCASE_LETTERS.len());
|
||||
let mut mapping: HashMap<String, usize> = HashMap::new();
|
||||
words
|
||||
|
@ -45,7 +45,7 @@ impl MappingMinifier {
|
|||
.drain(0..=top)
|
||||
.collect::<Vec<FrequencyWord>>();
|
||||
|
||||
MappingMinifier {
|
||||
Minifier {
|
||||
mapping: words
|
||||
.iter()
|
||||
.enumerate()
|
||||
|
@ -59,7 +59,14 @@ impl MappingMinifier {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn minify(&self, value: String) -> String {
|
||||
pub fn get_mapping(&self) -> HashMap<String, String> {
|
||||
self.mapping
|
||||
.iter()
|
||||
.map(|(key, value)| (value.to_owned(), key.to_owned()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn mapping_minify(&self, value: String) -> String {
|
||||
value
|
||||
.split_word_bounds()
|
||||
.into_iter()
|
||||
|
@ -67,37 +74,30 @@ impl MappingMinifier {
|
|||
.collect()
|
||||
}
|
||||
|
||||
pub fn get_mapping(&self) -> HashMap<String, String> {
|
||||
self.mapping
|
||||
.iter()
|
||||
.map(|(key, value)| (value.to_owned(), key.to_owned()))
|
||||
.collect()
|
||||
pub fn minify_url(url: String) -> String {
|
||||
url.to_lowercase()
|
||||
.replace("http://", "")
|
||||
.replace("https://", "")
|
||||
.replace("docs.rs", "D")
|
||||
.replace("crates.io", "C")
|
||||
.replace("github.io", "O")
|
||||
.replace("github.com", "G")
|
||||
.replace("index.html", "I")
|
||||
}
|
||||
|
||||
pub fn minify_json(json: String) -> String {
|
||||
let tokens: Tokens = simple_minify(&json)
|
||||
.into_iter()
|
||||
.map(|(token, _)| match token {
|
||||
Token::Keyword(Keyword::Null) => Token::Other("N"),
|
||||
_ => token,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into();
|
||||
aggregate_strings_into_array_filter(tokens, "C", |tokens, position| {
|
||||
// Ignore the key of json (AKA, the crate id).
|
||||
position > 5 && !tokens[position + 1].eq_char(ReservedChar::Colon)
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn minify_url(url: String) -> String {
|
||||
url.to_lowercase()
|
||||
.replace("http://", "")
|
||||
.replace("https://", "")
|
||||
.replace("docs.rs", "D")
|
||||
.replace("crates.io", "C")
|
||||
.replace("github.io", "O")
|
||||
.replace("github.com", "G")
|
||||
.replace("index.html", "I")
|
||||
}
|
||||
|
||||
pub(crate) fn minify_json(json: String) -> String {
|
||||
let tokens: Tokens = simple_minify(&json)
|
||||
.into_iter()
|
||||
.map(|(token, _)| match token {
|
||||
Token::Keyword(Keyword::Null) => Token::Other("N"),
|
||||
_ => token,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into();
|
||||
aggregate_strings_into_array_filter(tokens, "C", |tokens, position| {
|
||||
// Ignore the key of json (AKA, the crate id).
|
||||
position > 5 && !tokens[position + 1].eq_char(ReservedChar::Colon)
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue