use std::string::String;
use std::vec::Vec;
use crate::foundation::{KnotKind, NumericPath};
use crate::ValidationError;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "schema")]
use std::borrow::ToOwned;
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct PortRefDef {
pub knot: String,
pub port: String,
}
impl PortRefDef {
pub fn new(knot: impl Into<String>, port: impl Into<String>) -> Self {
Self {
knot: knot.into(),
port: port.into(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct KnotDef {
pub id: String,
pub kind: KnotKind,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct ThreadDef {
pub from: PortRefDef,
pub to: PortRefDef,
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct WeaveDef {
pub id: String,
pub numeric: NumericPath,
pub knots: Vec<KnotDef>,
pub threads: Vec<ThreadDef>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Weave {
id: String,
numeric: NumericPath,
knots: Vec<KnotDef>,
threads: Vec<ThreadDef>,
}
impl Weave {
pub fn builder(id: impl Into<String>) -> Result<crate::WeaveBuilder, crate::BuildError> {
crate::WeaveBuilder::new(id)
}
pub fn compose(
id: impl Into<String>,
compose: impl FnOnce(&mut crate::Composer) -> Result<(), crate::BuildError>,
) -> Result<Self, crate::ComposeError> {
let mut composer = crate::Composer::new(id)?;
compose(&mut composer)?;
Ok(composer.build()?)
}
pub fn id(&self) -> &str {
&self.id
}
pub fn numeric(&self) -> NumericPath {
self.numeric
}
pub fn knots(&self) -> &[KnotDef] {
&self.knots
}
pub fn threads(&self) -> &[ThreadDef] {
&self.threads
}
pub fn to_def(&self) -> WeaveDef {
WeaveDef {
id: self.id.clone(),
numeric: self.numeric,
knots: self.knots.clone(),
threads: self.threads.clone(),
}
}
pub(crate) fn from_validated(def: WeaveDef) -> Self {
Self {
id: def.id,
numeric: def.numeric,
knots: def.knots,
threads: def.threads,
}
}
}
impl TryFrom<WeaveDef> for Weave {
type Error = ValidationError;
fn try_from(def: WeaveDef) -> Result<Self, Self::Error> {
crate::authoring::validate::validate_def(&def)?;
Ok(Self::from_validated(def))
}
}
impl From<Weave> for WeaveDef {
fn from(weave: Weave) -> Self {
Self {
id: weave.id,
numeric: weave.numeric,
knots: weave.knots,
threads: weave.threads,
}
}
}