Skip to main content

elemental_driver/
descriptors.rs

1use std::str::FromStr;
2
3use crate::error::{Error, Result};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub struct DriverDescriptor {
7    pub id: &'static str,
8    pub name: &'static str,
9}
10
11pub const VANILLA_DRIVER: DriverDescriptor = DriverDescriptor {
12    id: "vanilla",
13    name: "Vanilla",
14};
15
16pub const FABRIC_DRIVER: DriverDescriptor = DriverDescriptor {
17    id: "fabric",
18    name: "Fabric",
19};
20
21pub const LEGACY_FABRIC_DRIVER: DriverDescriptor = DriverDescriptor {
22    id: "legacyfabric",
23    name: "LegacyFabric",
24};
25
26pub const BABRIC_DRIVER: DriverDescriptor = DriverDescriptor {
27    id: "babric",
28    name: "Babric",
29};
30
31pub const QUILT_DRIVER: DriverDescriptor = DriverDescriptor {
32    id: "quilt",
33    name: "Quilt",
34};
35
36pub const RIFT_DRIVER: DriverDescriptor = DriverDescriptor {
37    id: "rift",
38    name: "Rift",
39};
40
41pub const FORGE_DRIVER: DriverDescriptor = DriverDescriptor {
42    id: "forge",
43    name: "Forge",
44};
45
46pub const CLEANROOM_DRIVER: DriverDescriptor = DriverDescriptor {
47    id: "cleanroom",
48    name: "Cleanroom",
49};
50
51pub const OUTLANDS_DRIVER: DriverDescriptor = DriverDescriptor {
52    id: "outlands",
53    name: "Outlands",
54};
55
56pub const NEOFORGE_DRIVER: DriverDescriptor = DriverDescriptor {
57    id: "neoforge",
58    name: "NeoForge",
59};
60
61/// All supported drivers in instance-detection order.
62///
63/// More specific drivers come before `Vanilla`, so a modded instance is not
64/// claimed by the vanilla fallback first.
65pub const ALL_DRIVERS: &[DriverDescriptor] = &[
66    #[cfg(feature = "fabric")]
67    FABRIC_DRIVER,
68    #[cfg(feature = "legacyfabric")]
69    LEGACY_FABRIC_DRIVER,
70    #[cfg(feature = "babric")]
71    BABRIC_DRIVER,
72    #[cfg(feature = "quilt")]
73    QUILT_DRIVER,
74    #[cfg(feature = "rift")]
75    RIFT_DRIVER,
76    #[cfg(feature = "forge")]
77    FORGE_DRIVER,
78    #[cfg(feature = "cleanroom")]
79    CLEANROOM_DRIVER,
80    #[cfg(feature = "outlands")]
81    OUTLANDS_DRIVER,
82    #[cfg(feature = "neoforge")]
83    NEOFORGE_DRIVER,
84    VANILLA_DRIVER,
85];
86
87/// Resolves a known driver descriptor by its stable id.
88pub fn from_id(id: &str) -> Option<DriverDescriptor> {
89    ALL_DRIVERS
90        .iter()
91        .copied()
92        .find(|descriptor| descriptor.id == id)
93}
94
95impl TryFrom<&str> for DriverDescriptor {
96    type Error = Error;
97
98    fn try_from(id: &str) -> Result<Self> {
99        from_id(id).ok_or_else(|| Error::UnsupportedDriver { id: id.to_owned() })
100    }
101}
102
103impl TryFrom<String> for DriverDescriptor {
104    type Error = Error;
105
106    fn try_from(id: String) -> Result<Self> {
107        Self::try_from(id.as_str())
108    }
109}
110
111impl FromStr for DriverDescriptor {
112    type Err = Error;
113
114    fn from_str(id: &str) -> Result<Self> {
115        Self::try_from(id)
116    }
117}