use std::fmt::Display;
use libhaystack::val::kind::HaystackKind;
use super::BlockProps;
#[derive(Default, Debug, Clone, PartialEq)]
pub struct BlockDesc {
pub name: String,
pub library: String,
pub dis: String,
pub category: String,
pub ver: String,
pub inputs: Vec<BlockPin>,
pub outputs: Vec<BlockPin>,
pub doc: String,
pub implementation: BlockImplementation,
pub run_condition: Option<BlockRunCondition>,
}
impl BlockDesc {
pub fn qname(&self) -> String {
format!("{}::{}", self.library, self.name)
}
}
pub trait BlockStaticDesc: BlockProps {
fn desc() -> &'static BlockDesc
where
Self: Sized;
}
#[derive(Default, Debug, Clone, PartialEq)]
pub struct BlockPin {
pub name: String,
pub kind: HaystackKind,
}
#[derive(Default, Debug, Clone, PartialEq)]
pub enum BlockImplementation {
#[default]
Native,
External,
}
impl TryFrom<&str> for BlockImplementation {
type Error = String;
fn try_from(implementation: &str) -> Result<Self, Self::Error> {
match implementation {
"native" => Ok(BlockImplementation::Native),
"external" => Ok(BlockImplementation::External),
_ => Err(format!("Invalid implementation: {implementation}")),
}
}
}
impl Display for BlockImplementation {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let kind = match self {
BlockImplementation::Native => "native",
BlockImplementation::External => "external",
};
write!(fmt, "{kind}")
}
}
#[derive(Default, Debug, Clone, PartialEq)]
pub enum BlockRunCondition {
#[default]
Change,
Always,
}
impl TryFrom<&str> for BlockRunCondition {
type Error = String;
fn try_from(implementation: &str) -> Result<Self, Self::Error> {
match implementation {
"change" => Ok(BlockRunCondition::Change),
"always" => Ok(BlockRunCondition::Always),
_ => Err(format!("Invalid implementation: {implementation}")),
}
}
}
impl Display for BlockRunCondition {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let kind = match self {
BlockRunCondition::Change => "native",
BlockRunCondition::Always => "external",
};
write!(fmt, "{kind}")
}
}