protoflow_blocks/blocks/
io.rspub mod io {
use super::{
prelude::{vec, Box, Cow, Named, String, Vec},
BlockConnections, BlockInstantiation, InputPortName, OutputPortName, System,
};
use crate::{
prelude::{FromStr, ToString},
types::Encoding,
};
use protoflow_core::{Block, Message};
pub trait IoBlocks {
fn decode<T: Message + FromStr + 'static>(&mut self) -> Decode<T>;
fn decode_with<T: Message + FromStr + 'static>(&mut self, encoding: Encoding) -> Decode<T>;
fn decode_lines<T: Message + FromStr + 'static>(&mut self) -> Decode<T> {
self.decode_with::<T>(Encoding::TextWithNewlineSuffix)
}
fn encode<T: Message + ToString + 'static>(&mut self) -> Encode<T>;
fn encode_with<T: Message + ToString + 'static>(&mut self, encoding: Encoding)
-> Encode<T>;
fn encode_lines<T: Message + ToString + 'static>(&mut self) -> Encode<T> {
self.encode_with::<T>(Encoding::TextWithNewlineSuffix)
}
fn encode_hex(&mut self) -> EncodeHex;
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum IoBlockTag {
Decode,
Encode,
EncodeHex,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug)]
pub enum IoBlockConfig {
Decode {
input: InputPortName,
output: OutputPortName,
encoding: Option<Encoding>,
},
Encode {
input: InputPortName,
output: OutputPortName,
encoding: Option<Encoding>,
},
EncodeHex {
input: InputPortName,
output: OutputPortName,
},
}
impl Named for IoBlockConfig {
fn name(&self) -> Cow<str> {
use IoBlockConfig::*;
Cow::Borrowed(match self {
Decode { .. } => "Decode",
Encode { .. } => "Encode",
EncodeHex { .. } => "EncodeHex",
})
}
}
impl BlockConnections for IoBlockConfig {
fn output_connections(&self) -> Vec<(&'static str, Option<OutputPortName>)> {
use IoBlockConfig::*;
match self {
Decode { output, .. } | Encode { output, .. } | EncodeHex { output, .. } => {
vec![("output", Some(output.clone()))]
}
}
}
}
impl BlockInstantiation for IoBlockConfig {
fn instantiate(&self, system: &mut System) -> Box<dyn Block> {
use IoBlockConfig::*;
match self {
Decode { encoding, .. } => {
Box::new(super::Decode::<String>::with_system(system, *encoding))
}
Encode { encoding, .. } => {
Box::new(super::Encode::<String>::with_system(system, *encoding))
}
EncodeHex { .. } => Box::new(super::EncodeHex::with_system(system)),
}
}
}
mod decode;
pub use decode::*;
mod encode;
pub use encode::*;
mod encode_hex;
pub use encode_hex::*;
}
pub use io::*;