1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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,
        }
    }
}