influxdb_client/
models.rs1use std::fmt::Write;
2
3use crate::traits::PointSerialize;
4
5#[derive(Debug, Clone)]
6pub enum Value {
7 Str(String),
8 Int(i64),
9 Float(f64),
10 Bool(bool),
11}
12
13impl From<&str> for Value {
14 fn from(v: &str) -> Value {
15 Value::Str(v.to_string())
16 }
17}
18
19impl From<f64> for Value {
20 fn from(v: f64) -> Value {
21 Value::Float(v)
22 }
23}
24
25impl From<i64> for Value {
26 fn from(v: i64) -> Value {
27 Value::Int(v)
28 }
29}
30
31impl From<bool> for Value {
32 fn from(v: bool) -> Value {
33 Value::Bool(v)
34 }
35}
36
37impl std::string::ToString for Value {
38 fn to_string(&self) -> String {
39 match self {
40 Value::Str(s) => s.to_string(),
41 Value::Int(i) => i.to_string(),
42 Value::Float(f) => f.to_string(),
43 Value::Bool(b) => b.to_string(),
44 }
45 }
46}
47
48#[derive(Debug, Clone)]
49pub enum Timestamp {
50 Str(String),
51 Int(i64),
52}
53
54impl From<&str> for Timestamp {
55 fn from(v: &str) -> Timestamp {
56 Timestamp::Str(v.to_string())
57 }
58}
59
60impl From<i64> for Timestamp {
61 fn from(v: i64) -> Timestamp {
62 Timestamp::Int(v)
63 }
64}
65
66impl std::string::ToString for Timestamp {
67 fn to_string(&self) -> String {
68 match self {
69 Timestamp::Str(s) => s.to_string(),
70 Timestamp::Int(i) => i.to_string(),
71 }
72 }
73}
74
75#[derive(Debug, Clone)]
76pub struct Point {
77 pub measurement: String,
78 pub timestamp: Option<Timestamp>,
79 pub tags: Vec<(String, Value)>,
80 pub fields: Vec<(String, Value)>,
81}
82
83impl Point {
84 pub fn new<T: Into<String>>(measurement: T) -> Self {
85 Point {
86 measurement: measurement.into(),
87 tags: Vec::new(),
88 fields: Vec::new(),
89 timestamp: None,
90 }
91 }
92
93 pub fn tag<T: Into<String>, V: Into<Value>>(mut self, key: T, value: V) -> Self {
94 self.tags.push((key.into(), value.into()));
95 self
96 }
97
98 pub fn field<T: Into<String>, V: Into<Value>>(mut self, key: T, value: V) -> Self {
99 self.fields.push((key.into(), value.into()));
100 self
101 }
102
103 pub fn timestamp<T: Into<Timestamp>>(mut self, timestamp: T) -> Self {
104 self.timestamp = Some(timestamp.into());
105 self
106 }
107}
108
109impl PointSerialize for Point {
110 fn serialize(&self) -> String {
111 let mut builder = String::new();
112
113 write!(&mut builder, "{}", self.measurement).unwrap();
115
116 if !self.tags.is_empty() {
118 write!(&mut builder, ",").unwrap();
119
120 let tags = self
121 .tags
122 .iter()
123 .map(|field| format!("{}={}", field.0, field.1.to_string()))
124 .collect::<Vec<String>>()
125 .join(",")
126 .clone();
127
128 builder.push_str(&tags);
129 }
130
131 if !self.fields.is_empty() {
133 write!(&mut builder, " ").unwrap();
134
135 let fields = self
136 .fields
137 .iter()
138 .map(|field| {
139 format!(
140 "{}={}",
141 field.0,
142 match field.1.clone() {
143 Value::Str(s) => format!("\"{}\"", s),
144 _ => field.1.to_string(),
145 }
146 )
147 })
148 .collect::<Vec<String>>()
149 .join(",")
150 .clone();
151
152 builder.push_str(&fields);
153 }
154
155 builder
156 }
157
158 fn serialize_with_timestamp(&self, timestamp: Option<Timestamp>) -> String {
159 match timestamp {
160 Some(timestamp) => format!("{} {}", self.serialize(), timestamp.to_string()),
161 None => format!(
162 "{} {}",
163 self.serialize(),
164 self.timestamp
165 .clone()
166 .unwrap_or(Timestamp::from(0))
167 .to_string()
168 ),
169 }
170 }
171}
172
173#[derive(Debug)]
174pub enum InfluxError {
175 InvalidSyntax(String),
176 InvalidCredentials(String),
177 Forbidden(String),
178 Unknown(String),
179}
180
181#[derive(Clone)]
182pub enum TimestampOptions {
183 None,
184 Use(Timestamp),
185 FromPoint,
186}
187
188pub enum Precision {
189 NS,
190 US,
191 MS,
192 S,
193}
194
195impl Precision {
196
197 pub fn to_string(&self) -> &str {
198 match self {
199 Precision::NS => "ns",
200 Precision::US => "us",
201 Precision::MS => "ms",
202 Precision::S => "s",
203 }
204 }
205
206}