use std::borrow::Borrow;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use unicode_normalization::UnicodeNormalization;
use crate::Value;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct NodeName(String);
impl NodeName {
pub fn new(name: impl Into<String>) -> Self {
let name = name.into();
if name.is_ascii() {
return Self(name);
}
Self(name.nfc().collect())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Borrow<str> for NodeName {
fn borrow(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct NodeTypeId(String);
impl Borrow<str> for NodeTypeId {
fn borrow(&self) -> &str {
&self.0
}
}
impl NodeTypeId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Node {
pub(crate) name: NodeName,
type_id: NodeTypeId,
doc: Option<String>,
config: BTreeMap<String, Value>,
}
impl Node {
pub fn new(name: NodeName, type_id: NodeTypeId) -> Self {
Self {
name,
type_id,
doc: None,
config: BTreeMap::new(),
}
}
pub fn with_doc(mut self, doc: Option<String>) -> Self {
self.doc = doc;
self
}
pub fn name(&self) -> &str {
self.name.as_str()
}
pub fn type_id(&self) -> &str {
self.type_id.as_str()
}
pub fn doc(&self) -> Option<&str> {
self.doc.as_deref()
}
pub fn set_config(&mut self, pin: impl Into<String>, value: Value) {
self.config.insert(pin.into(), value);
}
pub fn config(&self, pin: &str) -> Option<&Value> {
self.config.get(pin)
}
pub fn config_entries(&self) -> impl Iterator<Item = (&String, &Value)> {
self.config.iter()
}
}