Skip to main content

steamkit/types/
pub_file.rs

1//! Typed `u64` handles, like [`PublishedFileID`].
2
3use std::fmt;
4use std::hash::Hash;
5
6/// A `u64` wrapped in its own type, so different kinds of ids can't be mixed
7/// up.
8pub trait UInt64Handle: Copy + PartialEq + Eq + Hash {
9    /// The wrapped value.
10    fn value(&self) -> u64;
11}
12
13/// A handle to a file published on the Steam workshop.
14#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
15pub struct PublishedFileID(u64);
16
17impl PublishedFileID {
18    /// Creates an invalid handle (`u64::MAX`).
19    pub fn new() -> PublishedFileID {
20        PublishedFileID(u64::MAX)
21    }
22
23    /// Creates a handle from its value.
24    pub fn from_value(file_id: u64) -> PublishedFileID {
25        PublishedFileID(file_id)
26    }
27
28    /// The wrapped value.
29    pub fn value(&self) -> u64 {
30        self.0
31    }
32}
33
34impl UInt64Handle for PublishedFileID {
35    fn value(&self) -> u64 {
36        self.0
37    }
38}
39
40impl fmt::Display for PublishedFileID {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        write!(f, "{}", self.0)
43    }
44}
45
46impl From<PublishedFileID> for u64 {
47    fn from(file: PublishedFileID) -> u64 {
48        file.0
49    }
50}
51
52impl From<u64> for PublishedFileID {
53    fn from(file_id: u64) -> PublishedFileID {
54        PublishedFileID(file_id)
55    }
56}