use serde::{Deserialize, Serialize};
#[deprecated(
since = "0.6.0",
note = "Use stack_ids::TraceCtx instead. This type will be removed in v1.0. Retained for backward compatibility only."
)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TraceId(pub String);
impl TraceId {
pub fn new() -> Self {
Self(uuid::Uuid::new_v4().to_string())
}
pub fn from_string(s: &str) -> Self {
Self(s.to_owned())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn to_trace_ctx(&self) -> stack_ids::TraceCtx {
stack_ids::TraceCtx::from_legacy_trace_id(&self.0)
}
pub fn from_trace_ctx(ctx: &stack_ids::TraceCtx) -> Self {
Self(ctx.to_legacy_trace_id().to_string())
}
}
impl Default for TraceId {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Display for TraceId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for TraceId {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for TraceId {
fn from(s: &str) -> Self {
Self(s.to_owned())
}
}
#[allow(deprecated)]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trace_id_new_is_unique() {
let id1 = TraceId::new();
let id2 = TraceId::new();
assert_ne!(id1, id2);
}
#[test]
fn test_trace_id_from_string() {
let id = TraceId::from_string("abc-123");
assert_eq!(id.as_str(), "abc-123");
assert_eq!(id.to_string(), "abc-123");
}
#[test]
fn test_trace_id_display() {
let id = TraceId::from_string("test-id");
assert_eq!(format!("{}", id), "test-id");
}
#[test]
fn test_trace_id_default() {
let id = TraceId::default();
assert!(!id.0.is_empty());
}
#[test]
fn test_trace_id_serde_roundtrip() {
let id = TraceId::from_string("serde-test");
let json = serde_json::to_string(&id).unwrap();
let back: TraceId = serde_json::from_str(&json).unwrap();
assert_eq!(id, back);
}
#[test]
fn test_trace_id_from_conversions() {
let from_str: TraceId = "hello".into();
assert_eq!(from_str.as_str(), "hello");
let from_string: TraceId = String::from("world").into();
assert_eq!(from_string.as_str(), "world");
}
}