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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//! Type-safe ID type for each resource.

use std::cmp::Ordering;
use std::fmt::{self, Write};
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::num::{NonZeroU64, ParseIntError, TryFromIntError};
use std::str::FromStr;

/// ID with a game marker.
pub type GameId = Id<marker::GameMarker>;
/// ID with a mod marker.
pub type ModId = Id<marker::ModMarker>;
/// ID with a file marker.
pub type FileId = Id<marker::FileMarker>;
/// ID with an event marker.
pub type EventId = Id<marker::EventMarker>;
/// ID with a comment marker.
pub type CommentId = Id<marker::CommentMarker>;
/// ID with a user marker.
pub type UserId = Id<marker::UserMarker>;
/// ID with a team member marker.
pub type MemberId = Id<marker::MemberMarker>;
/// ID with a resource marker.
pub type ResourceId = Id<marker::ResourceMarker>;

/// Markers for various resource types.
pub mod marker {
    /// Marker for game IDs.
    #[non_exhaustive]
    pub struct GameMarker;

    /// Marker for mod IDs.
    #[non_exhaustive]
    pub struct ModMarker;

    /// Marker for file IDs.
    #[non_exhaustive]
    pub struct FileMarker;

    /// Marker for event IDs.
    #[non_exhaustive]
    pub struct EventMarker;

    /// Marker for comment IDs.
    #[non_exhaustive]
    pub struct CommentMarker;

    /// Marker for user IDs.
    #[non_exhaustive]
    pub struct UserMarker;

    /// Marker for team member IDs.
    #[non_exhaustive]
    pub struct MemberMarker;

    /// Marker for resource IDs.
    #[non_exhaustive]
    pub struct ResourceMarker;
}

/// ID of a resource, such as the ID of a [game] or [mod].
///
/// [game]: crate::types::games::Game
/// [mod]: crate::types::mods::Mod
#[repr(transparent)]
pub struct Id<T> {
    phantom: PhantomData<fn(T) -> T>,
    value: NonZeroU64,
}

impl<T> Id<T> {
    const fn from_nonzero(value: NonZeroU64) -> Self {
        Self {
            phantom: PhantomData,
            value,
        }
    }

    /// Create a new ID.
    ///
    /// # Examples
    ///
    /// ```
    /// use modio::types::id::marker::GameMarker;
    /// use modio::types::id::Id;
    ///
    /// let id: Id<GameMarker> = Id::new(123);
    ///
    /// // Using the provided type aliases.
    /// use modio::types::id::GameId;
    ///
    /// let game_id = GameId::new(123);
    ///
    /// assert_eq!(id, game_id);
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the value is 0.
    #[track_caller]
    pub const fn new(value: u64) -> Self {
        if let Some(value) = Self::new_checked(value) {
            value
        } else {
            panic!("value is zero")
        }
    }

    /// Create a new ID if the given value is not zero.
    pub const fn new_checked(value: u64) -> Option<Self> {
        if let Some(value) = NonZeroU64::new(value) {
            Some(Self::from_nonzero(value))
        } else {
            None
        }
    }

    pub const fn get(self) -> u64 {
        self.value.get()
    }

    pub const fn transform<New>(self) -> Id<New> {
        Id::new(self.get())
    }
}

impl<T> Clone for Id<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for Id<T> {}

impl<T> fmt::Debug for Id<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Id")?;

        let name = std::any::type_name::<T>();
        if let Some(pos) = name.rfind("::") {
            if let Some(marker) = name.get(pos + 2..) {
                f.write_char('<')?;
                f.write_str(marker)?;
                f.write_char('>')?;
            }
        }

        f.write_char('(')?;
        fmt::Debug::fmt(&self.value, f)?;
        f.write_char(')')
    }
}

impl<T> fmt::Display for Id<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.value, f)
    }
}

impl<T> PartialEq for Id<T> {
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl<T> Eq for Id<T> {}

impl<T> PartialEq<u64> for Id<T> {
    fn eq(&self, other: &u64) -> bool {
        self.value.get() == *other
    }
}

impl<T> PartialEq<Id<T>> for u64 {
    fn eq(&self, other: &Id<T>) -> bool {
        other.value.get() == *self
    }
}

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

impl<T> Ord for Id<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.value.cmp(&other.value)
    }
}

impl<T> Hash for Id<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u64(self.value.get());
    }
}

impl<T> From<Id<T>> for u64 {
    fn from(value: Id<T>) -> Self {
        value.get()
    }
}

impl<T> From<NonZeroU64> for Id<T> {
    fn from(value: NonZeroU64) -> Self {
        Self::from_nonzero(value)
    }
}

impl<T> TryFrom<u64> for Id<T> {
    type Error = TryFromIntError;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        let value = NonZeroU64::try_from(value)?;
        Ok(Id::from_nonzero(value))
    }
}

impl<T> FromStr for Id<T> {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        NonZeroU64::from_str(s).map(Self::from_nonzero)
    }
}

use serde::{Deserialize, Deserializer, Serialize, Serializer};

impl<'de, T> Deserialize<'de> for Id<T> {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        NonZeroU64::deserialize(deserializer).map(Self::from_nonzero)
    }
}

impl<T> Serialize for Id<T> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        self.value.serialize(serializer)
    }
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::marker::*;
    use super::Id;

    #[test]
    fn from_str() {
        assert_eq!(
            Id::<GameMarker>::new(123),
            Id::<GameMarker>::from_str("123").unwrap()
        );
        assert!(Id::<GameMarker>::from_str("0").is_err());
        assert!(Id::<GameMarker>::from_str("123a").is_err());
    }
}