#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GraphMetadata {
pub created_at: u64,
pub version: u64,
pub description: String,
pub attributes: std::collections::HashMap<String, String>,
}
impl GraphMetadata {
pub fn new(description: &str) -> Self {
Self {
created_at: Self::current_timestamp(),
version: 1,
description: String::from(description),
attributes: std::collections::HashMap::new(),
}
}
pub fn with_version(description: &str, version: u64) -> Self {
Self {
created_at: Self::current_timestamp(),
version,
description: String::from(description),
attributes: std::collections::HashMap::new(),
}
}
fn current_timestamp() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
}
pub fn get_attribute(&self, key: &str) -> Option<&String> {
self.attributes.get(key)
}
}
impl Default for GraphMetadata {
fn default() -> Self {
Self::new("Hexagonal Architecture Graph")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_graph_metadata_creation() {
let metadata = GraphMetadata::new("Test Graph");
assert_eq!(metadata.description, "Test Graph");
assert_eq!(metadata.version, 1);
assert!(metadata.created_at > 0);
}
#[test]
fn test_graph_metadata_with_version() {
let metadata = GraphMetadata::with_version("Test", 42);
assert_eq!(metadata.version, 42);
}
#[test]
fn test_graph_metadata_default() {
let metadata = GraphMetadata::default();
assert!(metadata.description.contains("Hexagonal"));
}
}