From 6b0db3097527775913639b700b9524249ead18cd Mon Sep 17 00:00:00 2001 From: dark0dave Date: Wed, 3 Jul 2024 16:08:46 +0100 Subject: [PATCH] fix(qustions): Better question parsing, fixes #81 Signed-off-by: dark0dave --- .pre-commit-config.yaml | 20 ++++++++++++++++++-- src/weidu_parser.rs | 27 ++++++++++++++++----------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f44e5bc..a2190d8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,30 +1,41 @@ +default_install_hook_types: [pre-commit, commit-msg] repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 hooks: - id: check-added-large-files exclude: (?x)^(docs) + stages: [pre-commit] - id: check-case-conflict + stages: [pre-commit] - id: check-merge-conflict + stages: [pre-commit] - id: detect-private-key + stages: [pre-commit] - id: forbid-new-submodules + stages: [pre-commit] - id: check-builtin-literals + stages: [pre-commit] - repo: https://github.com/jumanjihouse/pre-commit-hooks rev: 3.0.0 hooks: - id: forbid-binary + stages: [pre-commit] exclude: (?x)^(docs) - id: git-dirty + stages: [pre-commit] - repo: https://github.com/Lucas-C/pre-commit-hooks rev: v1.5.5 hooks: - id: forbid-crlf + stages: [pre-commit] - id: remove-crlf + stages: [pre-commit] - repo: https://github.com/commitizen-tools/commitizen - rev: v3.22.0 + rev: v3.27.0 hooks: - id: commitizen stages: [commit-msg] @@ -33,12 +44,16 @@ repos: rev: v1.0 hooks: - id: fmt + stages: [pre-commit] - id: cargo-check + stages: [pre-commit] - id: clippy + stages: [pre-commit] - repo: local hooks: - id: cargo-test + stages: [pre-commit] name: cargo test description: Run the test suite entry: cargo test @@ -47,8 +62,9 @@ repos: pass_filenames: false - repo: https://github.com/codespell-project/codespell - rev: v2.2.6 + rev: v2.3.0 hooks: - id: codespell + stages: [pre-commit] args: - '--ignore-words-list=crate' diff --git a/src/weidu_parser.rs b/src/weidu_parser.rs index 0b80180..ae3ba3f 100644 --- a/src/weidu_parser.rs +++ b/src/weidu_parser.rs @@ -139,12 +139,12 @@ fn string_looks_like_question(weidu_output: &str) -> bool { { return false; } - (WEIDU_CHOICE.contains(&comparable_output.as_str())) - || WEIDU_CHOICE_SYMBOL.contains(&comparable_output.chars().last().unwrap_or_default()) - || comparable_output - .split(' ') - .take(1) - .any(|c| WEIDU_CHOICE.contains(&c)) + return WEIDU_CHOICE + .iter() + .map(|choice| comparable_output.contains(choice)) + .reduce(|a, b| a | b) + .unwrap_or(false) + || WEIDU_CHOICE_SYMBOL.contains(&comparable_output.chars().last().unwrap_or_default()); } fn detect_weidu_finished_state(weidu_output: &str) -> Option { @@ -191,10 +191,15 @@ mod tests { #[test] fn is_a_question() { - let test = "Enter the full path to your Baldur's Gate installation then press Enter."; - assert_eq!(string_looks_like_question(test), true); - let test = "Enter the full path to your BG:EE+SoD installation then press Enter.\ -Example: C:\\Program Files (x86)\\BeamDog\\Games\\00806"; - assert_eq!(string_looks_like_question(test), true) + let tests = vec!["Enter the full path to your Baldur's Gate installation then press Enter.", "Enter the full path to your BG:EE+SoD installation then press Enter.\ +Example: C:\\Program Files (x86)\\BeamDog\\Games\\00806", "[N]o, [Q]uit or choose one:", "Please enter the chance for items to randomly not be randomised as a integet number (e.g. 10 for 10%)"]; + for question in tests { + assert_eq!( + string_looks_like_question(question), + true, + "String {} doesn't look like a string", + question + ); + } } }