summaryrefslogtreecommitdiffstats
path: root/src/types/util.rs
blob: 0f95e8c3a4c23f7c51e5133c8905eba70d3c3dfb (plain)
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
use std::ops::Deref;

use chrono::NaiveDateTime;
use mime::Mime;

/// A simple version.
///
/// Like "1" for example
#[derive(Serialize, Deserialize, Debug)]
pub struct Version(usize);

impl From<usize> for Version {
    fn from(u: usize) -> Self {
        Version(u)
    }
}

/// A Timestamp which can be parsed into a NaiveDateTime object
/// Format: 2014-11-28T21:45:59.324310806+09:00
/// (RFC3999)
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Timestamp(NaiveDateTime);

impl From<NaiveDateTime> for Timestamp {
    fn from(ndt: NaiveDateTime) -> Self {
        Timestamp(ndt)
    }
}

impl ::std::fmt::Display for Timestamp {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        self.0.fmt(f)
    }
}

impl Timestamp {
    pub fn now() -> Self {
        Timestamp::from(chrono::offset::Local::now().naive_local())
    }
}

#[derive(Serialize, Deserialize, Debug, Hash, PartialOrd, PartialEq, Ord, Eq, Clone)]
pub struct IPFSHash(String);

impl AsRef<IPFSHash> for IPFSHash {
    fn as_ref(&self) -> &Self {
        &self
    }
}

impl From<String> for IPFSHash {
    fn from(s: String) -> Self {
        IPFSHash(s)
    }
}

impl<'a> From<&'a str> for IPFSHash {
    fn from(s: &str) -> Self {
        String::from(s).into()
    }
}

impl Deref for IPFSHash {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl ::std::fmt::Display for IPFSHash {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        self.0.fmt(f)
    }
}

#[derive(Serialize, Deserialize, Debug, Hash, PartialOrd, PartialEq, Ord, Eq, Clone)]
pub struct IPNSHash(String);

impl From<String> for IPNSHash {
    fn from(s: String) -> Self {
        IPNSHash(s)
    }
}

impl<'a> From<&'a str> for IPNSHash {
    fn from(s: &str) -> Self {
        String::from(s).into()
    }
}

impl Deref for IPNSHash {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl ::std::fmt::Display for IPNSHash {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        self.0.fmt(f)
    }
}

/// TODO: A String as mimetype... we can do this better!
#[derive(Debug, Hash, PartialEq, Eq)]
pub struct MimeType(Mime);

impl From<Mime> for MimeType {
    fn from(m: Mime) -> Self {
        MimeType(m)
    }
}

impl Deref for MimeType {
    type Target = Mime;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl ::std::fmt::Display for MimeType {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        self.0.fmt(f)
    }
}


//
// TODO: Remove code below as soon as "mime" has serde support
//

use std::fmt;
use std::str::FromStr;
use serde::de::{self, Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};

impl Serialize for MimeType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where S: Serializer,
    {
        serializer.serialize_str(self.0.as_ref())
    }
}

impl<'de> Deserialize<'de> for MimeType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
        where D: Deserializer<'de>,
    {
        struct Visitor;

        impl<'de> de::Visitor<'de> for Visitor {
            type Value = MimeType;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a valid MIME type")
            }

            fn visit_str<E>(self, value: &str) -> Result<MimeType, E>
                where E: de::Error,
            {
                Mime::from_str(value)
                    .map(MimeType)
                    .map_err(E::custom)
            }
        }
        deserializer.deserialize_str(Visitor)
    }
}