use crate::error::Result;
use serde::{Deserialize, Serialize};
use std::fmt;
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ForkId(Uuid);
impl ForkId {
pub fn new() -> Self {
Self(Uuid::new_v4())
}
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
pub fn parse(s: &str) -> Result<Self> {
let uuid = Uuid::parse_str(s).map_err(|e| {
crate::error::AllSourceError::InvalidInput(format!("Invalid ForkId: {e}"))
})?;
Ok(Self(uuid))
}
pub fn as_str(&self) -> String {
self.0.to_string()
}
pub fn as_uuid(&self) -> &Uuid {
&self.0
}
pub fn into_uuid(self) -> Uuid {
self.0
}
}
impl Default for ForkId {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for ForkId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl TryFrom<&str> for ForkId {
type Error = crate::error::AllSourceError;
fn try_from(value: &str) -> Result<Self> {
ForkId::parse(value)
}
}
impl TryFrom<String> for ForkId {
type Error = crate::error::AllSourceError;
fn try_from(value: String) -> Result<Self> {
ForkId::parse(&value)
}
}
impl From<Uuid> for ForkId {
fn from(uuid: Uuid) -> Self {
Self::from_uuid(uuid)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_fork_id() {
let fork_id = ForkId::new();
assert!(!fork_id.as_str().is_empty());
}
#[test]
fn test_fork_id_uniqueness() {
let id1 = ForkId::new();
let id2 = ForkId::new();
assert_ne!(id1, id2);
}
#[test]
fn test_parse_valid_uuid() {
let uuid_str = "550e8400-e29b-41d4-a716-446655440000";
let fork_id = ForkId::parse(uuid_str);
assert!(fork_id.is_ok());
assert_eq!(fork_id.unwrap().as_str(), uuid_str);
}
#[test]
fn test_parse_invalid_uuid() {
let result = ForkId::parse("not-a-uuid");
assert!(result.is_err());
}
#[test]
fn test_from_uuid() {
let uuid = Uuid::new_v4();
let fork_id = ForkId::from_uuid(uuid);
assert_eq!(fork_id.as_uuid(), &uuid);
}
#[test]
fn test_display() {
let fork_id = ForkId::parse("550e8400-e29b-41d4-a716-446655440000").unwrap();
assert_eq!(format!("{fork_id}"), "550e8400-e29b-41d4-a716-446655440000");
}
#[test]
fn test_equality() {
let uuid_str = "550e8400-e29b-41d4-a716-446655440000";
let id1 = ForkId::parse(uuid_str).unwrap();
let id2 = ForkId::parse(uuid_str).unwrap();
assert_eq!(id1, id2);
}
#[test]
fn test_try_from_str() {
let fork_id: Result<ForkId> = "550e8400-e29b-41d4-a716-446655440000".try_into();
assert!(fork_id.is_ok());
}
#[test]
fn test_try_from_string() {
let fork_id: Result<ForkId> = "550e8400-e29b-41d4-a716-446655440000"
.to_string()
.try_into();
assert!(fork_id.is_ok());
}
#[test]
fn test_hash_consistency() {
use std::collections::HashSet;
let uuid_str = "550e8400-e29b-41d4-a716-446655440000";
let id1 = ForkId::parse(uuid_str).unwrap();
let id2 = ForkId::parse(uuid_str).unwrap();
let mut set = HashSet::new();
set.insert(id1);
assert!(set.contains(&id2));
}
#[test]
fn test_serde_serialization() {
let fork_id = ForkId::parse("550e8400-e29b-41d4-a716-446655440000").unwrap();
let json = serde_json::to_string(&fork_id).unwrap();
assert!(json.contains("550e8400-e29b-41d4-a716-446655440000"));
let deserialized: ForkId = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, fork_id);
}
#[test]
fn test_into_uuid() {
let uuid = Uuid::new_v4();
let fork_id = ForkId::from_uuid(uuid);
let extracted = fork_id.into_uuid();
assert_eq!(extracted, uuid);
}
#[test]
fn test_default() {
let fork_id = ForkId::default();
assert!(!fork_id.as_str().is_empty());
}
}