From 0175ae78b49cfd37938de032a3ef72c9605c7c5a Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Thu, 5 Sep 2024 15:43:46 +0200 Subject: [PATCH] feat: setup config loading --- CHANGELOG.md | 1 + Cargo.toml | 4 ++-- README.md | 1 + justfile | 5 ++++ packages/autoi18n-config/Cargo.toml | 2 ++ packages/autoi18n-config/src/error.rs | 31 +++++++++++++++++++++++++ packages/autoi18n-config/src/lib.rs | 33 +++++++++++++++++++-------- packages/autoi18n/Cargo.toml | 1 + packages/autoi18n/src/error/mod.rs | 22 ++++++++++++++++++ packages/autoi18n/src/main.rs | 15 +++++++++++- packages/autoi18n/wix/main.wxs | 1 + 11 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 README.md create mode 100644 packages/autoi18n-config/src/error.rs create mode 100644 packages/autoi18n/src/error/mod.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index acdaa1d..f935724 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,4 +10,5 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). - chore: validation workflow [`#2`](https://github.com/autoi18n/cli/pull/2) - build: setup publishing [`#1`](https://github.com/autoi18n/cli/pull/1) - chore: setup workspace [`11b6f6c`](https://github.com/autoi18n/cli/commit/11b6f6c749b6ff69b7cf84a9131c05be73bde525) +- feat: setup config crate [`e89b17f`](https://github.com/autoi18n/cli/commit/e89b17fa9c6c04215ab21b04f6e967d726957552) - Initial commit [`d96cebc`](https://github.com/autoi18n/cli/commit/d96cebc79c1f5243aa1ed05b2e68aaf5e380c61a) diff --git a/Cargo.toml b/Cargo.toml index 1d6e127..719d82b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,8 @@ edition = "2021" license = "MIT" repository = "https://github.com/autoi18n/cli" documentation = "https://github.com/autoi18n/cli#readme" -keywords = [] -description = "" +keywords = ["i18n"] +description = "CLI tool for autoi18n" homepage = "https://autoi18n.com" readme = "README.md" categories = ["command-line-utilities", "development-tools"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..79e4b72 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# autoi18n-cli diff --git a/justfile b/justfile index 5a73459..673f123 100644 --- a/justfile +++ b/justfile @@ -18,6 +18,11 @@ lint: cargo fmt -- --check --color always cargo clippy --all-targets --all-features -- -D warnings +lint-aggressive: + cargo clean + cargo clippy --all-targets --all-features -- -Dclippy::style -Dclippy::double_neg -Dclippy::perf -Dclippy::pedantic -Dclippy::all -Dclippy::cargo -Dclippy::complexity -Dclippy::nursery -Dclippy::suspicious -Aclippy::module_name_repetitions -Aclippy::missing_errors_doc -Aclippy::must_use_candidate -Aclippy::multiple_crate_versions + cargo clean + test: just lint RUST_BACKTRACE=full cargo test --release diff --git a/packages/autoi18n-config/Cargo.toml b/packages/autoi18n-config/Cargo.toml index d8f807a..ef33c21 100644 --- a/packages/autoi18n-config/Cargo.toml +++ b/packages/autoi18n-config/Cargo.toml @@ -13,3 +13,5 @@ readme.workspace = true categories.workspace = true [dependencies] +serde = { version = "1.0.209", features = ["derive"] } +serde_json = "1.0.128" diff --git a/packages/autoi18n-config/src/error.rs b/packages/autoi18n-config/src/error.rs new file mode 100644 index 0000000..b68954f --- /dev/null +++ b/packages/autoi18n-config/src/error.rs @@ -0,0 +1,31 @@ +#[derive(Debug)] +pub enum ConfigError { + IoError(std::io::Error), + ParseError(serde_json::Error), +} + +impl std::error::Error for ConfigError {} + +impl core::fmt::Display for ConfigError { + #[inline] + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::IoError(e) => e.fmt(f), + Self::ParseError(e) => e.fmt(f), + } + } +} + +impl From for ConfigError { + #[inline] + fn from(value: std::io::Error) -> Self { + Self::IoError(value) + } +} + +impl From for ConfigError { + #[inline] + fn from(value: serde_json::Error) -> Self { + Self::ParseError(value) + } +} diff --git a/packages/autoi18n-config/src/lib.rs b/packages/autoi18n-config/src/lib.rs index b93cf3f..02511af 100644 --- a/packages/autoi18n-config/src/lib.rs +++ b/packages/autoi18n-config/src/lib.rs @@ -1,14 +1,29 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +use error::ConfigError; + +pub mod error; + +#[derive(serde::Serialize, serde::Deserialize)] +pub struct CliConfig { + #[serde(rename = "$schema", default = "CliConfig::default_schema_location")] + pub schema: String, } -#[cfg(test)] -mod tests { - use super::*; +impl CliConfig { + #[inline] + pub fn load(path: impl AsRef) -> Result { + let content = std::fs::read_to_string(path)?; + + let parsed = serde_json::from_str::(&content)?; + + Ok(parsed) + } + + #[inline] + fn default_schema_location() -> String { + let package_version = env!("CARGO_PKG_VERSION"); - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + format!( + "https://raw.githubusercontent.com/autoi18n/cli/main/schemas/v{package_version}/autoi18n.schema.json" + ) } } diff --git a/packages/autoi18n/Cargo.toml b/packages/autoi18n/Cargo.toml index f1b0399..787d0b2 100644 --- a/packages/autoi18n/Cargo.toml +++ b/packages/autoi18n/Cargo.toml @@ -19,3 +19,4 @@ license = false eula = false [dependencies] +autoi18n-config = { workspace = true } diff --git a/packages/autoi18n/src/error/mod.rs b/packages/autoi18n/src/error/mod.rs new file mode 100644 index 0000000..c3076f6 --- /dev/null +++ b/packages/autoi18n/src/error/mod.rs @@ -0,0 +1,22 @@ +#[derive(Debug)] +pub enum CliError { + Config(autoi18n_config::error::ConfigError), +} + +impl std::error::Error for CliError {} + +impl core::fmt::Display for CliError { + #[inline] + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Config(e) => e.fmt(f), + } + } +} + +impl From for CliError { + #[inline] + fn from(value: autoi18n_config::error::ConfigError) -> Self { + Self::Config(value) + } +} diff --git a/packages/autoi18n/src/main.rs b/packages/autoi18n/src/main.rs index e7a11a9..70b6417 100644 --- a/packages/autoi18n/src/main.rs +++ b/packages/autoi18n/src/main.rs @@ -1,3 +1,16 @@ +use autoi18n_config::CliConfig; + +mod error; + +#[inline] +fn _main() -> Result<(), error::CliError> { + let _config = CliConfig::load("autoi18n.json")?; + + Ok(()) +} + fn main() { - println!("Hello, world!"); + if let Err(error) = _main() { + eprintln!("{error}"); + } } diff --git a/packages/autoi18n/wix/main.wxs b/packages/autoi18n/wix/main.wxs index f71556a..11cb599 100644 --- a/packages/autoi18n/wix/main.wxs +++ b/packages/autoi18n/wix/main.wxs @@ -69,6 +69,7 @@