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/// Empty marker trait for Paperless ID types.
11pub trait PaperlessId:
12    std::fmt::Display + PartialEq + Eq + std::hash::Hash + serde::de::DeserializeOwned + Serialize
13{
14}
15
16/// Macro for defining ID wrapper types
17macro_rules! define_ids {
18    ($($def:tt),* $(,)?) => {
19        $(define_ids!(@single $def);)*
20    };
21
22    (@single ($name:ident, $type:ty)) => {
23        #[derive(Clone, Copy, Display, Default, PartialEq, Eq, Hash, Deserialize, Serialize)]
24        #[repr(transparent)]
25        pub struct $name(pub $type);
26
27        impl std::fmt::Debug for $name {
28            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29                write!(f, "{}({})", stringify!($name), *self)
30            }
31        }
32
33        impl std::ops::Deref for $name {
34            type Target = $type;
35
36            fn deref(&self) -> &Self::Target {
37                &self.0
38            }
39        }
40
41        impl PaperlessId for $name {}
42    };
43    (@single ($name:ident, $type:ty, noncopy)) => {
44        #[derive(Clone, Display, Default, PartialEq, Eq, Hash, Deserialize, Serialize)]
45        #[repr(transparent)]
46        pub struct $name(pub $type);
47
48        impl std::fmt::Debug for $name {
49            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50                write!(f, "{}({})", stringify!($name), *self)
51            }
52        }
53
54        impl std::ops::Deref for $name {
55            type Target = $type;
56
57            fn deref(&self) -> &Self::Target {
58                &self.0
59            }
60        }
61
62        impl PaperlessId for $name {}
63    };
64}
65
66define_ids!(
67    (CorrespondentId, u32),
68    (CustomFieldId, u32),
69    (DocumentId, u32),
70    (DocumentTypeId, u32),
71    (GroupId, u32),
72    (NoteId, u32),
73    (SavedViewId, u32),
74    (SelectableOptionId, String, noncopy),
75    (ShareLinkId, u32),
76    (StoragePathId, u32),
77    (TagId, u32),
78    (TaskId, String, noncopy),
79    (UserId, u32),
80    (WorkflowActionId, u32),
81    (WorkflowId, u32),
82    (WorkflowTriggerId, u32),
83    (WebhookActionId, u32),
84);