1
1
use std:: { default:: Default , fmt, str:: FromStr , string:: ToString } ;
2
2
3
3
use data_encoding:: { HEXLOWER , HEXLOWER_PERMISSIVE } ;
4
+ use mediatype:: MediaTypeBuf ;
4
5
use serde:: { Serialize , Serializer } ;
5
6
6
7
use crate :: {
7
8
errors:: { ApiError , FileMessageBuilderError } ,
8
- Key , Mime ,
9
+ Key ,
9
10
} ;
10
11
11
12
/// A message type.
@@ -79,15 +80,15 @@ pub struct FileMessage {
79
80
file_blob_id : BlobId ,
80
81
#[ serde( rename = "m" ) ]
81
82
#[ serde( serialize_with = "serialize_to_string" ) ]
82
- file_media_type : Mime ,
83
+ file_media_type : MediaTypeBuf ,
83
84
84
85
#[ serde( rename = "t" ) ]
85
86
#[ serde( skip_serializing_if = "Option::is_none" ) ]
86
87
thumbnail_blob_id : Option < BlobId > ,
87
88
#[ serde( rename = "p" ) ]
88
89
#[ serde( skip_serializing_if = "Option::is_none" ) ]
89
90
#[ serde( serialize_with = "serialize_opt_to_string" ) ]
90
- thumbnail_media_type : Option < Mime > ,
91
+ thumbnail_media_type : Option < MediaTypeBuf > ,
91
92
92
93
#[ serde( rename = "k" ) ]
93
94
#[ serde( serialize_with = "key_to_hex" ) ]
@@ -146,7 +147,7 @@ impl FileMessage {
146
147
pub fn builder (
147
148
file_blob_id : BlobId ,
148
149
blob_encryption_key : Key ,
149
- media_type : Mime ,
150
+ media_type : impl Into < MediaTypeBuf > ,
150
151
file_size_bytes : u32 ,
151
152
) -> FileMessageBuilder {
152
153
FileMessageBuilder :: new (
@@ -161,9 +162,9 @@ impl FileMessage {
161
162
/// Builder for [`FileMessage`](struct.FileMessage.html).
162
163
pub struct FileMessageBuilder {
163
164
file_blob_id : BlobId ,
164
- file_media_type : Mime ,
165
+ file_media_type : MediaTypeBuf ,
165
166
thumbnail_blob_id : Option < BlobId > ,
166
- thumbnail_media_type : Option < Mime > ,
167
+ thumbnail_media_type : Option < MediaTypeBuf > ,
167
168
blob_encryption_key : Key ,
168
169
file_name : Option < String > ,
169
170
file_size_bytes : u32 ,
@@ -190,12 +191,12 @@ impl FileMessageBuilder {
190
191
pub fn new (
191
192
file_blob_id : BlobId ,
192
193
blob_encryption_key : Key ,
193
- media_type : Mime ,
194
+ media_type : impl Into < MediaTypeBuf > ,
194
195
file_size_bytes : u32 ,
195
196
) -> Self {
196
197
FileMessageBuilder {
197
198
file_blob_id,
198
- file_media_type : media_type,
199
+ file_media_type : media_type. into ( ) ,
199
200
thumbnail_blob_id : None ,
200
201
thumbnail_media_type : None ,
201
202
blob_encryption_key,
@@ -221,7 +222,7 @@ impl FileMessageBuilder {
221
222
/// Before calling this function, you need to symmetrically encrypt the
222
223
/// thumbnail data (in JPEG format) with the same key used for the file
223
224
/// data and with the nonce `000...2`.
224
- pub fn thumbnail ( self , blob_id : BlobId , media_type : Mime ) -> Self {
225
+ pub fn thumbnail ( self , blob_id : BlobId , media_type : impl Into < MediaTypeBuf > ) -> Self {
225
226
self . thumbnail_opt ( Some ( ( blob_id, media_type) ) )
226
227
}
227
228
@@ -230,11 +231,11 @@ impl FileMessageBuilder {
230
231
/// Before calling this function, you need to symmetrically encrypt the
231
232
/// thumbnail data (in JPEG format) with the same key used for the file
232
233
/// data and with the nonce `000...2`.
233
- pub fn thumbnail_opt ( mut self , blob : Option < ( BlobId , Mime ) > ) -> Self {
234
+ pub fn thumbnail_opt ( mut self , blob : Option < ( BlobId , impl Into < MediaTypeBuf > ) > ) -> Self {
234
235
match blob {
235
236
Some ( ( blob_id, media_type) ) => {
236
237
self . thumbnail_blob_id = Some ( blob_id) ;
237
- self . thumbnail_media_type = Some ( media_type) ;
238
+ self . thumbnail_media_type = Some ( media_type. into ( ) ) ;
238
239
}
239
240
None => {
240
241
self . thumbnail_blob_id = None ;
@@ -425,6 +426,7 @@ fn key_to_hex<S: Serializer>(val: &Key, serializer: S) -> Result<S::Ok, S::Error
425
426
mod test {
426
427
use std:: collections:: HashMap ;
427
428
429
+ use mediatype:: media_type;
428
430
use serde_json as json;
429
431
430
432
use super :: * ;
@@ -544,10 +546,10 @@ mod test {
544
546
] ) ;
545
547
let file_blob_id = BlobId :: from_str ( "0123456789abcdef0123456789abcdef" ) . unwrap ( ) ;
546
548
let thumb_blob_id = BlobId :: from_str ( "abcdef0123456789abcdef0123456789" ) . unwrap ( ) ;
547
- let jpeg: Mime = "image/jpeg" . parse ( ) . unwrap ( ) ;
548
- let png: Mime = "image/jpeg" . parse ( ) . unwrap ( ) ;
549
- let msg = FileMessage :: builder ( file_blob_id. clone ( ) , key. clone ( ) , jpeg. clone ( ) , 2048 )
550
- . thumbnail ( thumb_blob_id. clone ( ) , png. clone ( ) )
549
+ let jpeg = media_type ! ( IMAGE / JPEG ) ;
550
+ let png = media_type ! ( IMAGE / PNG ) ;
551
+ let msg = FileMessage :: builder ( file_blob_id. clone ( ) , key. clone ( ) , & jpeg, 2048 )
552
+ . thumbnail ( thumb_blob_id. clone ( ) , & png)
551
553
. file_name ( "hello.jpg" )
552
554
. description ( String :: from ( "An image file" ) )
553
555
. rendering_type ( RenderingType :: Media )
@@ -557,7 +559,7 @@ mod test {
557
559
assert_eq ! ( msg. file_blob_id, file_blob_id) ;
558
560
assert_eq ! ( msg. file_media_type, jpeg) ;
559
561
assert_eq ! ( msg. thumbnail_blob_id, Some ( thumb_blob_id) ) ;
560
- assert_eq ! ( msg. thumbnail_media_type, Some ( png) ) ;
562
+ assert_eq ! ( msg. thumbnail_media_type, Some ( png. into ( ) ) ) ;
561
563
assert_eq ! ( msg. blob_encryption_key, key) ;
562
564
assert_eq ! ( msg. file_name, Some ( "hello.jpg" . to_string( ) ) ) ;
563
565
assert_eq ! ( msg. file_size_bytes, 2048 ) ;
0 commit comments