Skip to content

Commit 438ba1c

Browse files
committed
Code Review Improvements
1 parent 44d007b commit 438ba1c

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ edition = "2018"
1010
reqwest = "0.9.17"
1111
futures = "0.1.27"
1212
tokio = "0.1.20"
13-
either = "1.5.2"
13+
either = "1.5.2"

src/main.rs

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(dead_code)]
2+
13
extern crate futures;
24
extern crate reqwest;
35
extern crate tokio;
@@ -12,36 +14,36 @@ trait InfluxDbQuery {
1214
impl InfluxDbQuery {
1315
pub fn write() -> InfluxDbWrite {
1416
InfluxDbWrite {
15-
_measurement: String::from("marina_3"),
16-
_fields: Vec::new(),
17-
_tags: Vec::new(),
17+
measurement: String::from("marina_3"),
18+
fields: Vec::new(),
19+
tags: Vec::new(),
1820
}
1921
}
2022

2123
// pub fn read() {}
2224
}
2325

2426
pub struct InfluxDbWrite {
25-
_fields: Vec<(String, String)>,
26-
_tags: Vec<(String, String)>,
27-
_measurement: String,
27+
fields: Vec<(String, String)>,
28+
tags: Vec<(String, String)>,
29+
measurement: String,
2830
// precision: Precision
2931
}
3032

3133
impl InfluxDbWrite {
32-
fn add_field<'a, S>(&'a mut self, point: S, value: S) -> &'a mut Self
34+
fn add_field<'a, S>(mut self, point: S, value: S) -> Self
3335
where
3436
S: Into<String>,
3537
{
36-
self._fields.push((point.into(), value.into()));
38+
self.fields.push((point.into(), value.into()));
3739
self
3840
}
3941

40-
fn add_tag<'a, S>(&'a mut self, tag: S, value: S) -> &'a mut Self
42+
fn add_tag<'a, S>(mut self, tag: S, value: S) -> Self
4143
where
4244
S: Into<String>,
4345
{
44-
self._tags.push((tag.into(), value.into()));
46+
self.tags.push((tag.into(), value.into()));
4547
self
4648
}
4749
}
@@ -50,13 +52,13 @@ impl InfluxDbQuery for InfluxDbWrite {
5052
// fixme: time (with precision) and measurement
5153
fn build<'a>(self) -> String {
5254
let tags = self
53-
._tags
55+
.tags
5456
.into_iter()
5557
.map(|(tag, value)| format!("{tag}={value}", tag = tag, value = value))
5658
.collect::<Vec<String>>()
5759
.join(",");
5860
let fields = self
59-
._fields
61+
.fields
6062
.into_iter()
6163
.map(|(field, value)| format!("{field}={value}", field = field, value = value))
6264
.collect::<Vec<String>>()
@@ -71,8 +73,8 @@ impl InfluxDbQuery for InfluxDbWrite {
7173
}
7274

7375
pub struct InfluxDbClient {
74-
_url: String,
75-
_database: String,
76+
url: String,
77+
database: String,
7678
// _auth: InfluxDbAuthentication | NoAuthentication
7779
}
7880

@@ -81,7 +83,7 @@ pub fn main() {}
8183
impl InfluxDbClient {
8284
pub fn ping(&self) -> impl Future<Item = (String, String), Error = ()> {
8385
Client::new()
84-
.get(format!("{}/ping", self._url).as_str())
86+
.get(format!("{}/ping", self.url).as_str())
8587
.send()
8688
.map(|res| {
8789
let build = res
@@ -114,16 +116,16 @@ mod tests {
114116

115117
fn create_client() -> InfluxDbClient {
116118
InfluxDbClient {
117-
_url: String::from("http://localhost:8086"),
118-
_database: String::from("test"),
119+
url: String::from("http://localhost:8086"),
120+
database: String::from("test"),
119121
}
120122
}
121123

122124
#[test]
123125
fn test_ping() {
124126
let client = create_client();
125127
let result = get_runtime().block_on(client.ping());
126-
assert!(!result.is_err(), "Should be no error");
128+
assert!(result.is_ok(), "Should be no error");
127129

128130
let (build, version) = result.unwrap();
129131
assert!(!build.is_empty(), "Build should not be empty");
@@ -134,19 +136,19 @@ mod tests {
134136

135137
#[test]
136138
fn test_write_builder_single_field() {
137-
let mut query = InfluxDbQuery::write();
138-
139-
query.add_field("water_level", "2");
139+
let query = InfluxDbQuery::write()
140+
.add_field("water_level", "2");
141+
140142
assert_eq!(query.build(), "measurement, water_level=2 time");
141143
}
142144

143145
#[test]
144146
fn test_write_builder_multiple_fields() {
145-
let mut query = InfluxDbQuery::write();
147+
let query = InfluxDbQuery::write()
148+
.add_field("water_level", "2")
149+
.add_field("boat_count", "31")
150+
.add_field("algae_content", "0.85");
146151

147-
query.add_field("water_level", "2");
148-
query.add_field("boat_count", "31");
149-
query.add_field("algae_content", "0.85");
150152
assert_eq!(
151153
query.build(),
152154
"measurement, water_level=2,boat_count=31,algae_content=0.85 time"
@@ -157,18 +159,18 @@ mod tests {
157159
// fixme: quoting / escaping of long strings
158160
#[test]
159161
fn test_write_builder_single_tag() {
160-
let mut query = InfluxDbQuery::write();
162+
let query = InfluxDbQuery::write()
163+
.add_tag("marina_manager", "Smith");
161164

162-
query.add_tag("marina_manager", "Smith");
163165
assert_eq!(query.build(), "measurement,marina_manager=Smith time");
164166
}
165167

166168
#[test]
167169
fn test_write_builder_multiple_tags() {
168-
let mut query = InfluxDbQuery::write();
170+
let query = InfluxDbQuery::write()
171+
.add_tag("marina_manager", "Smith")
172+
.add_tag("manager_to_the_marina_manager", "Jonson");
169173

170-
query.add_tag("marina_manager", "Smith");
171-
query.add_tag("manager_to_the_marina_manager", "Jonson");
172174
assert_eq!(
173175
query.build(),
174176
"measurement,marina_manager=Smith,manager_to_the_marina_manager=Jonson time"
@@ -177,16 +179,21 @@ mod tests {
177179

178180
#[test]
179181
fn test_write_builder_full_query() {
180-
let mut query = InfluxDbQuery::write();
182+
let query = InfluxDbQuery::write()
183+
.add_field("water_level", "2")
184+
.add_field("boat_count", "31")
185+
.add_field("algae_content", "0.85")
186+
.add_tag("marina_manager", "Smith")
187+
.add_tag("manager_to_the_marina_manager", "Jonson");
181188

182-
query.add_field("water_level", "2");
183-
query.add_field("boat_count", "31");
184-
query.add_field("algae_content", "0.85");
185-
query.add_tag("marina_manager", "Smith");
186-
query.add_tag("manager_to_the_marina_manager", "Jonson");
187189
assert_eq!(
188190
query.build(),
189191
"measurement,marina_manager=Smith,manager_to_the_marina_manager=Jonson water_level=2,boat_count=31,algae_content=0.85 time"
190192
);
191193
}
192-
}
194+
195+
#[test]
196+
fn test_test() {
197+
InfluxDbQuery::write().add_field("test", "1").add_tag("my_tag", "0.85").build();
198+
}
199+
}

0 commit comments

Comments
 (0)