message_impl/
message_impl.rs

1use flarrow_message::prelude::{
2    thirdparty::{arrow_array::*, arrow_data::*, arrow_schema::*, *},
3    *,
4};
5
6#[derive(Debug)]
7struct Metadata {
8    name: Option<String>,
9    width: u32,
10    height: u32,
11}
12
13impl ArrowMessage for Metadata {
14    fn field(name: impl Into<String>) -> Field {
15        make_union_fields(
16            name,
17            vec![
18                Option::<String>::field("name"),
19                Option::<u32>::field("width"),
20                Option::<u32>::field("height"),
21            ],
22        )
23    }
24
25    fn try_from_arrow(data: ArrayData) -> Result<Self>
26    where
27        Self: Sized,
28    {
29        let (map, children) = unpack_union(data);
30
31        Ok(Metadata {
32            name: extract_union_data("name", &map, &children)?,
33            width: extract_union_data("width", &map, &children)?,
34            height: extract_union_data("height", &map, &children)?,
35        })
36    }
37
38    fn try_into_arrow(self) -> Result<ArrayRef> {
39        let union_fields = get_union_fields::<Self>()?;
40
41        make_union_array(
42            union_fields,
43            vec![
44                self.name.try_into_arrow()?,
45                self.width.try_into_arrow()?,
46                self.height.try_into_arrow()?,
47            ],
48        )
49    }
50}
51
52impl TryFrom<ArrayData> for Metadata {
53    type Error = eyre::Report;
54
55    fn try_from(data: ArrayData) -> Result<Self> {
56        Metadata::try_from_arrow(data)
57    }
58}
59
60impl TryFrom<Metadata> for ArrayData {
61    type Error = eyre::Report;
62
63    fn try_from(metadata: Metadata) -> Result<Self> {
64        metadata.try_into_arrow().map(|array| array.into_data())
65    }
66}
67
68#[derive(Debug)]
69struct Image {
70    data: UInt8Array,
71    metadata: Option<Metadata>,
72}
73
74impl ArrowMessage for Image {
75    fn field(name: impl Into<String>) -> Field {
76        make_union_fields(
77            name,
78            vec![
79                UInt8Array::field("data"),
80                Option::<Metadata>::field("metadata"),
81            ],
82        )
83    }
84
85    fn try_from_arrow(data: ArrayData) -> Result<Self>
86    where
87        Self: Sized,
88    {
89        let (map, children) = unpack_union(data);
90
91        Ok(Image {
92            data: extract_union_data("data", &map, &children)?,
93            metadata: extract_union_data("metadata", &map, &children)?,
94        })
95    }
96
97    fn try_into_arrow(self) -> Result<ArrayRef> {
98        let union_fields = get_union_fields::<Self>()?;
99
100        make_union_array(
101            union_fields,
102            vec![self.data.try_into_arrow()?, self.metadata.try_into_arrow()?],
103        )
104    }
105}
106
107impl TryFrom<ArrayData> for Image {
108    type Error = eyre::Report;
109
110    fn try_from(data: ArrayData) -> Result<Self> {
111        Image::try_from_arrow(data)
112    }
113}
114
115impl TryFrom<Image> for ArrayData {
116    type Error = eyre::Report;
117
118    fn try_from(image: Image) -> Result<Self> {
119        image.try_into_arrow().map(|array| array.into_data())
120    }
121}
122
123fn main() -> Result<()> {
124    let image = Image {
125        data: UInt8Array::from(vec![1, 2, 3]),
126        metadata: Some(Metadata {
127            name: Some("example".to_string()),
128            width: 12,
129            height: 12,
130        }),
131    };
132
133    println!("{:?}", image);
134
135    let arrow = ArrayData::try_from(image)?;
136    let image = Image::try_from(arrow)?;
137
138    println!("{:?}", image);
139
140    Ok(())
141}