1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Serialize, Deserialize)]
4pub struct Thumbnail {
5 pub url: String,
6 pub width: Option<u32>,
7 pub height: Option<u32>,
8}
9
10#[derive(Debug, Serialize, Deserialize)]
11pub struct Thumbnails {
12 pub tiny: Thumbnail,
13 pub small: Thumbnail,
14}
15
16#[derive(Debug, Serialize, Deserialize)]
17pub struct File {
18 pub url: String,
19 pub thumbnails: Option<Thumbnails>,
20 pub name: String,
21 pub size: u64,
22 pub mime_type: String,
23 pub is_image: bool,
24 pub image_width: Option<u32>,
25 pub image_height: Option<u32>,
26 pub uploaded_at: String,
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32 use serde_json::json;
33
34 #[test]
35 fn test_file_serialization() {
36 let file_with_dimensions = File {
38 url: "https://example.com/file.jpg".to_string(),
39 thumbnails: Some(Thumbnails {
40 tiny: Thumbnail {
41 url: "https://example.com/tiny.jpg".to_string(),
42 width: None,
43 height: Some(100),
44 },
45 small: Thumbnail {
46 url: "https://example.com/small.jpg".to_string(),
47 width: Some(200),
48 height: Some(200),
49 },
50 }),
51 name: "file.jpg".to_string(),
52 size: 1024,
53 mime_type: "image/jpeg".to_string(),
54 is_image: true,
55 image_width: Some(800),
56 image_height: Some(600),
57 uploaded_at: "2023-01-01T00:00:00Z".to_string(),
58 };
59
60 let json_with_dimensions = serde_json::to_value(&file_with_dimensions).unwrap();
61 assert_eq!(json_with_dimensions["image_width"], 800);
62 assert_eq!(json_with_dimensions["image_height"], 600);
63 assert!(json_with_dimensions["thumbnails"].is_object());
64
65 let file_without_dimensions = File {
67 url: "https://example.com/file.txt".to_string(),
68 thumbnails: None,
69 name: "file.txt".to_string(),
70 size: 1024,
71 mime_type: "text/plain".to_string(),
72 is_image: false,
73 image_width: None,
74 image_height: None,
75 uploaded_at: "2023-01-01T00:00:00Z".to_string(),
76 };
77
78 let json_without_dimensions = serde_json::to_value(&file_without_dimensions).unwrap();
79 assert!(json_without_dimensions["image_width"].is_null());
80 assert!(json_without_dimensions["image_height"].is_null());
81 assert!(json_without_dimensions["thumbnails"].is_null());
82
83 let json = json!({
85 "url": "https://example.com/file.txt",
86 "thumbnails": null,
87 "name": "file.txt",
88 "size": 1024,
89 "mime_type": "text/plain",
90 "is_image": false,
91 "image_width": null,
92 "image_height": null,
93 "uploaded_at": "2023-01-01T00:00:00Z"
94 });
95
96 let file: File = serde_json::from_value(json).unwrap();
97 assert!(file.image_width.is_none());
98 assert!(file.image_height.is_none());
99 assert!(file.thumbnails.is_none());
100 }
101}
102
103#[derive(Debug, Serialize, Deserialize)]
104pub struct UploadFileViaUrlRequest {
105 pub url: String,
106}