#![doc(html_root_url = "https://docs.rs/incipit/0.0.1")]
use serde::{Deserialize, Serialize};
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const STAGE: &str = "pre-alpha";
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GraphId(pub uuid::Uuid);
impl GraphId {
pub fn new() -> Self {
Self(uuid::Uuid::new_v4())
}
pub fn from_uuid(uuid: uuid::Uuid) -> Self {
Self(uuid)
}
pub fn nil() -> Self {
Self(uuid::Uuid::nil())
}
pub fn as_uuid(&self) -> &uuid::Uuid {
&self.0
}
}
impl Default for GraphId {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SessionId(pub uuid::Uuid);
impl SessionId {
pub fn new() -> Self {
Self(uuid::Uuid::new_v4())
}
pub fn from_uuid(uuid: uuid::Uuid) -> Self {
Self(uuid)
}
pub fn as_uuid(&self) -> &uuid::Uuid {
&self.0
}
}
impl Default for SessionId {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn graph_and_session_ids_are_distinct_types_over_the_same_uuid() {
let uuid = uuid::Uuid::new_v4();
let graph = GraphId::from_uuid(uuid);
let session = SessionId::from_uuid(uuid);
assert_eq!(graph.as_uuid(), session.as_uuid());
}
#[test]
fn nil_graph_id_is_the_unbound_marker() {
assert_eq!(GraphId::nil().as_uuid(), &uuid::Uuid::nil());
assert_ne!(GraphId::nil(), GraphId::new());
}
#[test]
fn ids_round_trip_through_serde() {
let graph = GraphId::new();
let json = serde_json::to_string(&graph).expect("serialize");
let back: GraphId = serde_json::from_str(&json).expect("deserialize");
assert_eq!(graph, back);
}
}