1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::debug_print_bytes;
use base64::DecodeError;
use bytes::Bytes;
#[cfg(feature = "fp-bindgen")]
use fp_bindgen::prelude::Serializable;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fmt::{self, Debug, Formatter};
use typed_builder::TypedBuilder;
#[derive(Clone, Default, Deserialize, Eq, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
feature = "fp-bindgen",
derive(Serializable),
fp(rust_module = "fiberplane_models::blobs")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct Blob {
#[builder(setter(into))]
pub data: Bytes,
#[builder(setter(into))]
pub mime_type: String,
}
impl TryFrom<EncodedBlob> for Blob {
type Error = DecodeError;
fn try_from(blob: EncodedBlob) -> Result<Self, Self::Error> {
Ok(Self {
data: base64::decode(&blob.data)?.into(),
mime_type: blob.mime_type,
})
}
}
impl TryFrom<&EncodedBlob> for Blob {
type Error = DecodeError;
fn try_from(blob: &EncodedBlob) -> Result<Self, Self::Error> {
Ok(Self {
data: base64::decode(&blob.data)?.into(),
mime_type: blob.mime_type.clone(),
})
}
}
impl Debug for Blob {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Blob")
.field("mime_type", &self.mime_type)
.field("data_length", &self.data.len())
.field("data", &debug_print_bytes(&self.data))
.finish()
}
}
#[derive(Clone, Default, Deserialize, Eq, PartialEq, Serialize, TypedBuilder)]
#[cfg_attr(
feature = "fp-bindgen",
derive(Serializable),
fp(rust_module = "fiberplane_models::blobs")
)]
#[non_exhaustive]
#[serde(rename_all = "camelCase")]
pub struct EncodedBlob {
#[builder(setter(into))]
pub data: String,
#[builder(setter(into))]
pub mime_type: String,
}
impl From<Blob> for EncodedBlob {
fn from(blob: Blob) -> Self {
Self {
data: base64::encode(blob.data.as_ref()),
mime_type: blob.mime_type,
}
}
}
impl From<&Blob> for EncodedBlob {
fn from(blob: &Blob) -> Self {
Self {
data: base64::encode(blob.data.as_ref()),
mime_type: blob.mime_type.clone(),
}
}
}
impl Debug for EncodedBlob {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("EncodedBlob")
.field("mime_type", &self.mime_type)
.field("data_length", &self.data.len())
.field("data", &debug_print_bytes(self.data.as_bytes()))
.finish()
}
}