Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parsing): Fix parsing error for windows closes #70 #72

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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]
Expand Down
94 changes: 62 additions & 32 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
76 changes: 58 additions & 18 deletions src/mod_component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
fs::File,
io::{BufRead, BufReader},
path::{PathBuf, MAIN_SEPARATOR},
path::PathBuf,
};

#[derive(Debug, PartialEq, PartialOrd, Clone)]
Expand All @@ -21,36 +21,60 @@ impl From<String> 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(':');
Expand Down Expand Up @@ -112,10 +136,10 @@ pub fn parse_weidu_log(weidu_log_path: PathBuf) -> Vec<ModComponent> {

#[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");
Expand Down Expand Up @@ -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)
}
}
3 changes: 2 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions src/weidu_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ fn detect_weidu_finished_state(weidu_output: &str) -> Option<State> {
#[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";
Expand Down
Loading