Skip to main content

DelegateJmapAccounts

Trait DelegateJmapAccounts 

Source
pub trait DelegateJmapAccounts:
    Send
    + Sync
    + 'static {
    type Target: JmapAccounts;

    // Required method
    fn jmap_accounts(&self) -> &Self::Target;

    // Provided methods
    fn account_exists(
        &self,
        caller: &<Self::Target as JmapAccounts>::CallerCtx,
        account_id: &Id,
    ) -> impl Future<Output = Result<bool, <Self::Target as JmapAccounts>::Error>> + Send { ... }
    fn principal_id(
        caller: &<Self::Target as JmapAccounts>::CallerCtx,
    ) -> Option<&Id> { ... }
    fn accept_language(
        caller: &<Self::Target as JmapAccounts>::CallerCtx,
    ) -> Option<&str> { ... }
    fn max_objects_in_set(
        &self,
        caller: &<Self::Target as JmapAccounts>::CallerCtx,
        account_id: &Id,
    ) -> u64 { ... }
    fn max_objects_in_get(
        &self,
        caller: &<Self::Target as JmapAccounts>::CallerCtx,
        account_id: &Id,
    ) -> u64 { ... }
    fn metadata_info<O: JmapObject>(
        &self,
        caller: &<Self::Target as JmapAccounts>::CallerCtx,
        account_id: &Id,
    ) -> Option<DataTypeMetadataInfo> { ... }
    fn i18n_info(
        &self,
        caller: &<Self::Target as JmapAccounts>::CallerCtx,
        account_id: &Id,
    ) -> Option<I18nInfo> { ... }
}
Expand description

Forward JmapAccounts to another backend, overriding only what the wrapper changes: implement this instead of JmapAccounts, and the blanket impl answers every JmapAccounts method with this trait’s method of the same name, each of which defaults to the one of Self::jmap_accounts.

For a wrapper that changes one answer (an account_exists that consults a directory, say) and keeps the wrapped backend’s others, whichever of them that backend overrides (a metadata_info or i18n_info, the max_objects_* limits): a method the wrapper does not override cannot silently fall back to the JmapAccounts default. The wrapper’s Error and CallerCtx are the target’s. Pair it with DelegateJmapReads to forward the reads too.

principal_id and accept_language are associated functions with no self (see JmapAccounts::principal_id for why), so their defaults call the target type’s functions, <Self::Target as JmapAccounts>::principal_id(caller); that is the same answer, since the caller context is the target’s. A wrapper may still override them.

A type implements either this trait or JmapAccounts, never both: the blanket impl is the JmapAccounts impl of every type that implements this one.

use jmap_server::{DelegateJmapAccounts, Id, JmapAccounts};

/// Some backend whose `max_objects_in_set` is not the default.
struct Inner;

impl JmapAccounts for Inner {
    type Error = MyError;
    type CallerCtx = ();
    async fn account_exists(&self, _: &(), _: &Id) -> Result<bool, MyError> {
        Ok(true)
    }
    fn max_objects_in_set(&self, _: &(), _: &Id) -> u64 {
        50
    }
}

/// Knows only the accounts of its directory; every other answer is
/// `Inner`'s.
struct Wrapper {
    inner: Inner,
    directory: Vec<Id>,
}

impl DelegateJmapAccounts for Wrapper {
    type Target = Inner;

    fn jmap_accounts(&self) -> &Inner {
        &self.inner
    }

    async fn account_exists(&self, _: &(), account_id: &Id) -> Result<bool, MyError> {
        Ok(self.directory.contains(account_id))
    }
}

let wrapper = Wrapper { inner: Inner, directory: vec![Id::from("a1")] };
// Through `JmapAccounts`, as a handler sees it: the limit is `Inner`'s.
assert_eq!(JmapAccounts::max_objects_in_set(&wrapper, &(), &Id::from("a1")), 50);

§Method-call syntax

The wrapper has each method twice, once from each trait. With both traits in scope, wrapper.account_exists(..) is ambiguous (E0034); call it as JmapAccounts::account_exists(&wrapper, ..). Generic code bounded by JmapAccounts (every handler) is unaffected.

Required Associated Types§

Source

type Target: JmapAccounts

The backend the answers come from.

Required Methods§

Source

fn jmap_accounts(&self) -> &Self::Target

The backend the answers come from.

Provided Methods§

Source

fn account_exists( &self, caller: &<Self::Target as JmapAccounts>::CallerCtx, account_id: &Id, ) -> impl Future<Output = Result<bool, <Self::Target as JmapAccounts>::Error>> + Send

JmapAccounts::account_exists; defaults to the target’s.

Source

fn principal_id( caller: &<Self::Target as JmapAccounts>::CallerCtx, ) -> Option<&Id>

JmapAccounts::principal_id; defaults to the target type’s.

Source

fn accept_language( caller: &<Self::Target as JmapAccounts>::CallerCtx, ) -> Option<&str>

JmapAccounts::accept_language; defaults to the target type’s.

Source

fn max_objects_in_set( &self, caller: &<Self::Target as JmapAccounts>::CallerCtx, account_id: &Id, ) -> u64

JmapAccounts::max_objects_in_set; defaults to the target’s.

Source

fn max_objects_in_get( &self, caller: &<Self::Target as JmapAccounts>::CallerCtx, account_id: &Id, ) -> u64

JmapAccounts::max_objects_in_get; defaults to the target’s.

Source

fn metadata_info<O: JmapObject>( &self, caller: &<Self::Target as JmapAccounts>::CallerCtx, account_id: &Id, ) -> Option<DataTypeMetadataInfo>

JmapAccounts::metadata_info; defaults to the target’s.

Source

fn i18n_info( &self, caller: &<Self::Target as JmapAccounts>::CallerCtx, account_id: &Id, ) -> Option<I18nInfo>

JmapAccounts::i18n_info; defaults to the target’s.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§