systemprompt_identifiers/
trace.rs

1//! Trace identifier type.
2
3use crate::{DbValue, ToDbValue};
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use std::fmt;
7
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
9#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
10#[cfg_attr(feature = "sqlx", sqlx(transparent))]
11#[serde(transparent)]
12pub struct TraceId(String);
13
14impl TraceId {
15    pub fn new(id: impl Into<String>) -> Self {
16        Self(id.into())
17    }
18
19    pub fn system() -> Self {
20        Self("system".to_string())
21    }
22
23    pub fn generate() -> Self {
24        Self(uuid::Uuid::new_v4().to_string())
25    }
26
27    pub fn as_str(&self) -> &str {
28        &self.0
29    }
30}
31
32impl fmt::Display for TraceId {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        write!(f, "{}", self.0)
35    }
36}
37
38impl From<String> for TraceId {
39    fn from(s: String) -> Self {
40        Self(s)
41    }
42}
43
44impl From<&str> for TraceId {
45    fn from(s: &str) -> Self {
46        Self(s.to_string())
47    }
48}
49
50impl AsRef<str> for TraceId {
51    fn as_ref(&self) -> &str {
52        &self.0
53    }
54}
55
56impl ToDbValue for TraceId {
57    fn to_db_value(&self) -> DbValue {
58        DbValue::String(self.0.clone())
59    }
60}
61
62impl ToDbValue for &TraceId {
63    fn to_db_value(&self) -> DbValue {
64        DbValue::String(self.0.clone())
65    }
66}