Skip to content

Commit

Permalink
Make clippy happy
Browse files Browse the repository at this point in the history
  • Loading branch information
Eijebong committed Oct 24, 2017
1 parent 1f08a58 commit 2e5abc1
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 80 deletions.
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
doc-valid-idents = ["InfluxDB"]
22 changes: 8 additions & 14 deletions src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum WriteStatus {
pub struct Options {
pub max_batch: Option<u16>,
pub precision: Option<Precision>,

pub epoch: Option<Precision>,
pub chunk_size: Option<u16>
}
Expand Down Expand Up @@ -59,12 +59,9 @@ impl<'a> Client for HttpClient<'a> {
query.insert("db", self.credentials.database.to_string());
query.insert("q", q);

match epoch {
Some(ref epoch) => {
query.insert("epoch", epoch.to_string());
}
_ => {}
};
if let Some(ref epoch) = epoch {
query.insert("epoch", epoch.to_string());
}

let request = Request {
url: &*{host.to_string() + "/query"},
Expand Down Expand Up @@ -102,12 +99,9 @@ impl<'a> Client for HttpClient<'a> {
let mut query = HashMap::new();
query.insert("db", self.credentials.database.to_string());

match precision {
Some(ref precision) => {
query.insert("precision", precision.to_string());
}
_ => {}
};
if let Some(ref precision) = precision {
query.insert("precision", precision.to_string());
}

let request = Request {
url: &*{host.to_string() + "/write"},
Expand Down Expand Up @@ -191,7 +185,7 @@ mod tests {
}
}

fn before<'a>(result: Box<Fn() -> HurlResult>) -> HttpClient<'a> {
fn before<'a>(result: Box<Fn() -> HurlResult>) -> HttpClient<'a> {
let credentials = Credentials {
username: "gobwas",
password: "1234",
Expand Down
4 changes: 2 additions & 2 deletions src/client/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub enum WriteStatus {
pub struct Options {
pub max_batch: Option<u16>,
pub precision: Option<Precision>,

pub epoch: Option<Precision>,
pub chunk_size: Option<u16>
}
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<'a> Client for UdpClient<'a> {

for chunk in measurements.chunks(self.max_batch as usize) {
let mut bytes = Vec::new();
const MAX_UDP_PACKET_LEN: usize = 65535;
const MAX_UDP_PACKET_LEN: usize = 65_535;

for measurement in chunk {
let line = self.serializer.serialize(measurement);
Expand Down
73 changes: 34 additions & 39 deletions src/hurl/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use std::io::Read;

use super::Hurl;

#[derive(Default)]
pub struct HyperHurl;

impl HyperHurl {
pub fn new() -> HyperHurl {
HyperHurl
HyperHurl::default()
}
}

Expand All @@ -38,50 +39,44 @@ impl Hurl for HyperHurl {
};

// if request need to be authorized
match req.auth {
Some(auth) => {
headers.set(
Authorization(
Basic {
username: auth.username.to_string(),
password: Some(auth.password.to_string())
}
)
);
}
_ => {}
};
if let Some(auth) = req.auth {
headers.set(
Authorization(
Basic {
username: auth.username.to_string(),
password: Some(auth.password.to_string())
}
)
);
}

// if request has query
match req.query {
Some(ref query) => {
// if any existing pairs
let existing: Vec<(String, String)> = url.query_pairs().map(|(a,b)| (a.to_string(), b.to_string())).collect();

// final pairs
let mut pairs: Vec<(&str, &str)> = Vec::new();

// add first existing
for pair in &existing {
pairs.push((&pair.0, &pair.1));
}

// add given query to the pairs
for (key, val) in query.iter() {
pairs.push((key, val));
}

// set new pairs
url.query_pairs_mut().clear().extend_pairs(
pairs.iter().map(|&(k, v)| { (&k[..], &v[..]) })
);
if let Some(ref query) = req.query {
// if any existing pairs
let existing: Vec<(String, String)> = url.query_pairs().map(|(a,b)| (a.to_string(), b.to_string())).collect();

// final pairs
let mut pairs: Vec<(&str, &str)> = Vec::new();

// add first existing
for pair in &existing {
pairs.push((&pair.0, &pair.1));
}
_ => {}
};

// add given query to the pairs
for (key, val) in query.iter() {
pairs.push((key, val));
}

// set new pairs
url.query_pairs_mut().clear().extend_pairs(
pairs.iter().map(|&(k, v)| { (&k[..], &v[..]) })
);
}

// create query
let mut query = client.request(method, url).headers(headers);

// if request has body
query = match req.body {
Some(ref body) => {
Expand Down
2 changes: 1 addition & 1 deletion src/hurl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ pub enum Method {
pub struct Auth<'a> {
pub username: &'a str,
pub password: &'a str
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn create_client<'a>(credentials: Credentials<'a>, hosts: Vec<&'a str>) -> H
/// use influent::create_udp_client;
/// let client = create_udp_client(vec!["127.0.0.1:8089"]);
/// ```
pub fn create_udp_client<'a>(hosts: Vec<&'a str>) -> UdpClient<'a> {
pub fn create_udp_client(hosts: Vec<&str>) -> UdpClient {
let mut client = UdpClient::new(Box::new(LineSerializer::new()));

for host in hosts {
Expand Down
5 changes: 2 additions & 3 deletions src/measurement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub struct Measurement<'a> {

/// Map of fields.
pub fields: BTreeMap<Cow<'a, str>, Value<'a>>,

/// Map of tags.
pub tags: BTreeMap<Cow<'a,str>, Cow<'a,str>>
}
Expand All @@ -33,7 +32,7 @@ impl<'a> Measurement<'a> {
/// Constructs a new `Measurement`.
///
/// # Examples
///
///
/// ```
/// use influent::measurement::Measurement;
///
Expand Down Expand Up @@ -92,4 +91,4 @@ impl<'a> Measurement<'a> {
pub fn set_timestamp(&mut self, timestamp: i64) {
self.timestamp = Some(timestamp);
}
}
}
24 changes: 11 additions & 13 deletions src/serializer/line.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ::measurement::{Measurement, Value};
use ::serializer::Serializer;

#[derive(Default)]
pub struct LineSerializer;

/// Line spec `Measurement` serializer.
Expand All @@ -23,7 +24,7 @@ impl LineSerializer {
/// assert_eq!("key,tag=value field=\"value\"", serializer.serialize(&measurement));
/// ```
pub fn new() -> LineSerializer {
LineSerializer
LineSerializer::default()
}
}

Expand Down Expand Up @@ -53,7 +54,7 @@ impl Serializer for LineSerializer {
fn serialize(&self, measurement: &Measurement) -> String {
let mut line = vec![escape(measurement.key)];

for (tag, value) in measurement.tags.iter() {
for (tag, value) in &measurement.tags {
line.push(",".to_string());
line.push(escape(tag));
line.push("=".to_string());
Expand All @@ -62,25 +63,22 @@ impl Serializer for LineSerializer {

let mut was_spaced = false;

for (field, value) in measurement.fields.iter() {
for (field, value) in &measurement.fields {
line.push({if !was_spaced { was_spaced = true; " " } else { "," }}.to_string());
line.push(escape(field));
line.push("=".to_string());

match value {
&Value::String(ref s) => line.push(as_string(s)),
&Value::Integer(ref i) => line.push(as_integer(i)),
&Value::Float(ref f) => line.push(as_float(f)),
&Value::Boolean(ref b) => line.push(as_boolean(b))
match *value {
Value::String(s) => line.push(as_string(s)),
Value::Integer(ref i) => line.push(as_integer(i)),
Value::Float(ref f) => line.push(as_float(f)),
Value::Boolean(ref b) => line.push(as_boolean(b))
};
}

match measurement.timestamp {
Some(t) => {
if let Some(t) = measurement.timestamp {
line.push(" ".to_string());
line.push(t.to_string());
}
_ => {}
}

line.join("")
Expand Down Expand Up @@ -138,7 +136,7 @@ mod tests {
measurement.add_field("b", Value::Boolean(false));

measurement.add_tag("tag", "value");

measurement.add_field("one, two", Value::String("three"));
measurement.add_tag("one ,two", "three, four");

Expand Down
2 changes: 1 addition & 1 deletion src/serializer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ pub mod line;
pub trait Serializer {
/// Serializes measurement to String.
fn serialize(&self, measurement: &Measurement) -> String;
}
}
10 changes: 4 additions & 6 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
extern crate influent;

use std::io::prelude::*;
use influent::create_client;
use influent::client::{Client, Credentials};
use influent::client::http::HttpClient;
use influent::hurl::hyper::HyperHurl;
use influent::measurement::{Measurement, Value};

fn before<'a>() -> HttpClient<'a> {
Expand All @@ -14,7 +12,7 @@ fn before<'a>() -> HttpClient<'a> {
database: "test"
};

let mut client = create_client(credentials, vec!["http://localhost:8086"]);
let client = create_client(credentials, vec!["http://localhost:8086"]);

client.query("drop database test".to_string(), None).unwrap();
client.query("create database test".to_string(), None).unwrap();
Expand All @@ -24,7 +22,7 @@ fn before<'a>() -> HttpClient<'a> {

#[test]
fn test_write_measurement() {
let mut client = before();
let client = before();

let mut measurement = Measurement::new("sut");

Expand All @@ -37,10 +35,10 @@ fn test_write_measurement() {
measurement.add_tag("tag", "value");
measurement.add_tag("tag, with comma", "three, four");

measurement.set_timestamp(1434055562000000000);
measurement.set_timestamp(1_434_055_562_000_000_000);

assert!(client.write_one(measurement, None).is_ok());

let fixture = "{\"results\":[{\"series\":[{\"name\":\"sut\",\"columns\":[\"time\",\"boolean\",\"float\",\"integer\",\"string\",\"tag\",\"tag, with comma\",\"with, comma\"],\"values\":[[\"2015-06-11T20:46:02Z\",false,10,10,\"string\",\"value\",\"three, four\",\"comma, with\"]]}]}]}";
assert_eq!(fixture, client.query("select * from \"sut\"".to_string(), None).unwrap());
}
}

0 comments on commit 2e5abc1

Please sign in to comment.