1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use ::measurement::Measurement;
use std::io;

#[cfg(feature = "http")]
pub mod http;
pub mod udp;

pub trait Client {
    fn write_many(&self, &[Measurement], Option<Precision>) -> ClientWriteResult;
    fn write_one(&self, Measurement, Option<Precision>) -> ClientWriteResult;
    fn query(&self, String, Option<Precision>) -> ClientReadResult;
}

pub struct Credentials<'a> {
    pub username: &'a str,
    pub password: &'a str,
    pub database: &'a str
}

pub enum Precision {
    Nanoseconds,
    Microseconds,
    Milliseconds,
    Seconds,
    Minutes,
    Hours
}

impl ToString for Precision {
    fn to_string(&self) -> String {
        let s = match (*self) {
            Precision::Nanoseconds  => "n",
            Precision::Microseconds => "u",
            Precision::Milliseconds => "ms",
            Precision::Seconds      => "s",
            Precision::Minutes      => "m",
            Precision::Hours        => "h"
        };

        s.to_string()
    }
}

pub type ClientWriteResult = Result<(), ClientError>;

// TODO: here parsing json?
pub type ClientReadResult = Result<String, ClientError>;

#[derive(Debug)]
pub enum ClientError {
    CouldNotComplete(String),
    Communication(String),
    Syntax(String),
    Unexpected(String),
    Unknown
}

impl From<io::Error> for ClientError {
    fn from(e: io::Error) -> Self {
        ClientError::Communication(format!("{}", e))
    }
}