use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Fan {
pub hyper_edge_id: String,
pub parent: u32,
pub children: HashMap<String, u32>,
}
impl Fan {
#[must_use]
pub fn new(hyper_edge_id: impl Into<String>, parent: u32) -> Self {
Self {
hyper_edge_id: hyper_edge_id.into(),
parent,
children: HashMap::new(),
}
}
#[must_use]
pub fn with_child(mut self, label: impl Into<String>, node_id: u32) -> Self {
self.children.insert(label.into(), node_id);
self
}
#[must_use]
pub fn arity(&self) -> usize {
self.children.len()
}
#[must_use]
pub fn child(&self, label: &str) -> Option<u32> {
self.children.get(label).copied()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fan_construction() {
let fan = Fan::new("fk_posts_users", 0)
.with_child("table", 1)
.with_child("column", 2)
.with_child("ref_table", 3)
.with_child("ref_column", 4);
assert_eq!(fan.arity(), 4);
assert_eq!(fan.child("table"), Some(1));
assert_eq!(fan.child("column"), Some(2));
assert_eq!(fan.child("missing"), None);
}
}