From d8048654bd4cad7c88dd0b1775eb5cc11e6f473e Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Thu, 5 Sep 2024 20:40:43 +0200 Subject: [PATCH] feat(config): json schema --- Cargo.toml | 4 +++ packages/autoi18n-config/Cargo.toml | 12 +++++++-- packages/autoi18n-config/src/lib.rs | 39 ++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 719d82b..f209ea4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,10 @@ categories = ["command-line-utilities", "development-tools"] [workspace.dependencies] autoi18n-config = { version = "0.0.0", path = "./packages/autoi18n-config" } +schemars = "0.8.21" +serde = { version = "1.0.209", features = ["derive"] } +serde_json = "1.0.128" +tempfile = "3.12.0" # Config for 'cargo dist' [workspace.metadata.dist] diff --git a/packages/autoi18n-config/Cargo.toml b/packages/autoi18n-config/Cargo.toml index ef33c21..0cfb03b 100644 --- a/packages/autoi18n-config/Cargo.toml +++ b/packages/autoi18n-config/Cargo.toml @@ -12,6 +12,14 @@ homepage.workspace = true readme.workspace = true categories.workspace = true +[features] +default = [] +json-schema = ["dep:schemars"] + [dependencies] -serde = { version = "1.0.209", features = ["derive"] } -serde_json = "1.0.128" +schemars = { workspace = true, optional = true } +serde = { workspace = true } +serde_json = { workspace = true } + +[dev-dependencies] +tempfile = { workspace = true } diff --git a/packages/autoi18n-config/src/lib.rs b/packages/autoi18n-config/src/lib.rs index 02511af..33eda84 100644 --- a/packages/autoi18n-config/src/lib.rs +++ b/packages/autoi18n-config/src/lib.rs @@ -2,7 +2,9 @@ use error::ConfigError; pub mod error; -#[derive(serde::Serialize, serde::Deserialize)] +#[derive(serde::Serialize, serde::Deserialize, Default)] +#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))] +#[cfg_attr(test, derive(Debug, PartialEq, Eq))] pub struct CliConfig { #[serde(rename = "$schema", default = "CliConfig::default_schema_location")] pub schema: String, @@ -23,7 +25,38 @@ impl CliConfig { let package_version = env!("CARGO_PKG_VERSION"); format!( - "https://raw.githubusercontent.com/autoi18n/cli/main/schemas/v{package_version}/autoi18n.schema.json" - ) + "https://raw.githubusercontent.com/autoi18n/cli/main/schemas/v{package_version}/autoi18n.schema.json" + ) + } +} + +#[cfg(test)] +mod test_config { + + use crate::CliConfig; + + #[test] + fn config_should_be_serializable() { + let config = CliConfig::default(); + + let json = serde_json::to_string_pretty(&config).expect("it to be serializable"); + + let file = tempfile::Builder::new() + .suffix(".json") + .tempfile() + .expect("it to create file"); + + std::fs::write(file.path(), json).expect("it to write to file"); + + let loaded = CliConfig::load(file.path()).expect("it to be parsed"); + + assert_eq!(config, loaded); + } + + #[test] + #[cfg(feature = "json-schema")] + fn json_schema_should_be_serializable() { + serde_json::to_string_pretty(&schemars::schema_for!(CliConfig)) + .expect("it to be serializable"); } }