lotus_lib/package/
package_type.rs

1/// Represents the type of a package trio
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub enum PackageType {
4    /// Header package
5    ///
6    /// Usually contains file headers and metadata
7    H,
8
9    /// File package
10    ///
11    /// Usually contains compressed sound and texture assets
12    F,
13
14    /// Binary package
15    ///
16    /// Contains compiled binary data
17    B,
18}
19
20impl TryFrom<String> for PackageType {
21    type Error = &'static str;
22
23    /// Converts from a [`String`].
24    /// Calls [`Self::try_from(&str)`] on the string.
25    ///
26    /// # Errors
27    ///
28    /// Returns an error if the string is not exactly one character long
29    fn try_from(s: String) -> Result<Self, Self::Error> {
30        Self::try_from(s.as_str())
31    }
32}
33
34impl TryFrom<&str> for PackageType {
35    type Error = &'static str;
36
37    /// Converts from a [`str`] reference.
38    /// Calls [`Self::try_from(char)`] on the first character of the string.
39    ///
40    /// # Errors
41    ///
42    /// Returns an error if the string is not exactly one character long
43    fn try_from(s: &str) -> Result<Self, Self::Error> {
44        if s.len() != 1 {
45            return Err("Invalid package trio type");
46        }
47        let c = s.chars().next().unwrap();
48        Self::try_from(c)
49    }
50}
51
52impl TryFrom<char> for PackageType {
53    type Error = &'static str;
54
55    /// Converts from a [`char`]
56    ///
57    /// # Errors
58    ///
59    /// Returns an error if the character is not 'H', 'F', or 'B'
60    fn try_from(c: char) -> Result<Self, Self::Error> {
61        match c {
62            'H' | 'h' => Ok(Self::H),
63            'F' | 'f' => Ok(Self::F),
64            'B' | 'b' => Ok(Self::B),
65            _ => Err("Invalid package trio type"),
66        }
67    }
68}
69
70impl From<PackageType> for char {
71    /// Converts from a [`PackageType`] to a [`char`]
72    fn from(package_type: PackageType) -> Self {
73        match package_type {
74            PackageType::H => 'H',
75            PackageType::F => 'F',
76            PackageType::B => 'B',
77        }
78    }
79}