Skip to content

Commit 8cc4b32

Browse files
pcpthmEmpty2k12
authored andcommitted
Replace use of Any by an enum (#21)
1 parent d4805e7 commit 8cc4b32

File tree

2 files changed

+60
-46
lines changed

2 files changed

+60
-46
lines changed

Diff for: src/client/mod.rs

+41-45
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ use reqwest::{StatusCode, Url};
2222
use std::mem;
2323

2424
use crate::error::InfluxDbError;
25-
use crate::query::read_query::InfluxDbReadQuery;
26-
use crate::query::write_query::InfluxDbWriteQuery;
27-
use crate::query::InfluxDbQuery;
28-
29-
use std::any::Any;
25+
use crate::query::{InfluxDbQuery, InfluxDbQueryTypes};
3026

3127
#[derive(Clone, Debug)]
3228
/// Internal Authentication representation
@@ -184,9 +180,10 @@ impl InfluxDbClient {
184180
/// a [`InfluxDbError`] variant will be returned.
185181
///
186182
/// [`InfluxDbError`]: enum.InfluxDbError.html
187-
pub fn query<Q>(&self, q: &Q) -> Box<dyn Future<Item = String, Error = InfluxDbError>>
183+
pub fn query<'q, Q>(&self, q: &'q Q) -> Box<dyn Future<Item = String, Error = InfluxDbError>>
188184
where
189-
Q: Any + InfluxDbQuery,
185+
Q: InfluxDbQuery,
186+
&'q Q: Into<InfluxDbQueryTypes<'q>>,
190187
{
191188
use futures::future;
192189

@@ -200,49 +197,48 @@ impl InfluxDbClient {
200197
Ok(query) => query,
201198
};
202199

203-
let any_value = q as &dyn Any;
204200
let basic_parameters: Vec<(String, String)> = self.into();
205201

206-
let client = if let Some(_) = any_value.downcast_ref::<InfluxDbReadQuery>() {
207-
let read_query = query.get();
202+
let client = match q.into() {
203+
InfluxDbQueryTypes::Read(_) => {
204+
let read_query = query.get();
205+
let mut url = match Url::parse_with_params(
206+
format!("{url}/query", url = self.database_url()).as_str(),
207+
basic_parameters,
208+
) {
209+
Ok(url) => url,
210+
Err(err) => {
211+
let error = InfluxDbError::UrlConstructionError {
212+
error: format!("{}", err),
213+
};
214+
return Box::new(future::err::<String, InfluxDbError>(error));
215+
}
216+
};
217+
url.query_pairs_mut().append_pair("q", &read_query.clone());
208218

209-
let mut url = match Url::parse_with_params(
210-
format!("{url}/query", url = self.database_url()).as_str(),
211-
basic_parameters,
212-
) {
213-
Ok(url) => url,
214-
Err(err) => {
215-
let error = InfluxDbError::UrlConstructionError {
216-
error: format!("{}", err),
217-
};
218-
return Box::new(future::err::<String, InfluxDbError>(error));
219+
if read_query.contains("SELECT") || read_query.contains("SHOW") {
220+
Client::new().get(url)
221+
} else {
222+
Client::new().post(url)
219223
}
220-
};
221-
url.query_pairs_mut().append_pair("q", &read_query.clone());
222-
223-
if read_query.contains("SELECT") || read_query.contains("SHOW") {
224-
Client::new().get(url)
225-
} else {
226-
Client::new().post(url)
227224
}
228-
} else if let Some(write_query) = any_value.downcast_ref::<InfluxDbWriteQuery>() {
229-
let mut url = match Url::parse_with_params(
230-
format!("{url}/write", url = self.database_url()).as_str(),
231-
basic_parameters,
232-
) {
233-
Ok(url) => url,
234-
Err(err) => {
235-
let error = InfluxDbError::InvalidQueryError {
236-
error: format!("{}", err),
237-
};
238-
return Box::new(future::err::<String, InfluxDbError>(error));
239-
}
240-
};
241-
url.query_pairs_mut()
242-
.append_pair("precision", &write_query.get_precision());
243-
Client::new().post(url).body(query.get())
244-
} else {
245-
unreachable!()
225+
InfluxDbQueryTypes::Write(write_query) => {
226+
let mut url = match Url::parse_with_params(
227+
format!("{url}/write", url = self.database_url()).as_str(),
228+
basic_parameters,
229+
) {
230+
Ok(url) => url,
231+
Err(err) => {
232+
let error = InfluxDbError::InvalidQueryError {
233+
error: format!("{}", err),
234+
};
235+
return Box::new(future::err::<String, InfluxDbError>(error));
236+
}
237+
};
238+
url.query_pairs_mut()
239+
.append_pair("precision", &write_query.get_precision());
240+
Client::new().post(url).body(query.get())
241+
}
246242
};
247243
Box::new(
248244
client

Diff for: src/query/mod.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ impl fmt::Display for Timestamp {
5050
}
5151
}
5252

53+
/// Internal enum used to represent either type of query.
54+
pub enum InfluxDbQueryTypes<'a> {
55+
Read(&'a InfluxDbReadQuery),
56+
Write(&'a InfluxDbWriteQuery),
57+
}
58+
59+
impl<'a> From<&'a InfluxDbReadQuery> for InfluxDbQueryTypes<'a> {
60+
fn from(query: &'a InfluxDbReadQuery) -> Self {
61+
Self::Read(query)
62+
}
63+
}
64+
65+
impl<'a> From<&'a InfluxDbWriteQuery> for InfluxDbQueryTypes<'a> {
66+
fn from(query: &'a InfluxDbWriteQuery) -> Self {
67+
Self::Write(query)
68+
}
69+
}
70+
5371
pub trait InfluxDbQuery {
5472
/// Builds valid InfluxSQL which can be run against the Database.
5573
/// In case no fields have been specified, it will return an error,
@@ -71,7 +89,7 @@ pub trait InfluxDbQuery {
7189
fn get_type(&self) -> QueryType;
7290
}
7391

74-
impl InfluxDbQuery {
92+
impl dyn InfluxDbQuery {
7593
/// Returns a [`InfluxDbWriteQuery`](crate::query::write_query::InfluxDbWriteQuery) builder.
7694
///
7795
/// # Examples

0 commit comments

Comments
 (0)