Skip to content

Commit

Permalink
feat(improvements): Add log file struct (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
dark0dave authored Jul 29, 2024
2 parents 7a5da96 + 1754e63 commit d68f2c6
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 164 deletions.
56 changes: 28 additions & 28 deletions Cargo.lock

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

15 changes: 11 additions & 4 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Please provide a valid weidu logging setting, options are:
#[clap(author, version, about, long_about = None)]
pub struct Args {
/// Full path to target log
#[clap(env, long, short = 'f', required = true)]
#[clap(env, long, short = 'f', value_parser = path_must_exist, required = true)]
pub log_file: PathBuf,

/// Full path to game directory
Expand Down Expand Up @@ -84,14 +84,21 @@ fn parse_weidu_log_mode(arg: &str) -> Result<String, String> {
Ok(output.join(" "))
}

fn path_must_exist(arg: &str) -> Result<PathBuf, std::io::Error> {
let path = PathBuf::from(arg);
path.try_exists()?;
Ok(path)
}

fn parse_absolute_path(arg: &str) -> Result<PathBuf, String> {
let path = Path::new(arg);
let path = path_must_exist(arg).map_err(|err| err.to_string())?;
if path.is_absolute() {
Ok(path.to_path_buf())
Ok(path)
} else {
Err("Please provide the absolute path".to_string())
Err("Please provide an absolute path".to_string())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
100 changes: 8 additions & 92 deletions src/component.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
use std::{
fs::File,
io::{BufRead, BufReader},
path::PathBuf,
};

// This should mirror the weidu component
// https://github.com/WeiDUorg/weidu/blob/devel/src/tp.ml#L98
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub struct Component {
pub tp_file: String,
pub name: String,
pub lang: String,
pub component: String,
pub component_name: String,
pub sub_component: String,
pub version: String,
pub(crate) struct Component {
pub(crate) tp_file: String,
pub(crate) name: String,
pub(crate) lang: String,
pub(crate) component: String,
pub(crate) component_name: String,
pub(crate) sub_component: String,
pub(crate) version: String,
}

impl From<String> for Component {
Expand Down Expand Up @@ -116,88 +110,10 @@ impl From<String> for Component {
}
}

pub fn parse_weidu_log(weidu_log_path: PathBuf) -> Vec<Component> {
let file = File::open(weidu_log_path).expect("Could not open weidu log exiting");
let reader = BufReader::new(file);

reader
.lines()
.flat_map(|line| match line {
// Ignore comments and empty lines
Ok(component)
if !component.is_empty()
&& !component.starts_with('\n')
&& !component.starts_with("//") =>
{
Some(Component::from(component))
}
_ => None,
})
.collect()
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use std::path::Path;

#[test]
fn test_parse_weidu_log() {
let test_log = Path::new("fixtures/test.log");
let logs = parse_weidu_log(test_log.to_path_buf());
assert_eq!(
logs,
vec![
Component {
tp_file: "TEST.TP2".to_string(),
name: "test_mod_name_1".to_string(),
lang: "0".to_string(),
component: "0".to_string(),
component_name: "test mod one".to_string(),
sub_component: "".to_string(),
version: "".to_string()
},
Component {
tp_file: "TEST.TP2".to_string(),
name: "test_mod_name_1".to_string(),
lang: "0".to_string(),
component: "1".to_string(),
component_name: "test mod two".to_string(),
sub_component: "".to_string(),
version: "".to_string()
},
Component {
tp_file: "END.TP2".to_string(),
name: "test_mod_name_2".to_string(),
lang: "0".to_string(),
component: "0".to_string(),
component_name: "test mod with subcomponent information".to_string(),
sub_component: "Standard installation".to_string(),
version: "".to_string()
},
Component {
tp_file: "END.TP2".to_string(),
name: "test_mod_name_3".to_string(),
lang: "0".to_string(),
component: "0".to_string(),
component_name: "test mod with version".to_string(),
sub_component: "".to_string(),
version: "1.02".to_string()
},
Component {
tp_file: "TWEAKS.TP2".to_string(),
name: "test_mod_name_4".to_string(),
lang: "0".to_string(),
component: "3346".to_string(),
component_name: "test mod with both subcomponent information and version"
.to_string(),
sub_component: "Casting speed only".to_string(),
version: "v16".to_string()
}
]
);
}

#[test]
fn test_parse_windows() {
Expand Down
Loading

0 comments on commit d68f2c6

Please sign in to comment.