use aube_codes::{CodeMeta, errors, warnings};
use serde::Serialize;
use std::collections::BTreeSet;
use std::fs;
use std::path::{Path, PathBuf};
#[derive(Serialize)]
struct ErrorCodesData<'a> {
errors: &'a [CodeMeta],
warnings: &'a [CodeMeta],
categories: Categories,
}
#[derive(Serialize)]
struct Categories {
errors: Vec<&'static str>,
warnings: Vec<&'static str>,
}
fn main() {
let root = workspace_root();
let out_path = root.join("docs/error-codes.data.json");
let data = ErrorCodesData {
errors: errors::ALL,
warnings: warnings::ALL,
categories: Categories {
errors: ordered_categories(errors::ALL),
warnings: ordered_categories(warnings::ALL),
},
};
let mut json = serde_json::to_string_pretty(&data)
.unwrap_or_else(|e| panic!("failed to serialize error-codes data: {e}"));
json.push('\n');
if let Some(parent) = out_path.parent() {
fs::create_dir_all(parent)
.unwrap_or_else(|e| panic!("failed to create {}: {e}", parent.display()));
}
fs::write(&out_path, json)
.unwrap_or_else(|e| panic!("failed to write {}: {e}", out_path.display()));
println!(
"generated {}",
out_path.strip_prefix(&root).unwrap().display()
);
}
fn workspace_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf()
}
fn ordered_categories(all: &[CodeMeta]) -> Vec<&'static str> {
let mut seen: BTreeSet<&'static str> = BTreeSet::new();
let mut ordered: Vec<&'static str> = Vec::new();
for meta in all {
if seen.insert(meta.category) {
ordered.push(meta.category);
}
}
ordered
}