use std::fmt;
use std::str::FromStr;
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("invalid id: {0}")]
pub struct IdError(String);
macro_rules! uuid_id {
($name:ident) => {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct $name(String);
impl $name {
pub fn new() -> Self {
Self(Uuid::new_v4().to_string())
}
pub fn parse(value: &str) -> Result<Self, IdError> {
Uuid::parse_str(value).map_err(|error| IdError(error.to_string()))?;
Ok(Self(value.to_string()))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for $name {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.0)
}
}
impl FromStr for $name {
type Err = IdError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Self::parse(value)
}
}
impl From<$name> for String {
fn from(id: $name) -> Self {
id.0
}
}
impl serde::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.0)
}
}
impl<'de> serde::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
value.parse().map_err(serde::de::Error::custom)
}
}
impl rusqlite::ToSql for $name {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
Ok(rusqlite::types::ToSqlOutput::from(self.0.as_str()))
}
}
impl rusqlite::types::FromSql for $name {
fn column_result(
value: rusqlite::types::ValueRef<'_>,
) -> rusqlite::types::FromSqlResult<Self> {
value
.as_str()?
.parse()
.map_err(|error| rusqlite::types::FromSqlError::Other(Box::new(error)))
}
}
};
}
uuid_id!(WaveId);
uuid_id!(TraceId);
uuid_id!(ExecId);
#[cfg(test)]
mod tests {
use super::{ExecId, TraceId, WaveId};
#[test]
fn ids_round_trip_as_uuid_strings() {
let wave = WaveId::new();
let encoded = serde_json::to_string(&wave).unwrap();
assert_eq!(serde_json::from_str::<WaveId>(&encoded).unwrap(), wave);
let _trace = TraceId::new();
let _exec = ExecId::new();
}
}