use super::json::Json;
use std::collections::{HashMap, HashSet};
pub fn slug(raw: &str) -> String {
let mut out = String::new();
let mut gap = false;
for c in raw.chars() {
if c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-') {
out.push(c);
gap = false;
} else if !gap {
out.push('_');
gap = true;
}
}
let s = out.trim_matches('_');
if s.is_empty() { "node".to_string() } else { s.to_string() }
}
pub fn fnv1a(s: &str) -> String {
let mut h: u32 = 0x811c_9dc5;
for unit in s.encode_utf16() {
h ^= unit as u32;
h = h.wrapping_mul(0x0100_0193);
}
format!("{h:08x}")
}
pub fn wire(node: &str, port: &str) -> Json {
Json::obj().with("wire", Json::obj().with("node", node).with("port", port))
}
pub fn channels(set: &str, values: Vec<Json>) -> Vec<(String, Json)> {
values.into_iter().enumerate().map(|(i, v)| (format!("{set}.{}", i + 1), v)).collect()
}
pub struct Scope {
pub id: String,
pub meta: Json,
nodes: Vec<(String, Json)>,
taken: HashSet<String>,
next: HashMap<String, usize>,
}
impl Scope {
pub fn new(id: &str, meta: Json) -> Scope {
Scope { id: id.to_string(), meta, nodes: Vec::new(), taken: HashSet::new(), next: HashMap::new() }
}
pub fn has(&self, id: &str) -> bool {
self.taken.contains(id)
}
pub fn unique_id(&mut self, base: &str) -> String {
let b = slug(base);
let mut n = *self.next.get(&b).unwrap_or(&2);
let mut i = b.clone();
while self.taken.contains(&i) {
i = format!("{b}_{n}");
n += 1;
}
self.next.insert(b, n);
self.taken.insert(i.clone());
i
}
pub fn add(&mut self, kind: &str, inputs: Vec<(String, Json)>, id: Option<&str>, label: Option<&str>, meta: Option<Json>) -> Result<String, String> {
let nid = match id {
Some(id) => {
let nid = slug(id);
if self.taken.contains(&nid) {
return Err(format!("{}: two nodes are called {nid}", self.id));
}
self.taken.insert(nid.clone());
nid
}
None => self.unique_id(label.unwrap_or(kind)),
};
let mut node = Json::obj().with("id", nid.as_str()).with("type", kind).with("inputs", Json::Obj(inputs));
if let Some(label) = label {
node.set("label", label);
}
if let Some(meta) = meta {
node.set("meta", meta);
}
self.nodes.push((nid.clone(), node));
Ok(nid)
}
pub fn disable(&mut self, id: &str) {
if let Some((_, node)) = self.nodes.iter_mut().find(|(nid, _)| nid == id) {
node.set("disabled", true);
}
}
pub fn set_input(&mut self, id: &str, port: &str, value: Json) {
if let Some((_, node)) = self.nodes.iter_mut().find(|(nid, _)| nid == id) {
let mut inputs = node.get("inputs").cloned().unwrap_or(Json::obj());
inputs.set(port, value);
node.set("inputs", inputs);
}
}
pub fn build(self) -> Json {
Json::obj().with("id", self.id.as_str()).with("nodes", Json::Obj(self.nodes)).with("meta", self.meta)
}
}