Skip to main content

glycin_utils/api/
creator.rs

1#[cfg(feature = "external")]
2use zbus::zvariant::{DeserializeDict, SerializeDict, Type, as_value};
3
4use crate::{ByteData, FungibleMemory, MemoryAllocationError, api};
5
6#[derive(Debug)]
7#[cfg_attr(
8    feature = "external",
9    derive(Type, serde::Serialize, serde::Deserialize)
10)]
11#[cfg_attr(feature = "external", zvariant(signature = "dict"))]
12#[cfg_attr(
13    feature = "external",
14    serde(bound(
15        serialize = "B: ByteData + serde::Serialize + zbus::zvariant::Type + 'static",
16        deserialize = "B: ByteData + serde::de::DeserializeOwned + zbus::zvariant::Type + 'static"
17    ))
18)]
19#[non_exhaustive]
20pub struct NewImage<B: ByteData> {
21    #[cfg_attr(feature = "external", serde(with = "as_value"))]
22    pub image_info: api::ImageDetails<B>,
23    #[cfg_attr(feature = "external", serde(with = "as_value"))]
24    pub frames: Vec<api::Frame<B>>,
25}
26
27impl<B: ByteData> NewImage<B> {
28    pub fn new(image_info: api::ImageDetails<B>, frames: Vec<api::Frame<B>>) -> Self {
29        Self { image_info, frames }
30    }
31
32    pub fn into_other<O: ByteData>(self) -> Result<NewImage<O>, MemoryAllocationError> {
33        Ok(NewImage {
34            image_info: self.image_info.into_other()?,
35            frames: self
36                .frames
37                .into_iter()
38                .map(|x| x.into_other::<O>())
39                .collect::<Result<_, _>>()?,
40        })
41    }
42
43    pub async fn initial_seal(&mut self) -> Result<(), MemoryAllocationError> {
44        self.image_info.initial_seal().await?;
45        for frame in &mut self.frames {
46            frame.initial_seal().await?;
47        }
48        Ok(())
49    }
50
51    pub async fn final_seal(&mut self) -> Result<(), MemoryAllocationError> {
52        self.image_info.final_seal().await?;
53        for frame in &mut self.frames {
54            frame.final_seal().await?;
55        }
56        Ok(())
57    }
58}
59
60#[derive(Debug, Default)]
61#[cfg_attr(feature = "external", derive(DeserializeDict, SerializeDict, Type))]
62#[cfg_attr(feature = "external", zvariant(signature = "dict"))]
63#[non_exhaustive]
64pub struct EncodingOptions {
65    pub quality: Option<u8>,
66    pub compression: Option<u8>,
67}
68
69#[derive(Debug)]
70#[cfg_attr(
71    feature = "external",
72    derive(Type, serde::Serialize, serde::Deserialize)
73)]
74#[cfg_attr(feature = "external", zvariant(signature = "dict"))]
75#[cfg_attr(
76    feature = "external",
77    serde(bound(
78        serialize = "B: ByteData + serde::Serialize + zbus::zvariant::Type + 'static",
79        deserialize = "B: ByteData + serde::de::DeserializeOwned + zbus::zvariant::Type + 'static"
80    ))
81)]
82#[non_exhaustive]
83pub struct EncodedImage<B: ByteData> {
84    #[cfg_attr(feature = "external", serde(with = "as_value"))]
85    pub data: B,
86}
87
88impl<B: ByteData> EncodedImage<B> {
89    pub fn new(data: B) -> Self {
90        Self { data }
91    }
92
93    pub async fn inital_seal(&mut self) -> Result<(), MemoryAllocationError> {
94        self.data.initial_seal().await
95    }
96
97    pub async fn final_seal(&mut self) -> Result<(), MemoryAllocationError> {
98        self.data.final_seal().await
99    }
100
101    pub fn into_fungible(self) -> EncodedImage<FungibleMemory> {
102        EncodedImage {
103            data: self.data.into_fungible(),
104        }
105    }
106}