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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::{
    cmp::Ordering,
    fmt::{Debug, Display, Formatter, Result as FmtResult},
    str::FromStr,
};

use crate::error::AcronymParseError;

/// The acronym of a [`GameMod`].
///
/// [`GameMod`]: crate::generated_mods::GameMod
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct Acronym([u8; 3]);

impl Acronym {
    /// Create an [`Acronym`] from a string.
    ///
    /// # Safety
    ///
    /// The given string must consist of two or three bytes representing capitalized ASCII letters or digits.
    ///
    /// # Example
    /// ```rust
    /// use rosu_mods::Acronym;
    ///
    /// let hd = unsafe { Acronym::from_str_unchecked("HD") };
    /// assert_eq!(hd.as_str(), "HD");
    /// ```
    ///
    /// Each of the following may lead to undefined behavior, don't do that!
    /// ```rust,no_run
    /// # use rosu_mods::Acronym;
    /// let _ = unsafe { Acronym::from_str_unchecked("HDHR") }; // must be 2 or 3 characters
    /// let _ = unsafe { Acronym::from_str_unchecked("hd") };   // must be uppercase
    /// ```
    pub const unsafe fn from_str_unchecked(s: &str) -> Self {
        let array = if s.len() == 2 {
            // SAFETY: `s` is guaranteed to be of length 2
            let [a, b] = unsafe { *(s.as_ptr().cast::<[u8; 2]>()) };

            [0, a, b]
        } else {
            // SAFETY: caller guarantees that `s` is of length 3
            unsafe { *s.as_ptr().cast::<[u8; 3]>() }
        };

        Self(array)
    }

    /// Returns the [`Acronym`] as a string.
    ///
    /// # Example
    /// ```rust
    /// use rosu_mods::Acronym;
    ///
    /// let hd = "HD".parse::<Acronym>().unwrap();
    /// assert_eq!(hd.as_str(), "HD");
    /// ```
    pub fn as_str(&self) -> &str {
        let start_idx = usize::from(self.0[0] == 0);

        // SAFETY: `self.0` is known to be constructed from a valid string
        unsafe { std::str::from_utf8_unchecked(&self.0[start_idx..]) }
    }
}

impl Debug for Acronym {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        Debug::fmt(self.as_str(), f)
    }
}

impl Display for Acronym {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str(self.as_str())
    }
}

impl FromStr for Acronym {
    type Err = AcronymParseError;

    /// Create an [`Acronym`] from a string.
    ///
    /// Errors if the acronym consists of fewer than 2 or more than 3 bytes.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match <[u8; 2]>::try_from(s.as_bytes()) {
            Ok([a, b]) => Ok(Self([0, a.to_ascii_uppercase(), b.to_ascii_uppercase()])),
            Err(_) => s
                .as_bytes()
                .try_into()
                .map(|mut array: [u8; 3]| {
                    array.make_ascii_uppercase();

                    Self(array)
                })
                .map_err(|_| AcronymParseError {
                    acronym: Box::from(s),
                }),
        }
    }
}

impl PartialOrd for Acronym {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Acronym {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
const _: () = {
    use serde::{
        de::{Deserialize, Deserializer, Error as DeError, Visitor},
        ser::{Serialize, Serializer},
    };

    impl<'de> Deserialize<'de> for Acronym {
        fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
            struct AcronymVisitor;

            impl<'de> Visitor<'de> for AcronymVisitor {
                type Value = Acronym;

                fn expecting(&self, f: &mut Formatter<'_>) -> FmtResult {
                    f.write_str("string")
                }

                fn visit_str<E: DeError>(self, v: &str) -> Result<Self::Value, E> {
                    v.parse().map_err(DeError::custom)
                }

                fn visit_string<E: DeError>(self, v: String) -> Result<Self::Value, E> {
                    self.visit_str(&v)
                }
            }

            d.deserialize_str(AcronymVisitor)
        }
    }

    impl Serialize for Acronym {
        fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
            s.serialize_str(self.as_str())
        }
    }
};

#[cfg(feature = "rkyv")]
#[cfg_attr(docsrs, doc(cfg(feature = "rkyv")))]
const _: () = {
    use std::ptr::addr_of;

    use rkyv::{
        bytecheck::{ErrorBox, TupleStructCheckError},
        Archive, CheckBytes, Deserialize, Fallible, Serialize,
    };

    impl Archive for Acronym {
        type Archived = Self;
        type Resolver = ();

        unsafe fn resolve(&self, pos: usize, (): Self::Resolver, out: *mut Self) {
            self.0.resolve(pos, [(); 3], out.cast());
        }
    }

    impl<S: Fallible + ?Sized> Serialize<S> for Acronym {
        fn serialize(&self, s: &mut S) -> Result<(), S::Error> {
            self.0.serialize(s).map(|_| ())
        }
    }

    impl<D: Fallible + ?Sized> Deserialize<Self, D> for Acronym {
        fn deserialize(&self, _: &mut D) -> Result<Self, D::Error> {
            Ok(*self)
        }
    }

    impl<C: ?Sized> CheckBytes<C> for Acronym {
        type Error = TupleStructCheckError;

        unsafe fn check_bytes<'a>(
            value: *const Self,
            ctx: &mut C,
        ) -> Result<&'a Self, TupleStructCheckError> {
            <[u8; 3] as CheckBytes<C>>::check_bytes(addr_of!((*value).0), ctx).map_err(|e| {
                TupleStructCheckError {
                    field_index: 0,
                    inner: ErrorBox::new(e),
                }
            })?;

            Ok(&*value)
        }
    }
};