feat: impl dashboard & issue list
This commit is contained in:
82
build.rs
82
build.rs
@@ -13,9 +13,13 @@ fn main() {
|
||||
let manifest_dir =
|
||||
PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("missing CARGO_MANIFEST_DIR"));
|
||||
let asset_root = manifest_dir.join("src/asset");
|
||||
let out_file = PathBuf::from(env::var("OUT_DIR").expect("missing OUT_DIR")).join("asset.rs");
|
||||
let fixture_root = manifest_dir.join("fixtures/github");
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("missing OUT_DIR"));
|
||||
let asset_out_file = out_dir.join("asset.rs");
|
||||
let fixture_out_file = out_dir.join("github_fixtures.rs");
|
||||
|
||||
println!("cargo::rerun-if-changed={}", asset_root.display());
|
||||
println!("cargo::rerun-if-changed={}", fixture_root.display());
|
||||
|
||||
let mut directory_entries = BTreeMap::<String, BTreeSet<String>>::new();
|
||||
directory_entries
|
||||
@@ -32,8 +36,11 @@ fn main() {
|
||||
);
|
||||
|
||||
let generated = render_assets(&asset_files, &directory_entries);
|
||||
let fixture_module = render_github_fixtures(&fixture_root);
|
||||
|
||||
fs::write(out_file, generated).expect("failed to write generated assets module");
|
||||
fs::write(asset_out_file, generated).expect("failed to write generated assets module");
|
||||
fs::write(fixture_out_file, fixture_module)
|
||||
.expect("failed to write generated github fixtures module");
|
||||
}
|
||||
|
||||
fn collect_assets(
|
||||
@@ -117,6 +124,77 @@ fn render_assets(
|
||||
output
|
||||
}
|
||||
|
||||
fn render_github_fixtures(fixture_root: &Path) -> String {
|
||||
let user_fetch = read_json_fixture(&fixture_root.join("user.fetch.json"));
|
||||
let repo_list = read_json_fixture(&fixture_root.join("repo.list.json"));
|
||||
|
||||
let mut issue_fixtures = BTreeMap::<(String, u32), String>::new();
|
||||
let mut entries = fs::read_dir(fixture_root)
|
||||
.unwrap_or_else(|err| panic!("failed to read {}: {err}", fixture_root.display()))
|
||||
.map(|entry| entry.expect("failed to read github fixture entry"))
|
||||
.collect::<Vec<_>>();
|
||||
entries.sort_by_key(|entry| entry.file_name());
|
||||
|
||||
for entry in entries {
|
||||
let file_name = entry
|
||||
.file_name()
|
||||
.into_string()
|
||||
.unwrap_or_else(|_| panic!("non-utf8 fixture name in {}", fixture_root.display()));
|
||||
|
||||
let Some((filter, page)) = parse_issue_fixture_name(&file_name) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
issue_fixtures.insert((filter, page), read_json_fixture(&entry.path()));
|
||||
}
|
||||
|
||||
let mut output = String::new();
|
||||
|
||||
output.push_str("pub fn user_fetch() -> &'static str {\n");
|
||||
output.push_str(" ");
|
||||
output.push_str(&string_literal(&user_fetch));
|
||||
output.push_str("\n}\n\n");
|
||||
|
||||
output.push_str("pub fn repo_list() -> &'static str {\n");
|
||||
output.push_str(" ");
|
||||
output.push_str(&string_literal(&repo_list));
|
||||
output.push_str("\n}\n\n");
|
||||
|
||||
output.push_str("pub fn issues_pull_requests(filter: &str, page: u32) -> Option<&'static str> {\n");
|
||||
output.push_str(" match (filter, page) {\n");
|
||||
for ((filter, page), json) in issue_fixtures {
|
||||
output.push_str(" (");
|
||||
output.push_str(&string_literal(&filter));
|
||||
output.push_str(", ");
|
||||
output.push_str(&page.to_string());
|
||||
output.push_str(") => Some(");
|
||||
output.push_str(&string_literal(&json));
|
||||
output.push_str("),\n");
|
||||
}
|
||||
output.push_str(" _ => None,\n");
|
||||
output.push_str(" }\n");
|
||||
output.push_str("}\n");
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
fn read_json_fixture(path: &Path) -> String {
|
||||
let raw = fs::read_to_string(path)
|
||||
.unwrap_or_else(|err| panic!("failed to read fixture {}: {err}", path.display()));
|
||||
let value: serde_json::Value = serde_json::from_str(&raw)
|
||||
.unwrap_or_else(|err| panic!("invalid json fixture {}: {err}", path.display()));
|
||||
serde_json::to_string(&value)
|
||||
.unwrap_or_else(|err| panic!("failed to serialize fixture {}: {err}", path.display()))
|
||||
}
|
||||
|
||||
fn parse_issue_fixture_name(file_name: &str) -> Option<(String, u32)> {
|
||||
let name = file_name.strip_suffix(".json")?;
|
||||
let rest = name.strip_prefix("issues.pull_requests.")?;
|
||||
let (filter, page) = rest.rsplit_once(".page")?;
|
||||
let page = page.parse::<u32>().ok()?;
|
||||
Some((filter.to_owned(), page))
|
||||
}
|
||||
|
||||
fn string_literal(value: &str) -> String {
|
||||
format!("{value:?}")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user