Skip to content

Commit

Permalink
Improve Serde support for core types.
Browse files Browse the repository at this point in the history
  • Loading branch information
artob committed Oct 8, 2024
1 parent d4bf665 commit fc7e10f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/protoflow-core/src/block_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ impl serde::Serialize for &dyn BlockDescriptor {
S: serde::Serializer,
{
use serde::ser::SerializeStruct;
let mut state = serializer.serialize_struct("BlockDescriptor", 1)?;
let mut state = serializer.serialize_struct("BlockDescriptor", 5)?;
state.serialize_field("name", &self.name())?;
// TODO: add more fields
state.serialize_field("label", &self.label())?;
state.serialize_field("parameters", &self.parameters())?;
state.serialize_field("inputs", &self.inputs())?;
state.serialize_field("outputs", &self.outputs())?;
state.end()
}
}
4 changes: 4 additions & 0 deletions lib/protoflow-core/src/parameter_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ use crate::prelude::{Cow, MaybeLabeled, Named, String};

/// A descriptor for a block parameter.
#[derive(Clone, Default, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ParameterDescriptor {
/// The machine-readable name of this parameter.
pub name: String,

/// A human-readable label, if any, for this parameter.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub label: Option<String>,

/// The data type, if known, of this parameter.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub r#type: Option<String>,

/// A default value, if any, for this parameter.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub default_value: Option<String>,
}

Expand Down
8 changes: 8 additions & 0 deletions lib/protoflow-core/src/port_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,38 @@ use crate::{

/// The dataflow direction of a port.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum PortDirection {
Input,
Output,
}

/// A descriptor for a block port.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct PortDescriptor {
/// The dataflow direction of this port.
pub direction: PortDirection,

/// The machine-readable name of this port, if any.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub name: Option<String>,

/// A human-readable label for this port, if any.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub label: Option<String>,

/// The data type for messages on this port.
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub r#type: Option<String>,

/// The unique identifier for this port.
#[cfg_attr(feature = "serde", serde(skip))]
pub id: PortID,

/// The current state of this port.
#[cfg_attr(feature = "serde", serde(skip))]
pub state: PortState,
}

Expand Down
4 changes: 4 additions & 0 deletions lib/protoflow-core/src/port_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use crate::prelude::{fmt, TryFrom};

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum PortID {
Input(InputPortID),
Output(OutputPortID),
Expand Down Expand Up @@ -69,6 +71,7 @@ impl fmt::Display for PortID {
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct InputPortID(pub(crate) isize);

impl InputPortID {
Expand Down Expand Up @@ -109,6 +112,7 @@ impl fmt::Display for InputPortID {
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OutputPortID(pub(crate) isize);

impl OutputPortID {
Expand Down
2 changes: 2 additions & 0 deletions lib/protoflow-core/src/port_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// This is free and unencumbered software released into the public domain.

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum PortState {
#[default]
Closed,
Expand Down

0 comments on commit fc7e10f

Please sign in to comment.