Skip to content

Commit

Permalink
feat: setup config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Sep 5, 2024
1 parent e89b17f commit 0175ae7
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# autoi18n-cli
5 changes: 5 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/autoi18n-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ readme.workspace = true
categories.workspace = true

[dependencies]
serde = { version = "1.0.209", features = ["derive"] }
serde_json = "1.0.128"
31 changes: 31 additions & 0 deletions packages/autoi18n-config/src/error.rs
Original file line number Diff line number Diff line change
@@ -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<std::io::Error> for ConfigError {
#[inline]
fn from(value: std::io::Error) -> Self {
Self::IoError(value)
}
}

impl From<serde_json::Error> for ConfigError {
#[inline]
fn from(value: serde_json::Error) -> Self {
Self::ParseError(value)
}
}
33 changes: 24 additions & 9 deletions packages/autoi18n-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<std::path::Path>) -> Result<Self, ConfigError> {
let content = std::fs::read_to_string(path)?;

let parsed = serde_json::from_str::<Self>(&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"
)
}
}
1 change: 1 addition & 0 deletions packages/autoi18n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ license = false
eula = false

[dependencies]
autoi18n-config = { workspace = true }
22 changes: 22 additions & 0 deletions packages/autoi18n/src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -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<autoi18n_config::error::ConfigError> for CliError {
#[inline]
fn from(value: autoi18n_config::error::ConfigError) -> Self {
Self::Config(value)
}
}
15 changes: 14 additions & 1 deletion packages/autoi18n/src/main.rs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
1 change: 1 addition & 0 deletions packages/autoi18n/wix/main.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

<Package Id='*'
Keywords='Installer'
Description='CLI tool for autoi18n'
Manufacturer='Mads Hougesen'
InstallerVersion='450'
Languages='1033'
Expand Down

0 comments on commit 0175ae7

Please sign in to comment.