1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//  Copyright 2017 Palantir Technologies, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

//! Trace IDs.
use data_encoding::{HEXLOWER_PERMISSIVE, DecodeError};
#[cfg(feature = "serde")]
use serde::{Serialize, Serializer};
use std::error::Error;
use std::fmt;
use std::str::FromStr;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Inner {
    Short([u8; 8]),
    Long([u8; 16]),
}

/// The ID of a trace.
///
/// Trace IDs are either 8 or 16 bytes, and are serialized as hexadecimal
/// strings.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct TraceId(Inner);

impl fmt::Display for TraceId {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        for b in self.bytes() {
            write!(fmt, "{:02x}", b)?;
        }
        Ok(())
    }
}

impl FromStr for TraceId {
    type Err = TraceIdParseError;

    fn from_str(s: &str) -> Result<TraceId, TraceIdParseError> {
        let inner = match HEXLOWER_PERMISSIVE.decode_len(s.len()) {
            Ok(8) => {
                let mut buf = [0; 8];
                HEXLOWER_PERMISSIVE
                    .decode_mut(s.as_bytes(), &mut buf)
                    .map_err(|e| TraceIdParseError(Some(e.error)))?;
                Inner::Short(buf)
            }
            Ok(16) => {
                let mut buf = [0; 16];
                HEXLOWER_PERMISSIVE
                    .decode_mut(s.as_bytes(), &mut buf)
                    .map_err(|e| TraceIdParseError(Some(e.error)))?;
                Inner::Long(buf)
            }
            _ => return Err(TraceIdParseError(None)),
        };

        Ok(TraceId(inner))
    }
}

#[cfg(feature = "serde")]
impl Serialize for TraceId {
    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        s.collect_str(self)
    }
}

impl TraceId {
    /// Returns the byte representation of the trace ID.
    pub fn bytes(&self) -> &[u8] {
        match self.0 {
            Inner::Short(ref buf) => buf,
            Inner::Long(ref buf) => buf,
        }
    }
}

impl From<[u8; 8]> for TraceId {
    fn from(bytes: [u8; 8]) -> TraceId {
        TraceId(Inner::Short(bytes))
    }
}

impl From<[u8; 16]> for TraceId {
    fn from(bytes: [u8; 16]) -> TraceId {
        TraceId(Inner::Long(bytes))
    }
}

/// The error returned when parsing a `TraceId` from a string.
#[derive(Debug)]
pub struct TraceIdParseError(Option<DecodeError>);

impl fmt::Display for TraceIdParseError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}: ", self.description())?;
        match self.0 {
            Some(ref err) => write!(fmt, "{}", err),
            None => fmt.write_str("invalid length"),
        }
    }
}

impl Error for TraceIdParseError {
    fn description(&self) -> &str {
        "error parsing trace ID"
    }

    fn cause(&self) -> Option<&Error> {
        match self.0 {
            Some(ref e) => Some(e),
            None => None,
        }
    }
}