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
use bs58;
use crate::rpc_proto;
use protobuf::Message;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TopicHash {
hash: String,
}
impl TopicHash {
#[inline]
pub fn from_raw(hash: String) -> TopicHash {
TopicHash { hash }
}
#[inline]
pub fn into_string(self) -> String {
self.hash
}
}
#[derive(Debug, Clone)]
pub struct Topic {
descriptor: rpc_proto::TopicDescriptor,
hash: TopicHash,
}
impl Topic {
#[inline]
pub fn hash(&self) -> &TopicHash {
&self.hash
}
}
impl AsRef<TopicHash> for Topic {
#[inline]
fn as_ref(&self) -> &TopicHash {
&self.hash
}
}
impl From<Topic> for TopicHash {
#[inline]
fn from(topic: Topic) -> TopicHash {
topic.hash
}
}
impl<'a> From<&'a Topic> for TopicHash {
#[inline]
fn from(topic: &'a Topic) -> TopicHash {
topic.hash.clone()
}
}
#[derive(Debug, Clone)]
pub struct TopicBuilder {
builder: rpc_proto::TopicDescriptor,
}
impl TopicBuilder {
pub fn new<S>(name: S) -> TopicBuilder
where
S: Into<String>,
{
let mut builder = rpc_proto::TopicDescriptor::new();
builder.set_name(name.into());
TopicBuilder { builder }
}
pub fn build(self) -> Topic {
let bytes = self
.builder
.write_to_bytes()
.expect("protobuf message is always valid");
let hash = TopicHash {
hash: bs58::encode(&bytes).into_string(),
};
Topic {
descriptor: self.builder,
hash,
}
}
}