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 From<$name> for $type {
34            #[inline]
35            fn from(item: $name) -> Self {
36                item.0
37            }
38        }
39
40        impl PaperlessId for $name {}
41    };
42    (@single ($name:ident, $type:ty, noncopy)) => {
43        #[derive(Clone, Display, Default, PartialEq, Eq, Hash, Deserialize, Serialize)]
44        #[repr(transparent)]
45        pub struct $name(pub $type);
46
47        impl std::fmt::Debug for $name {
48            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49                write!(f, "{}({})", stringify!($name), *self)
50            }
51        }
52
53        impl AsRef<str> for $name
54        {
55            #[inline]
56            fn as_ref(&self) -> &str {
57                self.0.as_ref()
58            }
59        }
60
61        impl PaperlessId for $name {}
62    };
63}
64
65define_ids!(
66    (CorrespondentId, u32),
67    (CustomFieldId, u32),
68    (DocumentId, u32),
69    (DocumentTypeId, u32),
70    (GroupId, u32),
71    (NoteId, u32),
72    (SavedViewId, u32),
73    (SelectableOptionId, String, noncopy),
74    (ShareLinkId, u32),
75    (StoragePathId, u32),
76    (TagId, u32),
77    (TaskId, String, noncopy),
78    (UserId, u32),
79    (WorkflowActionId, u32),
80    (WorkflowId, u32),
81    (WorkflowTriggerId, u32),
82    (WebhookActionId, u32),
83);