From 658d8003e6b93fdc657cc7ff4878007b2cf56ddb Mon Sep 17 00:00:00 2001 From: dark0dave Date: Sun, 14 Apr 2024 00:25:11 +0100 Subject: [PATCH] fix(parsing): Fix parsing error for windows closes #70 Signed-off-by: dark0dave --- .pre-commit-config.yaml | 4 +- Cargo.lock | 94 +++++++++++++++++++++++++++-------------- Cargo.toml | 3 ++ src/mod_component.rs | 76 +++++++++++++++++++++++++-------- src/utils.rs | 3 +- src/weidu_parser.rs | 2 + 6 files changed, 129 insertions(+), 53 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 66257f3..f44e5bc 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v4.6.0 hooks: - id: check-added-large-files exclude: (?x)^(docs) @@ -24,7 +24,7 @@ repos: - id: remove-crlf - repo: https://github.com/commitizen-tools/commitizen - rev: v3.18.0 + rev: v3.22.0 hooks: - id: commitizen stages: [commit-msg] diff --git a/Cargo.lock b/Cargo.lock index f1803be..cda533e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "aho-corasick" -version = "1.1.2" +version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] @@ -105,6 +105,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "env_filter" version = "0.1.0" @@ -154,9 +160,9 @@ checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" [[package]] name = "memchr" -version = "2.7.1" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "mod_installer" @@ -166,32 +172,43 @@ dependencies = [ "env_logger", "fs_extra", "log", + "pretty_assertions", "walkdir", ] +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro2" -version = "1.0.78" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" +checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "regex" -version = "1.10.3" +version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", @@ -212,9 +229,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.2" +version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" +checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" [[package]] name = "same-file" @@ -227,15 +244,15 @@ dependencies = [ [[package]] name = "strsim" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.52" +version = "2.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b699d15b36d1f02c3e7c69f8ffef53de37aefae075d8488d4ba1a7788d574a07" +checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687" dependencies = [ "proc-macro2", "quote", @@ -306,13 +323,14 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", + "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", @@ -321,42 +339,54 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" + +[[package]] +name = "yansi" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" diff --git a/Cargo.toml b/Cargo.toml index e2890f5..42319f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,6 @@ env_logger = "0.11.1" fs_extra = "1.3.0" log = "*" walkdir = "2.3.2" + +[dev-dependencies] +pretty_assertions = "1.3.0" diff --git a/src/mod_component.rs b/src/mod_component.rs index 1185848..c49cf15 100644 --- a/src/mod_component.rs +++ b/src/mod_component.rs @@ -1,7 +1,7 @@ use std::{ fs::File, io::{BufRead, BufReader}, - path::{PathBuf, MAIN_SEPARATOR}, + path::PathBuf, }; #[derive(Debug, PartialEq, PartialOrd, Clone)] @@ -21,36 +21,60 @@ impl From for ModComponent { let install_path = parts .nth(1) - .unwrap_or_else(|| panic!("Could not get full name of mod, from: {}", line)) + .unwrap_or_else(|| { + panic!( + "Could not get full name of mod, from provided string: {}", + line + ) + }) .to_string(); - let tp_file = install_path - .split(MAIN_SEPARATOR) - .nth(1) - .unwrap_or_else(|| panic!("Could not find tp2 file, from: {}", line)) - .to_string(); - - let name = install_path - .split(MAIN_SEPARATOR) - .next() - .unwrap_or_else(|| panic!("Could not split {} into mod into name and component", line)) - .to_ascii_lowercase(); + // This allows for both linux, macos and windows parsing + let (tp_file, name) = if let Some(windows_path) = install_path.split('\\').nth(1) { + let name = install_path + .split('\\') + .next() + .unwrap_or_else(|| { + panic!("Could not split {} into mod into name and component", line) + }) + .to_ascii_lowercase(); + (windows_path.to_string(), name) + } else if let Some(linux_path) = install_path.split('/').nth(1) { + let name = install_path + .split('/') + .next() + .unwrap_or_else(|| { + panic!("Could not split {} into mod into name and component", line) + }) + .to_ascii_lowercase(); + (linux_path.to_string(), name) + } else { + panic!( + "Could not find tp2 file name, from provided string: {}", + line + ) + }; let mut tail = parts .next() - .unwrap_or_else(|| panic!("Could not find lang and component, from {}", line)) + .unwrap_or_else(|| { + panic!( + "Could not find lang and component, from provided string {}", + line + ) + }) .split("//"); let mut lang_and_component = tail.next().unwrap_or_default().split(' '); let lang = lang_and_component .nth(1) - .unwrap_or_else(|| panic!("Could not find lang, from: {}", line)) + .unwrap_or_else(|| panic!("Could not find lang, from provided string: {}", line)) .replace('#', ""); let component = lang_and_component .next() - .unwrap_or_else(|| panic!("Could not find component, from {}", line)) + .unwrap_or_else(|| panic!("Could not find component, from provided string {}", line)) .replace('#', ""); let mut component_name_sub_component_version = tail.next().unwrap_or_default().split(':'); @@ -112,10 +136,10 @@ pub fn parse_weidu_log(weidu_log_path: PathBuf) -> Vec { #[cfg(test)] mod tests { - + use super::*; + use pretty_assertions::assert_eq; use std::path::Path; - use super::*; #[test] fn test_parse_weidu_log() { let test_log = Path::new("fixtures/test.log"); @@ -172,4 +196,20 @@ mod tests { ] ); } + + #[test] + fn test_parse_windows() { + let mod_string = r"~TOBEX\TOBEX.TP2~ #0 #100 // TobEx - Core: v28"; + let mod_component = ModComponent::from(mod_string.to_string()); + let expected = ModComponent { + tp_file: "TOBEX.TP2".to_string(), + name: "tobex".to_string(), + lang: "0".to_string(), + component: "100".to_string(), + component_name: "TobEx - Core".to_string(), + sub_component: "".to_string(), + version: "v28".to_string(), + }; + assert_eq!(mod_component, expected) + } } diff --git a/src/utils.rs b/src/utils.rs index 28ab6d2..10859a5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -79,8 +79,9 @@ pub fn sleep(millis: u64) { #[cfg(test)] mod tests { - use super::*; + use pretty_assertions::assert_eq; + #[test] fn finds_mod_folder() { let mod_component = ModComponent { diff --git a/src/weidu_parser.rs b/src/weidu_parser.rs index 7a848e8..e65f00b 100644 --- a/src/weidu_parser.rs +++ b/src/weidu_parser.rs @@ -163,6 +163,8 @@ fn detect_weidu_finished_state(weidu_output: &str) -> Option { #[cfg(test)] mod tests { use super::*; + use pretty_assertions::assert_eq; + #[test] fn test_exit_warnings() { let test = "INSTALLED WITH WARNINGS Additional equipment for Thieves and Bards";