use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct CompositeId(String);
impl CompositeId {
pub fn new(id: impl Into<String>) -> Self {
CompositeId(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for CompositeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for CompositeId {
fn from(s: String) -> Self {
CompositeId(s)
}
}
impl From<&str> for CompositeId {
fn from(s: &str) -> Self {
CompositeId(s.to_string())
}
}
impl AsRef<str> for CompositeId {
fn as_ref(&self) -> &str {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trips_through_serde_as_a_plain_string() {
let id = CompositeId::new("ai_map_reduce:digest");
let json = serde_json::to_string(&id).unwrap();
assert_eq!(json, "\"ai_map_reduce:digest\"");
let back: CompositeId = serde_json::from_str(&json).unwrap();
assert_eq!(id, back);
}
#[test]
fn display_is_the_bare_subgraph_id() {
assert_eq!(
CompositeId::from("ai_map_reduce:digest").to_string(),
"ai_map_reduce:digest"
);
}
}