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
use serde::{Serialize, Serializer};
use serde_json::Value;
use hex::ToHex;
use {Hash, Token};
#[derive(Debug, PartialEq, Default)]
pub struct RawTopicFilter {
pub topic0: Topic<Token>,
pub topic1: Topic<Token>,
pub topic2: Topic<Token>,
}
#[derive(Debug, PartialEq, Default)]
pub struct TopicFilter {
pub topic0: Topic<Hash>,
pub topic1: Topic<Hash>,
pub topic2: Topic<Hash>,
pub topic3: Topic<Hash>,
}
impl Serialize for TopicFilter {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
vec![&self.topic0, &self.topic1, &self.topic2, &self.topic3].serialize(serializer)
}
}
#[derive(Debug, PartialEq)]
pub enum Topic<T> {
Any,
OneOf(Vec<T>),
This(T),
}
impl<T> Topic<T> {
pub fn map<F, O>(self, f: F) -> Topic<O> where F: Fn(T) -> O {
match self {
Topic::Any => Topic::Any,
Topic::OneOf(topics) => Topic::OneOf(topics.into_iter().map(f).collect()),
Topic::This(topic) => Topic::This(f(topic)),
}
}
}
impl<T> Default for Topic<T> {
fn default() -> Self {
Topic::Any
}
}
impl<T> From<Option<T>> for Topic<T> {
fn from(o: Option<T>) -> Self {
match o {
Some(topic) => Topic::This(topic),
None => Topic::Any,
}
}
}
impl<T> From<T> for Topic<T> {
fn from(topic: T) -> Self {
Topic::This(topic)
}
}
impl<T> From<Vec<T>> for Topic<T> {
fn from(topics: Vec<T>) -> Self {
Topic::OneOf(topics)
}
}
impl<T> Into<Vec<T>> for Topic<T> {
fn into(self: Self) -> Vec<T> {
match self {
Topic::Any => vec![],
Topic::This(topic) => vec![topic],
Topic::OneOf(topics) => topics,
}
}
}
impl Serialize for Topic<Hash> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer {
let value = match *self {
Topic::Any => Value::Null,
Topic::OneOf(ref vec) => {
let v = vec.iter()
.map(|h| format!("0x{}", h.to_hex()))
.map(Value::String)
.collect();
Value::Array(v)
},
Topic::This(ref hash) => Value::String(format!("0x{}", hash.to_hex())),
};
value.serialize(serializer)
}
}
#[cfg(test)]
mod tests {
use serde_json;
use super::{Topic, TopicFilter};
use Hash;
fn hash(s: &'static str) -> Hash {
s.into()
}
#[test]
fn test_topic_filter_serialization() {
let expected =
r#"["0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b",null,["0x000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b","0x0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc"],null]"#;
let topic = TopicFilter {
topic0: Topic::This(hash("000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b")),
topic1: Topic::Any,
topic2: Topic::OneOf(vec![hash("000000000000000000000000a94f5374fce5edbc8e2a8697c15331677e6ebf0b"), hash("0000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebccc")]),
topic3: Topic::Any,
};
let topic_str = serde_json::to_string(&topic).unwrap();
assert_eq!(expected, &topic_str);
}
#[test]
fn test_topic_from() {
assert_eq!(Topic::Any as Topic<u64>, None.into());
assert_eq!(Topic::This(10u64), 10u64.into());
assert_eq!(Topic::OneOf(vec![10u64, 20]), vec![10u64, 20].into());
}
#[test]
fn test_topic_into_vec() {
let expected: Vec<u64> = vec![];
let is: Vec<u64> = (Topic::Any as Topic<u64>).into();
assert_eq!(expected, is);
let expected: Vec<u64> = vec![10];
let is: Vec<u64> = Topic::This(10u64).into();
assert_eq!(expected, is);
let expected: Vec<u64> = vec![10, 20];
let is: Vec<u64> = Topic::OneOf(vec![10u64, 20]).into();
assert_eq!(expected, is);
}
}