logic_mesh/base/block/
desc.rs1use std::fmt::Display;
8
9use libhaystack::val::kind::HaystackKind;
10
11use super::BlockProps;
12
13#[derive(Default, Debug, Clone, PartialEq)]
18pub struct BlockDesc {
19 pub name: String,
21 pub library: String,
23 pub dis: String,
25 pub category: String,
27 pub ver: String,
29 pub inputs: Vec<BlockPin>,
31 pub outputs: Vec<BlockPin>,
33 pub doc: String,
35 pub implementation: BlockImplementation,
37
38 pub run_condition: Option<BlockRunCondition>,
40}
41
42impl BlockDesc {
43 pub fn qname(&self) -> String {
45 format!("{}::{}", self.library, self.name)
46 }
47}
48
49pub trait BlockStaticDesc: BlockProps {
54 fn desc() -> &'static BlockDesc
56 where
57 Self: Sized;
58}
59
60#[derive(Default, Debug, Clone, PartialEq)]
64pub struct BlockPin {
65 pub name: String,
66 pub kind: HaystackKind,
67}
68
69#[derive(Default, Debug, Clone, PartialEq)]
71pub enum BlockImplementation {
72 #[default]
74 Native,
75 External,
77}
78
79impl TryFrom<&str> for BlockImplementation {
80 type Error = String;
81
82 fn try_from(implementation: &str) -> Result<Self, Self::Error> {
83 match implementation {
84 "native" => Ok(BlockImplementation::Native),
85 "external" => Ok(BlockImplementation::External),
86 _ => Err(format!("Invalid implementation: {implementation}")),
87 }
88 }
89}
90
91impl Display for BlockImplementation {
92 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93 let kind = match self {
94 BlockImplementation::Native => "native",
95 BlockImplementation::External => "external",
96 };
97 write!(fmt, "{kind}")
98 }
99}
100
101#[derive(Default, Debug, Clone, PartialEq)]
103pub enum BlockRunCondition {
104 #[default]
106 Change,
107 Always,
109}
110
111impl TryFrom<&str> for BlockRunCondition {
112 type Error = String;
113
114 fn try_from(implementation: &str) -> Result<Self, Self::Error> {
115 match implementation {
116 "change" => Ok(BlockRunCondition::Change),
117 "always" => Ok(BlockRunCondition::Always),
118 _ => Err(format!("Invalid implementation: {implementation}")),
119 }
120 }
121}
122
123impl Display for BlockRunCondition {
124 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125 let kind = match self {
126 BlockRunCondition::Change => "native",
127 BlockRunCondition::Always => "external",
128 };
129 write!(fmt, "{kind}")
130 }
131}