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
use std::marker::PhantomData;
use serde::ser::{Serialize, Serializer};
use serde::de::{Deserialize, Deserializer};
use std::cmp::PartialEq;
use std::hash::{Hash, Hasher};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Exactly<T> {
Empty,
Exactly(T),
Conflict,
}
impl<T> Exactly<T> where T: PartialEq {
pub fn and(self, other: Self) -> Self {
use self::Exactly::*;
match (self, other) {
(Conflict, _) | (_, Conflict) => Conflict,
(Empty, x@_) | (x@_, Empty) => x,
(Exactly(x), Exactly(y)) =>
if x == y {
Exactly(y)
} else {
Conflict
}
}
}
pub fn is_empty(&self) -> bool {
match *self {
Exactly::Empty => true,
_ => false,
}
}
pub fn matches(&self, value: &T) -> bool {
match *self {
Exactly::Exactly(ref id) => id == value,
Exactly::Empty => true,
_ => false
}
}
}
impl<T> Default for Exactly<T> {
fn default() -> Self {
Exactly::Empty
}
}
#[derive(Clone, Debug, Default, PartialEq, Hash, Eq)]
pub struct Phantom<T> {
phantom: PhantomData<T>
}
impl<T> Phantom<T> {
pub fn new() -> Self {
Phantom {
phantom: PhantomData
}
}
}
impl<T> Serialize for Phantom<T> {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer {
serializer.visit_unit()
}
}
impl<T> Deserialize for Phantom<T> {
fn deserialize<D>(_: &mut D) -> Result<Self, D::Error>
where D: Deserializer {
Ok(Phantom {
phantom: PhantomData
})
}
}
#[derive(Debug, Clone)]
pub struct Id<T> {
id: String,
phantom: Phantom<T>
}
impl<T> Id<T> {
pub fn new(id: String) -> Self {
Id {
id: id,
phantom: Phantom::new()
}
}
pub fn as_string(&self) -> &String {
&self.id
}
}
impl<T> PartialEq for Id<T> {
fn eq(&self, other: &Self) -> bool {
self.id.eq(&other.id)
}
}
impl<T> Eq for Id<T> {
}
impl<T> Hash for Id<T> {
fn hash<H>(&self, state: &mut H) where H: Hasher {
self.id.hash(state)
}
}
impl<T> Serialize for Id<T> {
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
where S: Serializer {
serializer.visit_str(&self.id)
}
}
impl<T> Deserialize for Id<T> {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: Deserializer {
Ok(Id {
id: try!(String::deserialize(deserializer)),
phantom: Phantom::new()
})
}
}