Skip to content

Commit

Permalink
refactor: codegen out of build.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Fishrock123 committed Jul 31, 2022
1 parent 4ef54af commit cc1551b
Show file tree
Hide file tree
Showing 7 changed files with 843 additions and 68 deletions.
17 changes: 6 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[workspace]
members = [
"generate-database"
]

[package]
name = "tide-compress"
version = "0.10.5"
Expand All @@ -22,11 +27,9 @@ brotli = ["async-compression/brotli"]
gzip = ["async-compression/gzip"]
deflate = ["async-compression/deflate"]

db-check = ["regex-check", "phf", "_build_deps"]
db-check = ["regex-check", "phf"]
regex-check = ["regex"]

_build_deps = ["async-std", "phf_codegen", "serde", "serde_json", "surf"]

[dependencies]
async-compression = { version = "0.3", features = ["futures-bufread"] }
futures-lite = "1.11"
Expand All @@ -44,14 +47,6 @@ version = "1"
default-features = false # Disable features which are enabled by default
features = ["user-hooks"]

[build-dependencies]
async-std = { version = "1.11", features = ["attributes"], optional = true }
cfg-if = "1"
phf_codegen = { version = "0.10", optional = true }
serde = { version = "1", features = ["derive"], optional = true }
serde_json = { version = "1", optional = true }
surf = { version = "2.3.2", default-features = false, features = ["h1-client-rustls"], optional = true }

[package.metadata.docs.rs]
# Prevent docs.rs from trying to pull down the database...
no-default-features = true
Expand Down
49 changes: 0 additions & 49 deletions build.rs

This file was deleted.

14 changes: 14 additions & 0 deletions generate-database/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "generate-database"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = { version = "1.11", features = ["attributes"] }
cfg-if = "1"
phf_codegen = "0.10"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
surf = { version = "2.3.2", default-features = false, features = ["h1-client-rustls"] }
44 changes: 44 additions & 0 deletions generate-database/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::collections::HashMap;
use std::env;
use std::path::Path;

use async_std::fs::File;
use async_std::io::BufWriter;
use async_std::io::WriteExt;
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
struct MimeInfo {
compressible: Option<bool>,
}

#[async_std::main]
async fn main() -> surf::Result<()> {
let path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap())
.join("..")
.join("src")
.join("codegen_database.rs");
let mut file = BufWriter::new(File::create(&path).await?);

let mime_db: HashMap<String, MimeInfo> =
surf::get("https://raw.githubusercontent.com/jshttp/mime-db/master/db.json")
.recv_json()
.await?;

let mut builder = phf_codegen::Set::new();

for (key, info) in mime_db {
if matches!(info.compressible, Some(yes) if yes) {
builder.entry(key);
}
}

writeln!(
&mut file,
"pub(crate) const MIME_DB: phf::Set<&'static str> =\n{};\n",
builder.build()
)
.await?;

Ok(())
}
Loading

0 comments on commit cc1551b

Please sign in to comment.