| 1 |
#![expect( |
| 2 |
clippy::unwrap_used, |
| 3 |
reason = "this is just used to generate jsonschema files" |
| 4 |
)] |
| 5 |
|
| 6 |
use bottom::{options::config, widgets}; |
| 7 |
use clap::Parser; |
| 8 |
use itertools::Itertools; |
| 9 |
use serde_json::Value; |
| 10 |
use strum::VariantArray; |
| 11 |
|
| 12 |
#[derive(Parser)] |
| 13 |
struct SchemaOptions { |
| 14 |
|
| 15 |
version: Option<String>, |
| 16 |
} |
| 17 |
|
| 18 |
macro_rules! generate_column_schemas { |
| 19 |
($struct_name:literal, $variants:expr, $schema:expr) => { |
| 20 |
match $schema |
| 21 |
.as_object_mut() |
| 22 |
.unwrap() |
| 23 |
.get_mut("$defs") |
| 24 |
.unwrap() |
| 25 |
.get_mut($struct_name) |
| 26 |
.unwrap() |
| 27 |
{ |
| 28 |
Value::Object(original) => { |
| 29 |
let enums = original.get_mut("enum").unwrap(); |
| 30 |
*enums = $variants |
| 31 |
.iter() |
| 32 |
.flat_map(|variant| variant.get_schema_names()) |
| 33 |
.flat_map(|variant| [variant.to_string(), variant.to_lowercase()]) |
| 34 |
.sorted() |
| 35 |
.dedup() |
| 36 |
.map(|variant| serde_json::Value::String(variant)) |
| 37 |
.collect(); |
| 38 |
|
| 39 |
Ok(()) |
| 40 |
} |
| 41 |
_ => Err(anyhow::anyhow!("missing proc columns definition")), |
| 42 |
} |
| 43 |
}; |
| 44 |
} |
| 45 |
|
| 46 |
fn generate_schema(schema_options: SchemaOptions) -> anyhow::Result<()> { |
| 47 |
let mut schema = schemars::schema_for!(config::Config); |
| 48 |
{ |
| 49 |
|
| 50 |
generate_column_schemas!("ProcColumn", widgets::ProcColumn::VARIANTS, schema)?; |
| 51 |
generate_column_schemas!( |
| 52 |
"DiskWidgetColumn", |
| 53 |
widgets::DiskWidgetColumn::VARIANTS, |
| 54 |
schema |
| 55 |
)?; |
| 56 |
generate_column_schemas!( |
| 57 |
"TempWidgetColumn", |
| 58 |
widgets::TempWidgetColumn::VARIANTS, |
| 59 |
schema |
| 60 |
)?; |
| 61 |
} |
| 62 |
|
| 63 |
let version = schema_options.version.unwrap_or("nightly".to_string()); |
| 64 |
let version_with_v = if version == "nightly" { |
| 65 |
"nightly".to_string() |
| 66 |
} else { |
| 67 |
format!("v{version}") |
| 68 |
}; |
| 69 |
|
| 70 |
schema.insert( |
| 71 |
"$id".into(), |
| 72 |
format!( |
| 73 |
"https://github.com/ClementTsang/bottom/blob/main/schema/{version_with_v}/bottom.json" |
| 74 |
) |
| 75 |
.into(), |
| 76 |
); |
| 77 |
|
| 78 |
schema.insert( |
| 79 |
"description".into(), |
| 80 |
format!("https://bottom.pages.dev/{version}/configuration/config-file/").into(), |
| 81 |
); |
| 82 |
|
| 83 |
schema.insert( |
| 84 |
"title".into(), |
| 85 |
format!("Schema for bottom's config file ({version_with_v})").into(), |
| 86 |
); |
| 87 |
|
| 88 |
println!("{}", serde_json::to_string_pretty(&schema).unwrap()); |
| 89 |
|
| 90 |
Ok(()) |
| 91 |
} |
| 92 |
|
| 93 |
fn main() -> anyhow::Result<()> { |
| 94 |
let schema_options = SchemaOptions::parse(); |
| 95 |
generate_schema(schema_options)?; |
| 96 |
|
| 97 |
Ok(()) |
| 98 |
} |