1#[cfg(feature = "serialize")]
2use crate::{Cell, Context, Control, IdList, PortParent, RRC};
3#[cfg(feature = "serialize")]
4use calyx_utils::GetName;
5#[cfg(feature = "serialize")]
6use serde::{
7 Serialize, Serializer,
8 ser::{SerializeSeq, SerializeStruct},
9};
10#[cfg(feature = "serialize")]
11use serde_with::SerializeAs;
12
13#[cfg(feature = "serialize")]
14impl Serialize for Context {
15 fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
16 where
17 S: Serializer,
18 {
19 let mut ctx = ser.serialize_struct("Context", 2)?;
20 ctx.serialize_field("components", &self.components)?;
21 ctx.serialize_field("entrypoint", &self.entrypoint)?;
22 ctx.end()
23 }
24}
25
26#[cfg(feature = "serialize")]
27pub struct SerCellRef;
28#[cfg(feature = "serialize")]
29impl SerializeAs<RRC<Cell>> for SerCellRef {
30 fn serialize_as<S>(
31 value: &RRC<Cell>,
32 serializer: S,
33 ) -> Result<S::Ok, S::Error>
34 where
35 S: serde::Serializer,
36 {
37 value.borrow().name().serialize(serializer)
38 }
39}
40
41#[cfg(feature = "serialize")]
42impl SerializeAs<RRC<Control>> for Control {
43 fn serialize_as<S>(
44 value: &RRC<Control>,
45 serializer: S,
46 ) -> Result<S::Ok, S::Error>
47 where
48 S: serde::Serializer,
49 {
50 value.borrow().serialize(serializer)
51 }
52}
53
54#[cfg(feature = "serialize")]
55impl<T: GetName + Serialize> Serialize for IdList<T> {
56 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
57 where
58 S: Serializer,
59 {
60 let mut seq = serializer.serialize_seq(Some(self.len()))?;
61 for obj in self.iter() {
62 seq.serialize_element(obj)?;
63 }
64 seq.end()
65 }
66}
67
68#[cfg(feature = "serialize")]
69impl Serialize for PortParent {
70 fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
71 where
72 S: Serializer,
73 {
74 match &self {
75 PortParent::Cell(weak_cell_ref) => {
76 weak_cell_ref.upgrade().borrow().name().serialize(ser)
77 }
78 PortParent::Group(weak_group_ref) => {
79 weak_group_ref.upgrade().borrow().name().serialize(ser)
80 }
81 PortParent::FSM(wref_fsm) => {
82 wref_fsm.upgrade().borrow().name().serialize(ser)
83 }
84 PortParent::StaticGroup(weak_sgroup_ref) => {
85 weak_sgroup_ref.upgrade().borrow().name().serialize(ser)
86 }
87 }
88 }
89}