@@ -37,41 +37,26 @@ impl LineProtoTerm<'_> {
37
37
}
38
38
}
39
39
40
+ /// Serializes Field Values.
40
41
fn escape_field_value ( v : & Type , use_v2 : bool ) -> String {
41
42
use Type :: * ;
42
43
match v {
43
- Boolean ( v) => {
44
- if * v {
45
- "true"
46
- } else {
47
- "false"
48
- }
49
- }
50
- . to_string ( ) ,
51
- Float ( v) => v. to_string ( ) ,
52
- SignedInteger ( v) => format ! ( "{}i" , v) ,
53
- UnsignedInteger ( v) => {
54
- if use_v2 {
55
- format ! ( "{}u" , v)
56
- } else {
57
- format ! ( "{}i" , v)
58
- }
59
- }
44
+ Boolean ( v) => Self :: escape_boolean ( v) ,
45
+ Float ( v) => Self :: escape_float ( v) ,
46
+ SignedInteger ( v) => Self :: escape_signed_integer ( v) ,
47
+ UnsignedInteger ( v) => Self :: escape_unsigned_integer ( v, use_v2) ,
60
48
Text ( v) => format ! ( r#""{}""# , Self :: escape_any( v, & QUOTES_SLASHES ) ) ,
61
49
}
62
50
}
63
51
52
+ /// Serializes Tag Values. InfluxDB stores tag values as strings, so we format everything to string.
53
+ ///
54
+ /// V2: https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#tag-set
55
+ /// V1: https://docs.influxdata.com/influxdb/v1/write_protocols/line_protocol_tutorial/#data-types
64
56
fn escape_tag_value ( v : & Type ) -> String {
65
57
use Type :: * ;
66
58
match v {
67
- Boolean ( v) => {
68
- if * v {
69
- "true"
70
- } else {
71
- "false"
72
- }
73
- }
74
- . to_string ( ) ,
59
+ Boolean ( v) => Self :: escape_boolean ( v) ,
75
60
Float ( v) => format ! ( r#"{}"# , v) ,
76
61
SignedInteger ( v) => format ! ( r#"{}"# , v) ,
77
62
UnsignedInteger ( v) => format ! ( r#"{}"# , v) ,
@@ -82,6 +67,51 @@ impl LineProtoTerm<'_> {
82
67
fn escape_any ( s : & str , re : & Regex ) -> String {
83
68
re. replace_all ( s, r"\$0" ) . to_string ( )
84
69
}
70
+
71
+ /// Escapes a Rust f64 to InfluxDB Line Protocol
72
+ ///
73
+ /// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#float
74
+ /// IEEE-754 64-bit floating-point numbers. Default numerical type.
75
+ /// InfluxDB supports scientific notation in float field values, but this crate does not serialize them.
76
+ fn escape_float ( v : & f64 ) -> String {
77
+ v. to_string ( )
78
+ }
79
+
80
+ /// Escapes a Rust bool to InfluxDB Line Protocol
81
+ ///
82
+ /// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#boolean
83
+ /// Stores true or false values.
84
+ fn escape_boolean ( v : & bool ) -> String {
85
+ if * v {
86
+ "true"
87
+ } else {
88
+ "false"
89
+ }
90
+ . to_string ( )
91
+ }
92
+
93
+ /// Escapes a Rust i64 to InfluxDB Line Protocol
94
+ ///
95
+ /// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#integer
96
+ /// Signed 64-bit integers. Trailing i on the number specifies an integer.
97
+ fn escape_signed_integer ( v : & i64 ) -> String {
98
+ format ! ( "{}i" , v)
99
+ }
100
+
101
+ /// Escapes a Rust u64 to InfluxDB Line Protocol
102
+ ///
103
+ /// https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/#uinteger
104
+ /// Unsigned 64-bit integers. Trailing u on the number specifies an unsigned integer.
105
+ ///
106
+ /// InfluxDB version 1 does not know unsigned, we fallback to (signed) integer:
107
+ /// https://docs.influxdata.com/influxdb/v1/write_protocols/line_protocol_tutorial/#data-types
108
+ fn escape_unsigned_integer ( v : & u64 , use_v2 : bool ) -> String {
109
+ if use_v2 {
110
+ format ! ( "{}u" , v)
111
+ } else {
112
+ format ! ( "{}i" , v)
113
+ }
114
+ }
85
115
}
86
116
87
117
#[ cfg( test) ]
0 commit comments