Skip to content

Replace ToString with Into<String> #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ impl Client {
/// ```
pub fn new<S1, S2>(url: S1, database: S2) -> Self
where
S1: ToString,
S2: ToString,
S1: Into<String>,
S2: Into<String>,
{
Client {
url: url.to_string(),
database: database.to_string(),
url: url.into(),
database: database.into(),
auth: None,
}
}
Expand All @@ -107,12 +107,12 @@ impl Client {
/// ```
pub fn with_auth<S1, S2>(mut self, username: S1, password: S2) -> Self
where
S1: ToString,
S2: ToString,
S1: Into<String>,
S2: Into<String>,
{
self.auth = Some(Authentication {
username: username.to_string(),
password: password.to_string(),
username: username.into(),
password: password.into(),
});
self
}
Expand Down
8 changes: 4 additions & 4 deletions src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl dyn Query {
/// ```
pub fn write_query<S>(timestamp: Timestamp, measurement: S) -> WriteQuery
where
S: ToString,
S: Into<String>,
{
WriteQuery::new(timestamp, measurement)
}
Expand All @@ -115,7 +115,7 @@ impl dyn Query {
/// ```
pub fn raw_read_query<S>(read_query: S) -> ReadQuery
where
S: ToString,
S: Into<String>,
{
ReadQuery::new(read_query)
}
Expand All @@ -131,10 +131,10 @@ impl ValidQuery {
}
impl<T> From<T> for ValidQuery
where
T: ToString,
T: Into<String>,
{
fn from(string: T) -> Self {
Self(string.to_string())
Self(string.into())
}
}
impl PartialEq<String> for ValidQuery {
Expand Down
8 changes: 4 additions & 4 deletions src/query/read_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ impl ReadQuery {
/// Creates a new [`ReadQuery`]
pub fn new<S>(query: S) -> Self
where
S: ToString,
S: Into<String>,
{
ReadQuery {
queries: vec![query.to_string()],
queries: vec![query.into()],
}
}

/// Adds a query to the [`ReadQuery`]
pub fn add_query<S>(mut self, query: S) -> Self
where
S: ToString,
S: Into<String>,
{
self.queries.push(query.to_string());
self.queries.push(query.into());
self
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/query/write_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::query::{QueryType, ValidQuery};
use crate::{Error, Query, Timestamp};
use std::fmt::{Display, Formatter};

// todo: batch write queries

Expand All @@ -19,12 +20,12 @@ impl WriteQuery {
/// Creates a new [`WriteQuery`](crate::query::write_query::WriteQuery)
pub fn new<S>(timestamp: Timestamp, measurement: S) -> Self
where
S: ToString,
S: Into<String>,
{
WriteQuery {
fields: vec![],
tags: vec![],
measurement: measurement.to_string(),
measurement: measurement.into(),
timestamp,
}
}
Expand All @@ -40,11 +41,11 @@ impl WriteQuery {
/// ```
pub fn add_field<S, I>(mut self, tag: S, value: I) -> Self
where
S: ToString,
S: Into<String>,
I: Into<Type>,
{
let val: Type = value.into();
self.fields.push((tag.to_string(), val.to_string()));
self.fields.push((tag.into(), val.to_string()));
self
}

Expand All @@ -63,11 +64,11 @@ impl WriteQuery {
/// ```
pub fn add_tag<S, I>(mut self, tag: S, value: I) -> Self
where
S: ToString,
S: Into<String>,
I: Into<Type>,
{
let val: Type = value.into();
self.tags.push((tag.to_string(), val.to_string()));
self.tags.push((tag.into(), val.to_string()));
self
}

Expand All @@ -93,16 +94,16 @@ pub enum Type {
Text(String),
}

impl ToString for Type {
fn to_string(&self) -> String {
impl Display for Type {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
use Type::*;

match self {
Boolean(x) => x.to_string(),
Float(x) => x.to_string(),
SignedInteger(x) => x.to_string(),
UnsignedInteger(x) => x.to_string(),
Text(text) => format!("\"{text}\"", text = text),
Boolean(x) => write!(f, "{}", x),
Float(x) => write!(f, "{}", x),
SignedInteger(x) => write!(f, "{}", x),
UnsignedInteger(x) => write!(f, "{}", x),
Text(text) => write!(f, "\"{text}\"", text = text),
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn get_runtime() -> Runtime {

fn create_client<T>(db_name: T) -> Client
where
T: ToString,
T: Into<String>,
{
Client::new("http://localhost:8086", db_name)
}
Expand All @@ -35,19 +35,21 @@ impl Drop for RunOnDrop {
}
}

fn create_db<T>(test_name: T) -> Result<String, Error>
fn create_db<T>(name: T) -> Result<String, Error>
where
T: ToString,
T: Into<String>,
{
let query = format!("CREATE DATABASE {}", test_name.to_string());
let test_name = name.into();
let query = format!("CREATE DATABASE {}", test_name);
get_runtime().block_on(create_client(test_name).query(&Query::raw_read_query(query)))
}

fn delete_db<T>(test_name: T) -> Result<String, Error>
fn delete_db<T>(name: T) -> Result<String, Error>
where
T: ToString,
T: Into<String>,
{
let query = format!("DROP DATABASE {}", test_name.to_string());
let test_name = name.into();
let query = format!("DROP DATABASE {}", test_name);
get_runtime().block_on(create_client(test_name).query(&Query::raw_read_query(query)))
}

Expand Down