summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 05d74dbd111afa5a6a3c82c608fbbd377f1fb7ae (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
pub mod apis;
pub mod cache;
pub mod commands;
pub mod config;
pub mod db;
pub mod error;
pub mod files;

use std::fmt::Display;

pub use apis::*;
pub use commands::*;
use error::{ErrorType, MLError, MLE};
use serde::{Deserialize, Serialize, de::Visitor};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Modloader {
    Fabric,
    Forge,
}

impl Modloader {
    pub fn from(string: &str) -> MLE<Modloader> {
        match string {
            "forge" => Ok(Modloader::Forge),
            "fabric" => Ok(Modloader::Fabric),
            _ => Err(MLError::new(ErrorType::ArgumentError, "UNKNOWN_MODLOADER")),
        }
    }
}

impl Display for Modloader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Modloader::Fabric => write!(f, "fabric"),
            Modloader::Forge => write!(f, "forge"),
        }
    }
}

impl Serialize for Modloader {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer {
        match self {
            Modloader::Fabric => serializer.serialize_str("fabric"),
            Modloader::Forge => serializer.serialize_str("forge"),
        }
    }
}

impl<'de> Deserialize<'de> for Modloader {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
    D: serde::Deserializer<'de>,
    {
        struct FieldVisitor;

        impl<'de> Visitor<'de> for FieldVisitor {
            type Value = Modloader;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("`fabric`, `forge` or `quilt`")
            }

            fn visit_str<E>(self, v: &str) -> Result<Modloader, E>
                where
                    E: serde::de::Error, {
                match v {
                    "fabric" => Ok(Modloader::Fabric),
                    "forge" => Ok(Modloader::Forge),
                    _ => Err(serde::de::Error::unknown_field(v, &["fabric", "forge", "quilt"]))
                }
            }
        }

        deserializer.deserialize_identifier(FieldVisitor)
    }
}