vis/src/lib.rs
2025-01-22 18:09:15 +01:00

75 lines
2 KiB
Rust

pub mod parse;
pub mod render;
use crate::Element::{Edge, Node};
use parse::tokens::TokenType;
use std::collections::HashMap;
use std::fmt::Display;
#[derive(Debug)]
pub struct Vis {
pub structure: Vec<Element>,
pub styles: Vec<StyleNode>,
}
#[derive(Debug, Clone)]
pub enum Element {
Node(String, Option<String>, Vec<Element>),
Edge(String, String, TokenType, Option<String>),
}
impl Element {
pub fn new_node(id: &str, label: Option<&str>, children: Vec<Element>) -> Element {
Element::Node(id.into(), label.map(|s| s.into()), children)
}
pub fn new_edge(
id: String,
source: String,
token: TokenType,
label: Option<String>,
) -> Element {
Edge(id, source, token, label)
}
}
impl Display for Element {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Node(id, label, children) => {
let mut string = String::new();
string.push_str(&format!(
"Node {{{}: {}",
id,
label.as_ref().unwrap_or(&"".to_string())
));
for child in children {
string.push_str(&format!(" {}", child));
}
string.push('}');
write!(f, "{}", string)
}
Edge(id, source, token, label) => {
let mut string = "Edge {{".to_string();
string.push_str(&format!("{} {} {:?}", id, source, token));
if let Some(label) = label {
string.push_str(&format!(" {}", label));
}
string.push('}');
write!(f, "{}", string)
}
}
}
}
#[derive(Debug, Clone)]
pub struct StyleNode {
pub id_ref: String,
pub containertype: ContainerType,
pub attributes: HashMap<String, String>,
}
#[derive(Debug, Clone)]
pub enum ContainerType {
NonGroup, // needs thinking about
Group,
}