Skip to content

Commit

Permalink
Refactor HZD language parsing in CLI
Browse files Browse the repository at this point in the history
- Move shared HZD language parsing logic to new parse_hzd_languages function in shared.rs
- Update functions calling language parsing to use new shared function
- Allow passing "all" to export/import all languages
- Bump version to 0.1.1
  • Loading branch information
YouKnow-sys committed Feb 10, 2024
1 parent 7faa56c commit 331986f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dloc-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dloc-cli"
version = "0.1.0"
version = "0.1.1"
authors.workspace = true
edition.workspace = true

Expand Down
19 changes: 3 additions & 16 deletions dloc-cli/src/commands/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ use std::path::PathBuf;

use anyhow::bail;
use clap::{Parser, ValueHint};
use dloc_core::{
games::hzd::{self, HZDLocal},
logger::Logger,
serialize::DecimaGroup,
};
use dloc_core::{games::hzd::HZDLocal, logger::Logger, serialize::DecimaGroup};

use crate::{logger::CliLogger, Game};

use super::{
shared::{HzdAction, SerializeType},
shared::{parse_hzd_languages, HzdAction, SerializeType},
utils,
};

Expand Down Expand Up @@ -45,16 +41,7 @@ impl Group {
.with_extension(self.serialize_type.extension())
});

let languages: Vec<_> = languages
.into_iter()
.filter_map(|s| match hzd::Language::try_from(s.clone()) {
Ok(r) => Some(r),
Err(_) => {
logger.warn(format!("Invalid language: {s}"));
None
}
})
.collect();
let languages = parse_hzd_languages(languages, &mut logger);

if languages.is_empty() {
bail!("Didn't found any valid Language.");
Expand Down
21 changes: 19 additions & 2 deletions dloc-cli/src/commands/shared.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;

use clap::{Subcommand, ValueEnum, ValueHint};
use dloc_core::serialize::SerializeType as CoreSerializeType;
use dloc_core::{games::hzd, logger::Logger, serialize::SerializeType as CoreSerializeType};

use super::utils;

Expand Down Expand Up @@ -38,7 +38,7 @@ pub enum HzdAction {
/// Export locals from input
#[command(arg_required_else_help = true)]
Export {
/// Languages to export
/// Languages to export, pass 'all' if you want to export everything
#[arg(num_args = 1)]
languages: Vec<String>,
/// This option is only used when serialize-type is Txt
Expand All @@ -65,3 +65,20 @@ impl HzdAction {
}
}
}

pub fn parse_hzd_languages(languages: Vec<String>, logger: &mut impl Logger) -> Vec<hzd::Language> {
if languages.iter().any(|l| l.eq_ignore_ascii_case("all")) {
hzd::Language::ALL_VARIANTS.to_vec()
} else {
languages
.into_iter()
.filter_map(|s| match hzd::Language::try_from(s.clone()) {
Ok(r) => Some(r),
Err(_) => {
logger.warn(format!("Invalid language: {s}"));
None
}
})
.collect()
}
}
19 changes: 3 additions & 16 deletions dloc-cli/src/commands/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ use std::{

use anyhow::bail;
use clap::{Parser, ValueHint};
use dloc_core::{
games::hzd::{self, HZDLocal},
logger::Logger,
serialize::SerializeData,
};
use dloc_core::{games::hzd::HZDLocal, logger::Logger, serialize::SerializeData};

use crate::{logger::CliLogger, Game};

use super::{
shared::{HzdAction, SerializeType},
shared::{parse_hzd_languages, HzdAction, SerializeType},
utils,
};

Expand Down Expand Up @@ -61,16 +57,7 @@ impl Single {
.with_extension(self.serialize_type.extension())
});

let languages: Vec<_> = languages
.into_iter()
.filter_map(|s| match hzd::Language::try_from(s.clone()) {
Ok(r) => Some(r),
Err(_) => {
logger.warn(format!("Invalid language: {s}"));
None
}
})
.collect();
let languages = parse_hzd_languages(languages, &mut logger);

if languages.is_empty() {
bail!("Didn't found any valid Language.");
Expand Down

0 comments on commit 331986f

Please sign in to comment.