-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bukowa <gitbukowa@gmail.com> Signed-off-by: Bukowa <gitbukowa@gmail.com>
- Loading branch information
Showing
9 changed files
with
155 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,4 @@ repos: | |
rev: eeee35a89e69d5772bdee97db1a6a898467b686e | ||
hooks: | ||
- id: fmt | ||
- id: clippy | ||
- id: cargo-check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
8
ck3oop-core-rs/tests/fixtures/windows/game_data/mod/_mod1.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -- |