Skip to main content

proton_sdk/
ids.rs

1//! Strongly-typed string identifiers used across the SDK.
2//!
3//! Proton API identifiers are opaque strings. We wrap them in newtypes so the
4//! type system prevents mixing e.g. a [`ShareId`] with a [`LinkId`], mirroring
5//! the dedicated ID types in the C# SDK.
6
7use serde::{Deserialize, Serialize};
8use std::fmt;
9
10macro_rules! string_id {
11    ($(#[$meta:meta])* $name:ident) => {
12        $(#[$meta])*
13        #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
14        #[serde(transparent)]
15        pub struct $name(pub String);
16
17        impl $name {
18            pub fn new(value: impl Into<String>) -> Self {
19                Self(value.into())
20            }
21
22            pub fn as_str(&self) -> &str {
23                &self.0
24            }
25        }
26
27        impl fmt::Display for $name {
28            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29                f.write_str(&self.0)
30            }
31        }
32
33        impl fmt::Debug for $name {
34            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35                write!(f, concat!(stringify!($name), "({:?})"), self.0)
36            }
37        }
38
39        impl From<String> for $name {
40            fn from(value: String) -> Self {
41                Self(value)
42            }
43        }
44
45        impl From<&str> for $name {
46            fn from(value: &str) -> Self {
47                Self(value.to_owned())
48            }
49        }
50    };
51}
52
53string_id!(
54    /// Identifies an authenticated API session (`x-pm-uid`).
55    SessionId
56);
57string_id!(
58    /// Identifies a user account.
59    UserId
60);
61string_id!(
62    /// Identifies a user-level encryption key.
63    UserKeyId
64);
65string_id!(
66    /// Identifies an email address attached to an account.
67    AddressId
68);
69string_id!(
70    /// Identifies an address-level encryption key.
71    AddressKeyId
72);
73string_id!(
74    /// Identifies a Drive volume.
75    VolumeId
76);
77string_id!(
78    /// Identifies a Drive share.
79    ShareId
80);
81string_id!(
82    /// Identifies a Drive link (node) within a volume.
83    LinkId
84);
85string_id!(
86    /// Identifies a Drive volume event; doubles as the enumeration cursor.
87    DriveEventId
88);
89string_id!(
90    /// Identifies a membership of an address in a Drive share.
91    ShareMembershipId
92);
93string_id!(
94    /// Identifies a Drive device (a synced folder registered by a desktop client).
95    ///
96    /// Opaque and volume-free: the C# `DeviceUid` dropped its volume component
97    /// (upstream `chore: remove volume id from device uid`), so it is a plain id
98    /// rather than a `{volume}~{link}` pair like [`NodeUid`].
99    DeviceUid
100);
101
102/// Globally addresses a Drive node: a [`LinkId`] qualified by its [`VolumeId`].
103#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
104pub struct NodeUid {
105    pub volume_id: VolumeId,
106    pub link_id: LinkId,
107}
108
109impl NodeUid {
110    pub fn new(volume_id: VolumeId, link_id: LinkId) -> Self {
111        Self { volume_id, link_id }
112    }
113}
114
115impl fmt::Display for NodeUid {
116    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117        write!(f, "{}~{}", self.volume_id, self.link_id)
118    }
119}