jmap_server/caller.rs
1//! The caller context the in-repo reference `MemoryBackend`s accept.
2
3use jmap_types::Id;
4
5/// A caller context the in-repo reference `MemoryBackend`s accept as their
6/// [`JmapAccounts::CallerCtx`](crate::JmapAccounts::CallerCtx): anything
7/// that names the caller's Principal, or names none.
8///
9/// Every reference backend in this workspace is generic over one
10/// `C: MemoryCaller`, with [`Id`] as the default, and its
11/// [`JmapAccounts::principal_id`](crate::JmapAccounts::principal_id)
12/// returns [`MemoryCaller::principal`]. A deployment therefore picks one
13/// caller type and every family's memory backend takes it, so one
14/// [`Dispatcher`](crate::Dispatcher) of that type hosts all their
15/// handlers.
16///
17/// - [`Id`]: the caller is the Principal with that id. A backend that
18/// models per-user rights (sharing, owners, moderators) checks them
19/// against it; a single-user backend treats every caller as the account
20/// owner.
21/// - `()`: a deployment without identity. Every caller acts as the account
22/// owner, with all rights.
23///
24/// A production deployment implements its own backends against its own
25/// caller type and does not need this trait; it exists so the reference
26/// backends agree on one.
27pub trait MemoryCaller: Clone + Send + Sync + 'static {
28 /// The caller's Principal id, or `None` for a deployment without
29 /// identity, whose every caller acts as the account owner.
30 fn principal(&self) -> Option<&Id>;
31}
32
33/// The caller is the Principal with this id.
34impl MemoryCaller for Id {
35 fn principal(&self) -> Option<&Id> {
36 Some(self)
37 }
38}
39
40/// A deployment without identity: every caller acts as the account owner.
41impl MemoryCaller for () {
42 fn principal(&self) -> Option<&Id> {
43 None
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn id_names_itself_and_unit_names_nobody() {
53 let id = Id::from("alice");
54 assert_eq!(id.principal(), Some(&Id::from("alice")));
55 assert_eq!(().principal(), None);
56 }
57}