spectacles_model/
snowflake.rs1use std::fmt;
2
3use serde::{de, de::{Deserializer, Visitor}, Deserialize};
4use serde::ser::{Serialize, Serializer};
5
6#[derive(Default, Debug, Clone)]
8pub struct Snowflake(pub u64);
9
10impl Into<u64> for Snowflake {
11 fn into(self) -> u64 {
12 self.0
13 }
14}
15
16impl From<u64> for Snowflake {
17 fn from(u: u64) -> Self {
18 Snowflake(u)
19 }
20}
21
22
23impl fmt::Display for Snowflake {
24 fn fmt(&self, fmtter: &mut fmt::Formatter) -> fmt::Result {
25 write!(fmtter, "{}", self.0)
26 }
27}
28
29impl Serialize for Snowflake {
30 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
31 where S: Serializer,
32 {
33 serializer.serialize_str(&self.0.to_string().as_str())
34
35 }
36}
37
38struct SnowflakeVisitor;
39
40impl <'de> Visitor<'de> for SnowflakeVisitor {
41 type Value = Snowflake;
42
43 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
44 formatter.write_str("A snowflake as a string.")
45 }
46
47 fn visit_str<E>(self, value: &str) -> std::result::Result<Self::Value, E>
48 where
49 E: de::Error,
50 {
51 let i: u64 = value.parse().map_err(|_| de::Error::invalid_type(de::Unexpected::Str(value), &self))?;
52 Ok(Snowflake(i))
53 }
54}
55
56impl<'de> Deserialize<'de> for Snowflake {
57 fn deserialize<D>(deserializer: D) -> std::result::Result<Snowflake, D::Error>
58 where
59 D: Deserializer<'de>,
60 {
61 deserializer.deserialize_identifier(SnowflakeVisitor)
62 }
63}