z_mirror/fmm

git clone https://git.raiguard.me/z_mirror/fmm

raiguardBroken half-implementation of dependencies7007399

dependencies
1.9 KiB65 linesraw
1use semver::Version;
2use std::fmt;
3use std::str::FromStr;
4use thiserror::Error;
5
6use crate::mods_set::ModEnabledType;
7
8#[derive(Clone, Debug)]
9pub struct InputMod {
10    pub name: String,
11    pub version: ModEnabledType,
12}
13
14impl FromStr for InputMod {
15    type Err = InputModErr;
16
17    fn from_str(s: &str) -> Result<Self, Self::Err> {
18        let parts: Vec<&str> = s.split('@').collect();
19        match parts[..] {
20            [name] => Ok(Self {
21                name: name.to_string(),
22                version: ModEnabledType::Latest,
23            }),
24            [name, version] => {
25                let parsed_version = Version::parse(version);
26                if let Ok(version) = parsed_version {
27                    // Validate that the version does *not* have prerelease or build data
28                    if version.pre.len() > 0 || version.build.len() > 0 {
29                        Err(InputModErr::InvalidVersion(version.to_string()))
30                    } else {
31                        Ok(Self {
32                            name: name.to_string(),
33                            version: ModEnabledType::Version(version),
34                        })
35                    }
36                } else {
37                    Err(InputModErr::InvalidVersion(version.to_string()))
38                }
39            }
40            _ => Err(InputModErr::IncorrectArgCount(parts.len())),
41        }
42    }
43}
44
45impl fmt::Display for InputMod {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        write!(
48            f,
49            "{}{}",
50            self.name,
51            match &self.version {
52                ModEnabledType::Version(version) => format!(" v{}", version),
53                _ => "".to_string(),
54            }
55        )
56    }
57}
58
59#[derive(Debug, Error)]
60pub enum InputModErr {
61    #[error("Incorrect argument count: expected 1 or 2, got {0}")]
62    IncorrectArgCount(usize),
63    #[error("Invalid version identifier: `{0}`")]
64    InvalidVersion(String),
65}