@@ -4,25 +4,23 @@ use std::{borrow::Cow, collections::HashMap, sync::OnceLock};
4
4
5
5
use crate :: metrics:: TagMap ;
6
6
7
- pub struct NormalizedTags < ' a > {
8
- tags : HashMap < Cow < ' a , str > , String > ,
7
+ pub fn normalize_tags ( tags : & TagMap ) -> NormalizedTags {
8
+ NormalizedTags {
9
+ tags : tags
10
+ . iter ( )
11
+ . map ( |( k, v) | {
12
+ (
13
+ NormalizedTags :: normalize_key ( super :: truncate ( k, 32 ) ) ,
14
+ NormalizedTags :: normalize_value ( super :: truncate ( v, 200 ) ) ,
15
+ )
16
+ } )
17
+ . filter ( |( k, v) | !k. is_empty ( ) && !v. is_empty ( ) )
18
+ . collect ( ) ,
19
+ }
9
20
}
10
21
11
- impl < ' a > From < & ' a TagMap > for NormalizedTags < ' a > {
12
- fn from ( tags : & ' a TagMap ) -> Self {
13
- Self {
14
- tags : tags
15
- . iter ( )
16
- . map ( |( k, v) | {
17
- (
18
- Self :: normalize_key ( super :: truncate ( k, 32 ) ) ,
19
- Self :: normalize_value ( super :: truncate ( v, 200 ) ) ,
20
- )
21
- } )
22
- . filter ( |( k, v) | !k. is_empty ( ) && !v. is_empty ( ) )
23
- . collect ( ) ,
24
- }
25
- }
22
+ pub struct NormalizedTags < ' a > {
23
+ tags : HashMap < Cow < ' a , str > , String > ,
26
24
}
27
25
28
26
impl < ' a > NormalizedTags < ' a > {
@@ -76,7 +74,6 @@ impl std::fmt::Display for NormalizedTags<'_> {
76
74
77
75
#[ cfg( test) ]
78
76
mod test {
79
- use super :: NormalizedTags ;
80
77
use super :: TagMap ;
81
78
82
79
#[ test]
@@ -95,7 +92,7 @@ mod test {
95
92
) ;
96
93
let expected = "aa:a\\ na,bb:b\\ rb,cc:c\\ tc,dd:d\\ \\ d,ee:e\\ u{7c}e,ff:f\\ u{2c}f" ;
97
94
98
- let actual = NormalizedTags :: from ( & tags) . to_string ( ) ;
95
+ let actual = super :: normalize_tags ( & tags) . to_string ( ) ;
99
96
100
97
assert_eq ! ( expected, actual) ;
101
98
}
@@ -109,7 +106,7 @@ mod test {
109
106
) ;
110
107
let expected = "" ;
111
108
112
- let actual = NormalizedTags :: from ( & tags) . to_string ( ) ;
109
+ let actual = super :: normalize_tags ( & tags) . to_string ( ) ;
113
110
114
111
assert_eq ! ( expected, actual) ;
115
112
}
@@ -119,7 +116,7 @@ mod test {
119
116
let tags = TagMap :: from ( [ ( "aA1_-./+ö{ 😀" . into ( ) , "aA1_-./+ö{ 😀" . into ( ) ) ] ) ;
120
117
let expected = "aA1_-./:aA1_-./+ö{ 😀" ;
121
118
122
- let actual = NormalizedTags :: from ( & tags) . to_string ( ) ;
119
+ let actual = super :: normalize_tags ( & tags) . to_string ( ) ;
123
120
124
121
assert_eq ! ( expected, actual) ;
125
122
}
@@ -132,7 +129,7 @@ mod test {
132
129
] ) ;
133
130
let expected = "environment:production,release:default_release" ;
134
131
135
- let actual = NormalizedTags :: from ( & TagMap :: new ( ) )
132
+ let actual = super :: normalize_tags ( & TagMap :: new ( ) )
136
133
. with_default_tags ( & default_tags)
137
134
. to_string ( ) ;
138
135
@@ -147,7 +144,7 @@ mod test {
147
144
] ) ;
148
145
let expected = "environment:custom_env,release:custom_release" ;
149
146
150
- let actual = NormalizedTags :: from ( & TagMap :: from ( [
147
+ let actual = super :: normalize_tags ( & TagMap :: from ( [
151
148
( "release" . into ( ) , "custom_release" . into ( ) ) ,
152
149
( "environment" . into ( ) , "custom_env" . into ( ) ) ,
153
150
] ) )
@@ -167,7 +164,7 @@ mod test {
167
164
+ ":"
168
165
+ "v" . repeat ( 200 ) . as_str ( ) ;
169
166
170
- let actual = NormalizedTags :: from ( & TagMap :: from ( [ (
167
+ let actual = super :: normalize_tags ( & TagMap :: from ( [ (
171
168
"k" . repeat ( 35 ) . into ( ) ,
172
169
"v" . repeat ( 210 ) . into ( ) ,
173
170
) ] ) )
0 commit comments