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
173
174
175
176
177
178
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash)]
pub enum Sharing {
#[serde(rename = "public")]
Public,
#[serde(rename = "private")]
Private,
#[serde(rename = "encrypted")]
Encrypted,
}
impl Sharing {
#[cfg_attr(rustfmt, rustfmt_skip)]
pub fn can_publish(self) -> bool {
match self {
Sharing::Public => true,
Sharing::Private => false,
Sharing::Encrypted => true,
}
}
}
impl Default for Sharing {
fn default() -> Self {
Sharing::Public
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash)]
pub struct LinksTo {
#[serde(default)]
pub target_type: String,
#[serde(default)]
pub tag: String,
}
impl Default for LinksTo {
fn default() -> Self {
LinksTo {
target_type: String::new(),
tag: String::new(),
}
}
}
impl LinksTo {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash)]
pub struct LinkedFrom {
#[serde(default)]
pub base_type: String,
#[serde(default)]
pub tag: String,
}
impl Default for LinkedFrom {
fn default() -> Self {
LinkedFrom {
base_type: String::new(),
tag: String::new(),
}
}
}
impl LinkedFrom {
pub fn new() -> Self {
Default::default()
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Hash)]
pub struct EntryTypeDef {
#[serde(default)]
pub description: String,
#[serde(default)]
pub sharing: Sharing,
#[serde(default)]
pub links_to: Vec<LinksTo>,
#[serde(default)]
pub linked_from: Vec<LinkedFrom>,
}
impl Default for EntryTypeDef {
fn default() -> Self {
EntryTypeDef {
description: String::new(),
sharing: Sharing::Public,
links_to: Vec::new(),
linked_from: Vec::new(),
}
}
}
impl EntryTypeDef {
pub fn new() -> Self {
Default::default()
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json;
#[test]
fn can_publish() {
assert!(Sharing::Public.can_publish());
assert!(!Sharing::Private.can_publish());
}
#[test]
fn build_and_compare() {
let fixture: EntryTypeDef = serde_json::from_str(
r#"{
"description": "test",
"sharing": "public",
"links_to": [
{
"target_type": "test",
"tag": "test"
}
],
"linked_from": [
{
"base_type": "HcSysAgentKeyHash",
"tag": "authored_posts"
}
]
}"#,
).unwrap();
let mut entry = EntryTypeDef::new();
entry.description = String::from("test");
entry.sharing = Sharing::Public;
let mut link = LinksTo::new();
link.target_type = String::from("test");
link.tag = String::from("test");
entry.links_to.push(link);
let mut linked = LinkedFrom::new();
linked.base_type = String::from("HcSysAgentKeyHash");
linked.tag = String::from("authored_posts");
entry.linked_from.push(linked);
assert_eq!(fixture, entry);
}
}