Skip to content

Commit

Permalink
feat(rust-core): mod file parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Bukowa <gitbukowa@gmail.com>

Signed-off-by: Bukowa <gitbukowa@gmail.com>
  • Loading branch information
bukowa committed Jun 17, 2024
1 parent bd3c731 commit 03ce8e1
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 6 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ repos:
rev: eeee35a89e69d5772bdee97db1a6a898467b686e
hooks:
- id: fmt
- id: clippy
- id: cargo-check
2 changes: 1 addition & 1 deletion ck3oop-core-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ repository = "https://github.com/bukowa/ck3oop"
keywords = ["ck3oop"]
license = "MIT"
homepage = "https://github.com/bukowa/ck3oop"

description = "ck3oop"
[dependencies]
5 changes: 1 addition & 4 deletions ck3oop-core-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
// This function adds two numbers.!
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub mod mods;
67 changes: 67 additions & 0 deletions ck3oop-core-rs/src/mods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::path::PathBuf;

pub fn mod_from_file_content(content: &str) -> std::io::Result<Mod> {
let mut name = String::from("");
let mut path = String::from("");
let mut version = String::from("");
let mut supported_version = String::from("");
let mut remote_file_id = String::from("");

for line in content.lines() {
let mut parts = line.split('=');
let key = parts.next().unwrap_or("");
let value = parts.next().unwrap_or("").trim_matches('"');

match key {
"name" => name = String::from(value),
"path" => path = String::from(value),
"version" => version = String::from(value),
"supported_version" => supported_version = String::from(value),
"remote_file_id" => remote_file_id = String::from(value),
_ => (),
}
}

Ok(Mod::new(
name,
path,
version,
supported_version,
remote_file_id,
))
}

pub struct Mod {
pub name: String,
pub path: String,
pub version: String,
pub supported_version: String,
pub remote_file_id: String,
}

impl Mod {
pub fn new_from_path(path: &PathBuf) -> std::io::Result<Mod> {
let content = std::fs::read_to_string(path)?;
Mod::new_from_file_content(&content)
}

fn new_from_file_content(content: &str) -> std::io::Result<Mod> {
mod_from_file_content(content)
}

fn new(
name: String,
path: String,
version: String,
supported_version: String,
remote_file_id: String,
) -> Mod {
Mod {
name,
path,
version,
supported_version,
remote_file_id,
}
}
}
8 changes: 8 additions & 0 deletions ck3oop-core-rs/tests/fixtures/windows/game_data/mod/_mod1.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version="1.0.0"
tags={
"Alternative History"
}
name="My Awesome Mod 1"
supported_version="1.12.5"
path="C:/Users/User1/Documents/Paradox Interactive/Crusader Kings III/mod/test_mod"
remote_file_id="123456789"
7 changes: 7 additions & 0 deletions ck3oop-core-rs/tests/fixtures/windows/game_data/mod/mod1.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version="1.0.0"
tags={
"Alternative History"
}
name="My Awesome Mod 1"
supported_version="1.12.5"
path="C:/Users/buk/Documents/Paradox Interactive/Crusader Kings III/mod/testtesttest"
7 changes: 7 additions & 0 deletions ck3oop-core-rs/tests/fixtures/windows/game_data/mod/mod2.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version="1.1.0"
tags={
"Alternative History"
}
name="My Awesome Mod 2"
supported_version="1.12.5"
path="C:/Users/buk/Documents/Paradox Interactive/Crusader Kings III/mod/testtesttest"
55 changes: 55 additions & 0 deletions ck3oop-core-rs/tests/mods_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use ck3oop_core_rs::mods::mod_from_file_content;
use std::env::{current_dir, current_exe};
use std::path::{Path, PathBuf};
use std::thread::current;

pub fn get_tests_absolute_path() -> PathBuf {
// Get the current directory
let dir1 = current_dir().unwrap();
let dir1 = dir1.parent().unwrap();
// Get the file path relative to the current file
let dir2 = Path::new(file!());
// Get the parent directory of the file path
let dir3 = dir2.parent().unwrap();
// Join the current directory with the parent directory of the file path
let dir4 = dir1.join(dir3);
dir4
}

const CONTENT: &str = r#"
version="1.0.0"
tags={
"Alternative History"
}
name="My Awesome Mod 1"
supported_version="1.12.5"
path="C:/Users/User1/Documents/Paradox Interactive/Crusader Kings III/mod/test_mod"
remote_file_id="123456789"
"#;

pub fn assert_mod_equal_to_content(mod_: &ck3oop_core_rs::mods::Mod) {
assert_eq!(mod_.name, "My Awesome Mod 1");
assert_eq!(
mod_.path,
"C:/Users/User1/Documents/Paradox Interactive/Crusader Kings III/mod/test_mod"
);
assert_eq!(mod_.version, "1.0.0");
assert_eq!(mod_.supported_version, "1.12.5");
assert_eq!(mod_.remote_file_id, "123456789");
}

#[test]
pub fn test_parse_mod_file_content() {
let content = String::from(CONTENT);
let mod_ = mod_from_file_content(content.as_str()).unwrap();
assert_mod_equal_to_content(&mod_);
}

#[test]
pub fn test_mod_new_from_path() {
let fixture_path = "fixtures\\windows\\game_data\\mod\\_mod1.mod";
let fixture_absolute_path = get_tests_absolute_path().join(fixture_path);
println!("{:?}", fixture_absolute_path);
let mod_ = ck3oop_core_rs::mods::Mod::new_from_path(&fixture_absolute_path).unwrap();
assert_mod_equal_to_content(&mod_);
}
9 changes: 9 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

clippy:
cargo clippy -- -D warnings

clippy-fix:
cargo clippy --fix -- -D warnings

fmt:
cargo fmt --

0 comments on commit 03ce8e1

Please sign in to comment.