Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): json schema #5

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 10 additions & 2 deletions packages/autoi18n-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
39 changes: 36 additions & 3 deletions packages/autoi18n-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
}
}