use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NavigationDirection {
Covariant,
Contravariant,
Invariant,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CardNode {
pub hash: String,
pub handle: Option<String>,
pub card_type: String,
pub content_hash: String,
pub upstream_vcards: Vec<String>,
pub downstream_pcards: Vec<String>,
}
pub struct CardGraph {
pub nodes: HashMap<String, CardNode>,
}
impl Default for CardGraph {
fn default() -> Self {
Self::new()
}
}
impl CardGraph {
pub fn new() -> Self {
Self {
nodes: HashMap::new(),
}
}
pub fn insert_node(&mut self, node: CardNode) {
self.nodes.insert(node.hash.clone(), node);
}
pub fn navigate(&self, start_hash: &str, direction: NavigationDirection) -> Vec<String> {
let node = match self.nodes.get(start_hash) {
Some(n) => n,
None => return Vec::new(),
};
match direction {
NavigationDirection::Covariant => {
let mut results = node.downstream_pcards.clone();
results.sort();
results
}
NavigationDirection::Contravariant => {
let mut results = node.upstream_vcards.clone();
results.sort();
results
}
NavigationDirection::Invariant => {
let mut results: Vec<String> = self
.nodes
.values()
.filter(|n| n.content_hash == node.content_hash)
.map(|n| n.hash.clone())
.collect();
results.sort();
results
}
}
}
}