Skip to content

Commit afc0ec2

Browse files
committed
Added Rustdoc Comments where appropriate
1 parent 35a918d commit afc0ec2

File tree

3 files changed

+148
-42
lines changed

3 files changed

+148
-42
lines changed

Cargo.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
[package]
2-
name = "rust-influxdb"
2+
name = "influxdb"
33
version = "0.1.0"
44
authors = ["Gero Gerke <[email protected]>"]
55
edition = "2018"
66

7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8-
97
[dependencies]
108
reqwest = "0.9.17"
119
futures = "0.1.27"

src/main.rs renamed to src/lib.rs

Lines changed: 136 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ use itertools::Itertools;
88
use reqwest::r#async::Client;
99

1010
#[derive(Debug, Fail)]
11-
enum InfluxDbError {
11+
/// Errors that might happen in the crate
12+
pub enum InfluxDbError {
1213
#[fail(display = "query must contain at least one field")]
14+
/// Error happens when query has zero fields
1315
InvalidQueryError,
1416
}
1517

1618
#[derive(Debug)]
17-
struct ValidQuery(String);
18-
impl From<String> for ValidQuery {
19-
fn from(s: String) -> ValidQuery {
20-
ValidQuery(s)
21-
}
22-
}
19+
#[doc(hidden)]
20+
pub struct ValidQuery(String);
2321
impl PartialEq<String> for ValidQuery {
2422
fn eq(&self, other: &String) -> bool {
2523
&self.0 == other
@@ -31,16 +29,56 @@ impl PartialEq<&str> for ValidQuery {
3129
}
3230
}
3331

34-
trait InfluxDbQuery {
32+
/// Used to create read or [`InfluxDbWriteQuery`] queries to InfluxDB
33+
///
34+
/// # Examples
35+
///
36+
/// ```rust
37+
/// use influxdb::InfluxDbQuery;
38+
///
39+
/// let write_query = InfluxDbQuery::write_query("measurement")
40+
/// .add_field("field1", "5")
41+
/// .add_tag("tag1", "Gero")
42+
/// .build();
43+
///
44+
/// assert!(query.is_ok());
45+
///
46+
/// //todo: document read query once it's implemented.
47+
/// ```
48+
pub trait InfluxDbQuery {
49+
/// Builds valid InfluxSQL which can be run against the Database.
50+
/// In case no fields have been specified, it will return an error,
51+
/// as that is invalid InfluxSQL syntax.
52+
///
53+
/// # Examples
54+
///
55+
/// ```rust
56+
/// use influxdb::InfluxDbQuery;
57+
///
58+
/// let invalid_query = InfluxDbQuery::write_query("measurement").build();
59+
/// assert!(query.is_err());
60+
///
61+
/// let valid_query = InfluxDbQuery::write_query("measurement").add_field("myfield1", "11").build();
62+
/// assert!(query.is_ok());
63+
/// ```
3564
fn build<'a>(self) -> Result<ValidQuery, InfluxDbError>;
3665
}
3766

3867
impl InfluxDbQuery {
39-
pub fn write<S>(measurement: S) -> InfluxDbWrite
68+
/// Returns a [`InfluxDbWriteQuery`] builder.
69+
///
70+
/// # Examples
71+
///
72+
/// ```rust
73+
/// use influxdb::InfluxDbQuery;
74+
///
75+
/// InfluxDbQuery::write_query("measurement"); // Is of type [`InfluxDbWriteQuery`]
76+
/// ```
77+
pub fn write_query<S>(measurement: S) -> InfluxDbWriteQuery
4078
where
4179
S: Into<String>,
4280
{
43-
InfluxDbWrite {
81+
InfluxDbWriteQuery {
4482
measurement: measurement.into(),
4583
fields: Vec::new(),
4684
tags: Vec::new(),
@@ -50,23 +88,48 @@ impl InfluxDbQuery {
5088
// pub fn read() {}
5189
}
5290

53-
pub struct InfluxDbWrite {
91+
/// Write Query Builder returned by [InfluxDbQuery::write_query]()
92+
///
93+
/// Can only be instantiated by using [InfluxDbQuery::write_query]()
94+
pub struct InfluxDbWriteQuery {
5495
fields: Vec<(String, String)>,
5596
tags: Vec<(String, String)>,
5697
measurement: String,
5798
// precision: Precision
5899
}
59100

60-
impl InfluxDbWrite {
61-
fn add_field<'a, S>(mut self, point: S, value: S) -> Self
101+
impl InfluxDbWriteQuery {
102+
/// Adds a field to the [`InfluxDbWriteQuery`]
103+
///
104+
/// # Examples
105+
///
106+
/// ```rust
107+
/// use influxdb::InfluxDbQuery;
108+
///
109+
/// InfluxDbQuery::write_query("measurement").add_field("field1", "5").build();
110+
/// ```
111+
pub fn add_field<'a, S>(mut self, point: S, value: S) -> Self
62112
where
63113
S: Into<String>,
64114
{
65115
self.fields.push((point.into(), value.into()));
66116
self
67117
}
68118

69-
fn add_tag<'a, S>(mut self, tag: S, value: S) -> Self
119+
/// Adds a tag to the [`InfluxDbWriteQuery`]
120+
///
121+
/// Please note that a [`InfluxDbWriteQuery`] requires at least one field. Composing a query with
122+
/// only tags will result in a failure building the query.
123+
///
124+
/// # Examples
125+
///
126+
/// ```rust
127+
/// use influxdb::InfluxDbQuery;
128+
///
129+
/// InfluxDbQuery::write_query("measurement")
130+
/// .add_tag("field1", "5"); // calling `.build()` now would result in a `Err(InfluxDbError::InvalidQueryError)`
131+
/// ```
132+
pub fn add_tag<'a, S>(mut self, tag: S, value: S) -> Self
70133
where
71134
S: Into<String>,
72135
{
@@ -76,8 +139,8 @@ impl InfluxDbWrite {
76139
}
77140

78141
// todo: fuse_with(other: ValidQuery), so multiple queries can be run at the same time
79-
impl InfluxDbQuery for InfluxDbWrite {
80-
// fixme: time (with precision) and measurement
142+
impl InfluxDbQuery for InfluxDbWriteQuery {
143+
// todo: time (with precision)
81144
fn build<'a>(self) -> Result<ValidQuery, InfluxDbError> {
82145
if self.fields.is_empty() {
83146
return Err(InfluxDbError::InvalidQueryError);
@@ -96,7 +159,7 @@ impl InfluxDbQuery for InfluxDbWrite {
96159
.join(",")
97160
+ " ";
98161

99-
Ok(ValidQuery::from(format!(
162+
Ok(ValidQuery(format!(
100163
"{measurement},{tags}{fields}time",
101164
measurement = self.measurement,
102165
tags = tags,
@@ -105,15 +168,61 @@ impl InfluxDbQuery for InfluxDbWrite {
105168
}
106169
}
107170

171+
/// Client which can read and write data from InfluxDB.
172+
///
173+
/// # Arguments
174+
///
175+
/// * `url`: The URL where InfluxDB is running (ex. `http://localhost:8086`).
176+
/// * `database`: The Database against which queries and writes will be run.
177+
///
178+
/// # Examples
179+
///
180+
/// ```rust
181+
/// use influxdb::InfluxDbClient;
182+
///
183+
/// let client = InfluxDbClient::new("http://localhost:8086", "test");
184+
///
185+
/// assert_eq!(client.get_database_name(), "test");
186+
/// ```
108187
pub struct InfluxDbClient {
109188
url: String,
110189
database: String,
111190
// auth: Option<InfluxDbAuthentication>
112191
}
113192

114-
pub fn main() {}
115-
116193
impl InfluxDbClient {
194+
/// Instantiates a new [`InfluxDbClient`]
195+
///
196+
/// # Arguments
197+
///
198+
/// * `url`: The URL where InfluxDB is running (ex. `http://localhost:8086`).
199+
/// * `database`: The Database against which queries and writes will be run.
200+
///
201+
/// # Examples
202+
///
203+
/// ```rust
204+
/// use influxdb::InfluxDbClient;
205+
///
206+
/// let _client = InfluxDbClient::new("http://localhost:8086", "test");
207+
/// ```
208+
pub fn new<S>(url: S, database: S) -> Self
209+
where
210+
S: Into<String>,
211+
{
212+
InfluxDbClient {
213+
url: url.into(),
214+
database: database.into(),
215+
}
216+
}
217+
218+
pub fn get_database_name(self) -> String {
219+
self.database
220+
}
221+
222+
pub fn get_database_url(self) -> String {
223+
self.url
224+
}
225+
117226
pub fn ping(&self) -> impl Future<Item = (String, String), Error = ()> {
118227
Client::new()
119228
.get(format!("{}/ping", self.url).as_str())
@@ -138,6 +247,8 @@ impl InfluxDbClient {
138247
}
139248
}
140249

250+
pub fn main() {}
251+
141252
#[cfg(test)]
142253
mod tests {
143254
use super::{InfluxDbClient, InfluxDbQuery};
@@ -148,10 +259,7 @@ mod tests {
148259
}
149260

150261
fn create_client() -> InfluxDbClient {
151-
InfluxDbClient {
152-
url: String::from("http://localhost:8086"),
153-
database: String::from("test"),
154-
}
262+
InfluxDbClient::new("http://localhost:8086", "test")
155263
}
156264

157265
#[test]
@@ -169,14 +277,14 @@ mod tests {
169277

170278
#[test]
171279
fn test_write_builder_empty_query() {
172-
let query = InfluxDbQuery::write("marina_3").build();
280+
let query = InfluxDbQuery::write_query("marina_3").build();
173281

174282
assert!(query.is_err(), "Query was not empty");
175283
}
176284

177285
#[test]
178286
fn test_write_builder_single_field() {
179-
let query = InfluxDbQuery::write("marina_3")
287+
let query = InfluxDbQuery::write_query("marina_3")
180288
.add_field("water_level", "2")
181289
.build();
182290

@@ -186,7 +294,7 @@ mod tests {
186294

187295
#[test]
188296
fn test_write_builder_multiple_fields() {
189-
let query = InfluxDbQuery::write("marina_3")
297+
let query = InfluxDbQuery::write_query("marina_3")
190298
.add_field("water_level", "2")
191299
.add_field("boat_count", "31")
192300
.add_field("algae_content", "0.85")
@@ -202,7 +310,7 @@ mod tests {
202310
// fixme: quoting / escaping of long strings
203311
#[test]
204312
fn test_write_builder_only_tags() {
205-
let query = InfluxDbQuery::write("marina_3")
313+
let query = InfluxDbQuery::write_query("marina_3")
206314
.add_tag("marina_manager", "Smith")
207315
.build();
208316

@@ -211,7 +319,7 @@ mod tests {
211319

212320
#[test]
213321
fn test_write_builder_full_query() {
214-
let query = InfluxDbQuery::write("marina_3")
322+
let query = InfluxDbQuery::write_query("marina_3")
215323
.add_field("water_level", "2")
216324
.add_field("boat_count", "31")
217325
.add_field("algae_content", "0.85")

0 commit comments

Comments
 (0)