Skip to main content

paperless_api/
id.rs

1//! Paperless ID types.
2//!
3//! Paperless uses numeric IDs for most entities.
4//! To avoid confusion, ID types are defined as wrappers around the underlying numeric type.
5//! e.g. DocumentId(u32)
6
7use derive_more::Display;
8use serde::{Deserialize, Serialize};
9
10/// Macro for defining ID wrapper types
11macro_rules! define_ids {
12    ($($def:tt),* $(,)?) => {
13        $(define_ids!(@single $def);)*
14    };
15
16    (@single ($name:ident, $type:ty)) => {
17        #[derive(Clone, Copy, Display, Default, PartialEq, Eq, Hash, Deserialize, Serialize)]
18        #[repr(transparent)]
19        pub struct $name(pub $type);
20
21        impl std::fmt::Debug for $name {
22            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23                write!(f, "{}({})", stringify!($name), *self)
24            }
25        }
26
27        impl std::ops::Deref for $name {
28            type Target = $type;
29
30            fn deref(&self) -> &Self::Target {
31                &self.0
32            }
33        }
34    };
35    (@single ($name:ident, $type:ty, noncopy)) => {
36        #[derive(Clone, Display, Default, PartialEq, Eq, Hash, Deserialize, Serialize)]
37        #[repr(transparent)]
38        pub struct $name(pub $type);
39
40        impl std::fmt::Debug for $name {
41            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42                write!(f, "{}({})", stringify!($name), *self)
43            }
44        }
45
46        impl std::ops::Deref for $name {
47            type Target = $type;
48
49            fn deref(&self) -> &Self::Target {
50                &self.0
51            }
52        }
53    };
54}
55
56define_ids!(
57    (CorrespondentId, u32),
58    (CustomFieldId, u32),
59    (DocumentId, u32),
60    (DocumentTypeId, u32),
61    (GroupId, u32),
62    (NoteId, u32),
63    (SavedViewId, u32),
64    (SelectableOptionId, String, noncopy),
65    (ShareLinkId, u32),
66    (StoragePathId, u32),
67    (TagId, u32),
68    (TaskId, String, noncopy),
69    (UserId, u32),
70    (WorkflowActionId, u32),
71    (WorkflowId, u32),
72    (WorkflowTriggerId, u32),
73    (WebhookActionId, u32),
74);