1
+ use crate :: trace:: { TraceError , TraceResult } ;
1
2
use std:: collections:: VecDeque ;
2
3
use std:: fmt;
3
4
use std:: hash:: Hash ;
@@ -263,15 +264,15 @@ impl TraceState {
263
264
/// # Examples
264
265
///
265
266
/// ```
266
- /// use opentelemetry_api::trace::{ TraceState, TraceStateError} ;
267
+ /// use opentelemetry_api::trace::TraceState;
267
268
///
268
269
/// let kvs = vec![("foo", "bar"), ("apple", "banana")];
269
- /// let trace_state: Result<TraceState, TraceStateError> = TraceState::from_key_value(kvs);
270
+ /// let trace_state = TraceState::from_key_value(kvs);
270
271
///
271
272
/// assert!(trace_state.is_ok());
272
273
/// assert_eq!(trace_state.unwrap().header(), String::from("foo=bar,apple=banana"))
273
274
/// ```
274
- pub fn from_key_value < T , K , V > ( trace_state : T ) -> Result < Self , TraceStateError >
275
+ pub fn from_key_value < T , K , V > ( trace_state : T ) -> TraceResult < Self >
275
276
where
276
277
T : IntoIterator < Item = ( K , V ) > ,
277
278
K : ToString ,
@@ -282,10 +283,10 @@ impl TraceState {
282
283
. map ( |( key, value) | {
283
284
let ( key, value) = ( key. to_string ( ) , value. to_string ( ) ) ;
284
285
if !TraceState :: valid_key ( key. as_str ( ) ) {
285
- return Err ( TraceStateError :: InvalidKey ( key) ) ;
286
+ return Err ( TraceStateError :: Key ( key) ) ;
286
287
}
287
288
if !TraceState :: valid_value ( value. as_str ( ) ) {
288
- return Err ( TraceStateError :: InvalidValue ( value) ) ;
289
+ return Err ( TraceStateError :: Value ( value) ) ;
289
290
}
290
291
291
292
Ok ( ( key, value) )
@@ -318,17 +319,17 @@ impl TraceState {
318
319
/// updated key/value is returned.
319
320
///
320
321
/// [W3 Spec]: https://www.w3.org/TR/trace-context/#mutating-the-tracestate-field
321
- pub fn insert < K , V > ( & self , key : K , value : V ) -> Result < TraceState , TraceStateError >
322
+ pub fn insert < K , V > ( & self , key : K , value : V ) -> TraceResult < TraceState >
322
323
where
323
324
K : Into < String > ,
324
325
V : Into < String > ,
325
326
{
326
327
let ( key, value) = ( key. into ( ) , value. into ( ) ) ;
327
328
if !TraceState :: valid_key ( key. as_str ( ) ) {
328
- return Err ( TraceStateError :: InvalidKey ( key) ) ;
329
+ return Err ( TraceStateError :: Key ( key) . into ( ) ) ;
329
330
}
330
331
if !TraceState :: valid_value ( value. as_str ( ) ) {
331
- return Err ( TraceStateError :: InvalidValue ( value) ) ;
332
+ return Err ( TraceStateError :: Value ( value) . into ( ) ) ;
332
333
}
333
334
334
335
let mut trace_state = self . delete_from_deque ( key. clone ( ) ) ;
@@ -346,10 +347,10 @@ impl TraceState {
346
347
/// If the key is not in `TraceState`. The original `TraceState` will be cloned and returned.
347
348
///
348
349
/// [W3 Spec]: https://www.w3.org/TR/trace-context/#mutating-the-tracestate-field
349
- pub fn delete < K : Into < String > > ( & self , key : K ) -> Result < TraceState , TraceStateError > {
350
+ pub fn delete < K : Into < String > > ( & self , key : K ) -> TraceResult < TraceState > {
350
351
let key = key. into ( ) ;
351
352
if !TraceState :: valid_key ( key. as_str ( ) ) {
352
- return Err ( TraceStateError :: InvalidKey ( key) ) ;
353
+ return Err ( TraceStateError :: Key ( key) . into ( ) ) ;
353
354
}
354
355
355
356
Ok ( self . delete_from_deque ( key) )
@@ -387,15 +388,15 @@ impl TraceState {
387
388
}
388
389
389
390
impl FromStr for TraceState {
390
- type Err = TraceStateError ;
391
+ type Err = TraceError ;
391
392
392
393
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
393
394
let list_members: Vec < & str > = s. split_terminator ( ',' ) . collect ( ) ;
394
395
let mut key_value_pairs: Vec < ( String , String ) > = Vec :: with_capacity ( list_members. len ( ) ) ;
395
396
396
397
for list_member in list_members {
397
398
match list_member. find ( '=' ) {
398
- None => return Err ( TraceStateError :: InvalidList ( list_member. to_string ( ) ) ) ,
399
+ None => return Err ( TraceStateError :: List ( list_member. to_string ( ) ) . into ( ) ) ,
399
400
Some ( separator_index) => {
400
401
let ( key, value) = list_member. split_at ( separator_index) ;
401
402
key_value_pairs
@@ -411,18 +412,30 @@ impl FromStr for TraceState {
411
412
/// Error returned by `TraceState` operations.
412
413
#[ derive( Error , Debug ) ]
413
414
#[ non_exhaustive]
414
- pub enum TraceStateError {
415
- /// The key is invalid. See <https://www.w3.org/TR/trace-context/#key> for requirement for keys.
415
+ enum TraceStateError {
416
+ /// The key is invalid.
417
+ ///
418
+ /// See <https://www.w3.org/TR/trace-context/#key> for requirement for keys.
416
419
#[ error( "{0} is not a valid key in TraceState, see https://www.w3.org/TR/trace-context/#key for more details" ) ]
417
- InvalidKey ( String ) ,
420
+ Key ( String ) ,
418
421
419
- /// The value is invalid. See <https://www.w3.org/TR/trace-context/#value> for requirement for values.
422
+ /// The value is invalid.
423
+ ///
424
+ /// See <https://www.w3.org/TR/trace-context/#value> for requirement for values.
420
425
#[ error( "{0} is not a valid value in TraceState, see https://www.w3.org/TR/trace-context/#value for more details" ) ]
421
- InvalidValue ( String ) ,
426
+ Value ( String ) ,
422
427
423
- /// The value is invalid. See <https://www.w3.org/TR/trace-context/#list> for requirement for list members.
428
+ /// The list is invalid.
429
+ ///
430
+ /// See <https://www.w3.org/TR/trace-context/#list> for requirement for list members.
424
431
#[ error( "{0} is not a valid list member in TraceState, see https://www.w3.org/TR/trace-context/#list for more details" ) ]
425
- InvalidList ( String ) ,
432
+ List ( String ) ,
433
+ }
434
+
435
+ impl From < TraceStateError > for TraceError {
436
+ fn from ( err : TraceStateError ) -> Self {
437
+ TraceError :: Other ( Box :: new ( err) )
438
+ }
426
439
}
427
440
428
441
/// Immutable portion of a [`Span`] which can be serialized and propagated.
0 commit comments