#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GraphNode {
pub name: &'static str,
pub dependencies: &'static [&'static str],
pub scope: &'static str,
}
impl GraphNode {
pub const fn new(name: &'static str, dependencies: &'static [&'static str]) -> Self {
Self {
name,
dependencies,
scope: "singleton",
}
}
pub const fn leaf(name: &'static str) -> Self {
Self {
name,
dependencies: &[],
scope: "singleton",
}
}
pub const fn with_scope(
name: &'static str,
dependencies: &'static [&'static str],
scope: &'static str,
) -> Self {
Self {
name,
dependencies,
scope,
}
}
pub const fn leaf_with_scope(name: &'static str, scope: &'static str) -> Self {
Self {
name,
dependencies: &[],
scope,
}
}
pub fn is_leaf(&self) -> bool {
self.dependencies.is_empty()
}
pub fn dependency_count(&self) -> usize {
self.dependencies.len()
}
pub fn is_singleton(&self) -> bool {
self.scope == "singleton"
}
pub fn is_transient(&self) -> bool {
self.scope == "transient"
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn leaf_node() {
let n = GraphNode::leaf("Database");
assert_eq!(n.name, "Database");
assert!(n.is_leaf());
assert_eq!(n.dependency_count(), 0);
assert!(n.is_singleton());
assert!(!n.is_transient());
}
#[test]
fn new_node_with_deps() {
let n = GraphNode::new("UserService", &["Database", "Cache"]);
assert_eq!(n.name, "UserService");
assert!(!n.is_leaf());
assert_eq!(n.dependency_count(), 2);
assert!(n.is_singleton());
}
#[test]
fn with_scope_transient() {
let n = GraphNode::with_scope("Logger", &["Config"], "transient");
assert!(n.is_transient());
assert!(!n.is_singleton());
assert_eq!(n.dependency_count(), 1);
}
#[test]
fn leaf_with_scope_singleton() {
let n = GraphNode::leaf_with_scope("Config", "singleton");
assert!(n.is_leaf());
assert!(n.is_singleton());
assert!(!n.is_transient());
}
#[test]
fn leaf_with_scope_transient() {
let n = GraphNode::leaf_with_scope("Req", "transient");
assert!(n.is_transient());
assert!(!n.is_singleton());
}
#[test]
fn derive_traits() {
let a = GraphNode::leaf("A");
let b = GraphNode::leaf("A");
let c = GraphNode::leaf("B");
assert_eq!(a, b);
assert_ne!(a, c);
let _ = format!("{a:?}");
}
}