2
2
#[ cfg( feature = "serialize" ) ]
3
3
use serde:: { Deserialize , Serialize } ;
4
4
use std:: borrow:: Cow ;
5
+ use std:: fmt;
5
6
6
7
/// Key used for metric `LabelSet`s and trace `Span` attributes.
7
8
#[ cfg_attr( feature = "serialize" , derive( Deserialize , Serialize ) ) ]
@@ -52,7 +53,7 @@ impl Key {
52
53
}
53
54
54
55
/// Create a `KeyValue` pair for arrays.
55
- pub fn array < T : Into < Vec < Value > > > ( & self , value : T ) -> KeyValue {
56
+ pub fn array < T : Into < Array > > ( & self , value : T ) -> KeyValue {
56
57
KeyValue {
57
58
key : self . clone ( ) ,
58
59
value : Value :: Array ( value. into ( ) ) ,
@@ -86,6 +87,83 @@ impl From<Key> for String {
86
87
}
87
88
}
88
89
90
+ /// Array of homogeneous values
91
+ #[ cfg_attr( feature = "serialize" , derive( Deserialize , Serialize ) ) ]
92
+ #[ derive( Clone , Debug , PartialEq ) ]
93
+ pub enum Array {
94
+ /// Array of bools
95
+ Bool ( Vec < Option < bool > > ) ,
96
+ /// Array of integers
97
+ I64 ( Vec < Option < i64 > > ) ,
98
+ /// Array of floats
99
+ F64 ( Vec < Option < f64 > > ) ,
100
+ /// Array of strings
101
+ String ( Vec < Option < Cow < ' static , str > > > ) ,
102
+ }
103
+
104
+ impl fmt:: Display for Array {
105
+ fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
106
+ match self {
107
+ Array :: Bool ( values) => display_array_str ( & values, fmt) ,
108
+ Array :: I64 ( values) => display_array_str ( & values, fmt) ,
109
+ Array :: F64 ( values) => display_array_str ( & values, fmt) ,
110
+ Array :: String ( values) => {
111
+ write ! ( fmt, "[" ) ?;
112
+ for ( i, t) in values. iter ( ) . enumerate ( ) {
113
+ if i > 0 {
114
+ write ! ( fmt, "," ) ?;
115
+ }
116
+ match t {
117
+ Some ( t) => write ! ( fmt, "{:?}" , t) ?,
118
+ None => write ! ( fmt, "null" ) ?,
119
+ }
120
+ }
121
+ write ! ( fmt, "]" )
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ fn display_array_str < T : fmt:: Display > (
128
+ slice : & [ Option < T > ] ,
129
+ fmt : & mut fmt:: Formatter < ' _ > ,
130
+ ) -> fmt:: Result {
131
+ write ! ( fmt, "[" ) ?;
132
+ for ( i, t) in slice. iter ( ) . enumerate ( ) {
133
+ if i > 0 {
134
+ write ! ( fmt, "," ) ?;
135
+ }
136
+ match t {
137
+ Some ( t) => write ! ( fmt, "{}" , t) ?,
138
+ None => write ! ( fmt, "null" ) ?,
139
+ }
140
+ }
141
+ write ! ( fmt, "]" )
142
+ }
143
+
144
+ macro_rules! into_array {
145
+ ( $( ( $t: ty, $val: expr, $src: ident, $convert: expr) , ) +) => {
146
+ $(
147
+ impl From <$t> for Array {
148
+ fn from( $src: $t) -> Self {
149
+ $val( $convert)
150
+ }
151
+ }
152
+ ) +
153
+ }
154
+ }
155
+
156
+ into_array ! (
157
+ ( Vec <Option <bool >>, Array :: Bool , t, t) ,
158
+ ( Vec <Option <i64 >>, Array :: I64 , t, t) ,
159
+ ( Vec <Option <f64 >>, Array :: F64 , t, t) ,
160
+ ( Vec <Option <Cow <' static , str >>>, Array :: String , t, t) ,
161
+ ( Vec <bool >, Array :: Bool , t, t. into_iter( ) . map( Some ) . collect( ) ) ,
162
+ ( Vec <i64 >, Array :: I64 , t, t. into_iter( ) . map( Some ) . collect( ) ) ,
163
+ ( Vec <f64 >, Array :: F64 , t, t. into_iter( ) . map( Some ) . collect( ) ) ,
164
+ ( Vec <Cow <' static , str >>, Array :: String , t, t. into_iter( ) . map( Some ) . collect( ) ) ,
165
+ ) ;
166
+
89
167
/// Value types for use in `KeyValue` pairs.
90
168
#[ cfg_attr( feature = "serialize" , derive( Deserialize , Serialize ) ) ]
91
169
#[ derive( Clone , Debug , PartialEq ) ]
@@ -99,7 +177,7 @@ pub enum Value {
99
177
/// String values
100
178
String ( Cow < ' static , str > ) ,
101
179
/// Array of homogeneous values
102
- Array ( Vec < Value > ) ,
180
+ Array ( Array ) ,
103
181
}
104
182
105
183
macro_rules! from_values {
@@ -122,7 +200,7 @@ from_values!(
122
200
( bool , Value :: Bool ) ;
123
201
( i64 , Value :: I64 ) ;
124
202
( f64 , Value :: F64 ) ;
125
- ( Vec < Value >, Value :: Array ) ;
203
+ ( Cow < ' static , str >, Value :: String ) ;
126
204
) ;
127
205
128
206
impl From < & ' static str > for Value {
@@ -148,7 +226,7 @@ impl From<Value> for String {
148
226
Value :: I64 ( value) => value. to_string ( ) ,
149
227
Value :: F64 ( value) => value. to_string ( ) ,
150
228
Value :: String ( value) => value. into_owned ( ) ,
151
- Value :: Array ( value) => format_value_array_as_string ( & value) ,
229
+ Value :: Array ( value) => value. to_string ( ) ,
152
230
}
153
231
}
154
232
}
@@ -162,24 +240,11 @@ impl From<&Value> for String {
162
240
Value :: I64 ( value) => value. to_string ( ) ,
163
241
Value :: F64 ( value) => value. to_string ( ) ,
164
242
Value :: String ( value) => value. to_string ( ) ,
165
- Value :: Array ( value) => format_value_array_as_string ( value) ,
243
+ Value :: Array ( value) => value. to_string ( ) ,
166
244
}
167
245
}
168
246
}
169
247
170
- fn format_value_array_as_string ( v : & [ Value ] ) -> String {
171
- format ! (
172
- "[{}]" ,
173
- v. iter( )
174
- . map( |elem| match elem {
175
- Value :: String ( s) => format!( r#""{}""# , s) ,
176
- v => String :: from( v) ,
177
- } )
178
- . collect:: <Vec <_>>( )
179
- . join( "," )
180
- )
181
- }
182
-
183
248
/// `KeyValue` pairs are used by `LabelSet`s and `Span` attributes.
184
249
#[ cfg_attr( feature = "serialize" , derive( Deserialize , Serialize ) ) ]
185
250
#[ derive( Clone , Debug , PartialEq ) ]
0 commit comments