paperless_api/dto.rs
1//! DTO traits and helpers.
2
3use serde::Serialize;
4
5pub trait CreateDtoObject: Serialize {
6 type BaseType: serde::de::DeserializeOwned;
7
8 fn endpoint() -> &'static str;
9}
10
11/// Marker trait for update DTOs.
12pub trait UpdateDto {
13 // Patches the entity with the given ID using the data from this DTO.
14 //fn patch(&self, client: &PaperlessClient) -> impl std::future::Future<()>;
15}
16
17// #[cfg(test)]
18// mod tests {
19// use paperless_api_macros::{CreateDto as CreateDtoDerive, UpdateDto as UpdateDtoDerive};
20// use serde::{Deserialize, Serialize};
21
22// use super::{CreateDto, UpdateDto};
23
24// #[derive(Debug, Default, Clone, Deserialize, Serialize, CreateDtoDerive, UpdateDtoDerive)]
25// struct TestItem {
26// #[dto(skip)]
27// pub id: u32,
28// pub name: String,
29// #[serde(rename = "match")]
30// pub match_pattern: String,
31// pub optional: Option<bool>,
32// #[dto(skip)]
33// #[serde(default)]
34// pub document_count: u32,
35// }
36
37// #[test]
38// fn create_dto_has_correct_fields() {
39// let dto = CreateTestItem {
40// name: "hello".to_string(),
41// match_pattern: "pattern".to_string(),
42// optional: None,
43// };
44// let json = serde_json::to_string(&dto).unwrap();
45// assert!(json.contains("\"name\""));
46// assert!(json.contains("\"match\""));
47// assert!(!json.contains("\"id\""));
48// assert!(!json.contains("\"document_count\""));
49// }
50
51// #[test]
52// fn update_dto_wraps_fields_in_option() {
53// let dto = UpdateTestItem {
54// name: Some("hello".to_string()),
55// match_pattern: None,
56// optional: Some(Some(true)),
57// };
58// let json = serde_json::to_string(&dto).unwrap();
59// assert!(json.contains("\"name\":\"hello\""));
60// assert!(!json.contains("\"match\""));
61// assert!(json.contains("\"optional\":true"));
62// assert!(!json.contains("\"id\""));
63// }
64
65// #[test]
66// fn create_dto_implements_marker_trait() {
67// fn assert_create<T: CreateDto>() {}
68// assert_create::<CreateTestItem>();
69// }
70
71// #[test]
72// fn update_dto_implements_marker_trait() {
73// fn assert_update<T: UpdateDto>() {}
74// assert_update::<UpdateTestItem>();
75// }
76
77// #[test]
78// fn create_dto_default_works() {
79// let dto = CreateTestItem::default();
80// assert_eq!(dto.name, "");
81// assert_eq!(dto.match_pattern, "");
82// assert_eq!(dto.optional, None);
83// }
84
85// #[test]
86// fn update_dto_default_is_all_none() {
87// let dto = UpdateTestItem::default();
88// assert_eq!(dto.name, None);
89// assert_eq!(dto.match_pattern, None);
90// assert_eq!(dto.optional, None);
91// }
92// }