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/// 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        /// ID type for a Paperless entity.
26        pub struct $name(pub $type);
27
28        impl std::fmt::Debug for $name {
29            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30                write!(f, "{}({})", stringify!($name), *self)
31            }
32        }
33
34        impl From<$name> for $type {
35            #[inline]
36            fn from(item: $name) -> Self {
37                item.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        /// ID type for a Paperless entity (non-copy).
47        pub struct $name(pub $type);
48
49        impl std::fmt::Debug for $name {
50            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51                write!(f, "{}({})", stringify!($name), *self)
52            }
53        }
54
55        impl AsRef<str> for $name
56        {
57            #[inline]
58            fn as_ref(&self) -> &str {
59                self.0.as_ref()
60            }
61        }
62
63        impl PaperlessId for $name {}
64    };
65}
66
67define_ids!(
68    (CorrespondentId, u32),
69    (CustomFieldId, u32),
70    (DocumentId, u32),
71    (DocumentTypeId, u32),
72    (GroupId, u32),
73    (NoteId, u32),
74    (SavedViewId, u32),
75    (SelectableOptionId, String, noncopy),
76    (ShareLinkId, u32),
77    (StoragePathId, u32),
78    (TagId, u32),
79    (TaskId, String, noncopy),
80    (UserId, u32),
81    (WorkflowActionId, u32),
82    (WorkflowId, u32),
83    (WorkflowTriggerId, u32),
84    (WebhookActionId, u32),
85);