use std::collections::{HashMap, HashSet};
use crate::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NodeLayout {
pub label: String,
pub uuid: Uuid,
}
impl NodeLayout {
pub fn new(label: impl Into<String>) -> Self {
NodeLayout {
label: label.into(),
uuid: Uuid::new_v4(),
}
}
pub fn input(&self, input: impl Into<String>) -> InputLayout {
let label = input.into();
InputLayout {
uuid: Uuid::new_v3(&self.uuid, label.as_bytes()),
label,
}
}
pub fn output(&self, output: impl Into<String>) -> OutputLayout {
let label = output.into();
OutputLayout {
uuid: Uuid::new_v3(&self.uuid, label.as_bytes()),
label,
}
}
pub fn query(&self, query: impl Into<String>) -> QueryLayout {
let label = query.into();
QueryLayout {
uuid: Uuid::new_v3(&self.uuid, label.as_bytes()),
label,
}
}
pub fn queryable(&self, queryable: impl Into<String>) -> QueryableLayout {
let label = queryable.into();
QueryableLayout {
uuid: Uuid::new_v3(&self.uuid, label.as_bytes()),
label,
}
}
}
pub struct NodeIOBuilder {
pub layout: NodeLayout,
pub inputs: HashSet<Uuid>,
pub outputs: HashSet<Uuid>,
pub queries: HashSet<Uuid>,
pub queryables: HashSet<Uuid>,
pub labels: HashMap<Uuid, String>,
}
impl NodeIOBuilder {
pub fn new(layout: &NodeLayout) -> Self {
Self {
layout: layout.clone(),
inputs: HashSet::new(),
outputs: HashSet::new(),
queryables: HashSet::new(),
queries: HashSet::new(),
labels: HashMap::new(),
}
}
pub fn input(&mut self, input: impl Into<String>) -> IOLayout {
let label: String = input.into();
let layout = self.layout.input(&label);
self.inputs.insert(layout.uuid);
self.labels.insert(layout.uuid, label.clone());
tracing::debug!(
"Node '{}' (uuid: {}) added input layout with label: '{}' (uuid: {})",
self.layout.label,
self.layout.uuid,
label,
layout.uuid
);
layout.into()
}
pub fn output(&mut self, output: impl Into<String>) -> IOLayout {
let label: String = output.into();
let layout = self.layout.output(&label);
self.outputs.insert(layout.uuid);
self.labels.insert(layout.uuid, label.clone());
tracing::debug!(
"Node '{}' (uuid: {}) added output layout with label: '{}' (uuid: {})",
self.layout.label,
self.layout.uuid,
label,
layout.uuid
);
layout.into()
}
pub fn query(&mut self, query: impl Into<String>) -> IOLayout {
let label: String = query.into();
let layout = self.layout.query(&label);
self.queries.insert(layout.uuid);
self.labels.insert(layout.uuid, label.clone());
tracing::debug!(
"Node '{}' (uuid: {}) added query layout with label: '{}' (uuid: {})",
self.layout.label,
self.layout.uuid,
label,
layout.uuid
);
layout.into()
}
pub fn queryable(&mut self, queryable: impl Into<String>) -> IOLayout {
let label: String = queryable.into();
let layout = self.layout.queryable(&label);
self.queryables.insert(layout.uuid);
self.labels.insert(layout.uuid, label.clone());
tracing::debug!(
"Node '{}' (uuid: {}) added queryable layout with label: '{}' (uuid: {})",
self.layout.label,
self.layout.uuid,
label,
layout.uuid
);
layout.into()
}
}