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
use crate::{
model::{add_qp, graph::EdgeDefinition, BuildUrl, QueryParam::WaitForSync},
Connection,
};
use anyhow::{Context, Result};
use derive_builder::Builder;
use getset::Getters;
use reqwest::Url;
use serde::{Deserialize, Serialize};
const EMPTY_EDGE_DEFINITIONS_ERR: &str = "edge_definitions cannot be empty!";
#[derive(Builder, Clone, Debug, Default, Deserialize, Getters, Serialize)]
#[getset(get = "pub(crate)")]
pub struct Config {
graph: GraphMeta,
#[builder(setter(strip_option), default)]
#[serde(skip_serializing_if = "Option::is_none")]
wait_for_sync: Option<bool>,
}
impl Config {
fn build_suffix(&self, base: &str) -> String {
let mut url = base.to_string();
let mut has_qp = false;
add_qp(*self.wait_for_sync(), &mut url, &mut has_qp, WaitForSync);
url
}
}
impl BuildUrl for Config {
fn build_url(&self, base: &str, conn: &Connection) -> Result<Url> {
let suffix = self.build_suffix(base);
conn.db_url()
.join(&suffix)
.with_context(|| format!("Unable to build '{}' url", suffix))
}
}
#[derive(Builder, Clone, Debug, Default, Deserialize, Getters, Serialize)]
#[getset(get = "pub(crate)")]
#[builder(build_fn(validate = "Self::validate"))]
pub struct GraphMeta {
#[builder(setter(into))]
name: String,
#[serde(rename = "edgeDefinitions")]
edge_definitions: Vec<EdgeDefinition>,
#[builder(setter(strip_option), default)]
#[serde(skip_serializing_if = "Option::is_none")]
orphan_collections: Option<Vec<String>>,
}
impl GraphMetaBuilder {
fn validate(&self) -> std::result::Result<(), String> {
self.edge_definitions.as_ref().map_or(Ok(()), |ed| {
if ed.is_empty() {
Err(EMPTY_EDGE_DEFINITIONS_ERR.into())
} else {
Ok(())
}
})
}
}
#[cfg(test)]
mod test {
use super::{Config, ConfigBuilder, GraphMetaBuilder, EMPTY_EDGE_DEFINITIONS_ERR};
use crate::{
graph::BASE_GRAPH_SUFFIX,
model::{
graph::{EdgeDefinition, EdgeDefinitionBuilder},
WAIT_FOR_SYNC_QP,
},
};
use anyhow::Result;
use const_format::concatcp;
const BASIC_ACTUAL: &str = concatcp!(BASE_GRAPH_SUFFIX);
const WAIT_FOR_SYNC_ACTUAL: &str = concatcp!(BASIC_ACTUAL, "?", WAIT_FOR_SYNC_QP);
fn check_url(config: &Config, actual: &str) {
assert_eq!(actual, config.build_suffix(BASE_GRAPH_SUFFIX));
}
fn ve(vec: Vec<&str>) -> Vec<String> {
vec.into_iter().map(str::to_string).collect()
}
fn edge_definition() -> Result<Vec<EdgeDefinition>> {
let ed = EdgeDefinitionBuilder::default()
.collection("test_edge")
.from(ve(vec!["test_coll"]))
.to(ve(vec!["test_coll"]))
.build()?;
Ok(vec![ed])
}
#[test]
fn create_url() -> Result<()> {
let graph_meta = GraphMetaBuilder::default()
.name("test")
.edge_definitions(edge_definition()?)
.build()?;
let config = ConfigBuilder::default().graph(graph_meta).build()?;
check_url(&config, BASIC_ACTUAL);
Ok(())
}
#[test]
fn create_wait_for_sync_url() -> Result<()> {
let graph_meta = GraphMetaBuilder::default()
.name("test")
.edge_definitions(edge_definition()?)
.build()?;
let config = ConfigBuilder::default()
.graph(graph_meta)
.wait_for_sync(true)
.build()?;
check_url(&config, WAIT_FOR_SYNC_ACTUAL);
Ok(())
}
#[test]
fn empty_ed_errors() {
match GraphMetaBuilder::default()
.name("test")
.edge_definitions(vec![])
.build()
{
Ok(_) => panic!("The builder should fail!"),
Err(e) => assert_eq!(EMPTY_EDGE_DEFINITIONS_ERR, format!("{}", e)),
}
}
}