use crate::{
ActivityRecord, BackendContext, Capabilities, EndInput, Error, LayoutActivityRecord,
LayoutEndInput, LayoutStartInput, LayoutUpdateInput, PermissionKind, Permissions, PushTokens,
Result, StartInput, UpdateInput,
};
use std::{future::Future, pin::Pin, sync::Arc};
pub type BackendFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T>> + Send + 'a>>;
fn unsupported<'a, T: 'a>(message: &'static str) -> BackendFuture<'a, T> {
Box::pin(async move { Err(Error::unsupported_feature(message)) })
}
pub trait LiveActivityBackend: Send + Sync + 'static {
fn attach(&self, events: BackendContext) -> Result<()> {
let _ = events;
Ok(())
}
fn capabilities(&self) -> BackendFuture<'_, Capabilities>;
fn permissions(&self) -> BackendFuture<'_, Permissions>;
fn start(&self, input: StartInput) -> BackendFuture<'_, ActivityRecord>;
fn update(&self, input: UpdateInput) -> BackendFuture<'_, ActivityRecord>;
fn end(&self, input: EndInput) -> BackendFuture<'_, ActivityRecord>;
fn list(&self) -> BackendFuture<'_, Vec<ActivityRecord>>;
fn request_permissions(&self, kind: PermissionKind) -> BackendFuture<'_, Permissions> {
let _ = kind;
unsupported("This backend cannot request permissions.")
}
fn start_layout(&self, input: LayoutStartInput) -> BackendFuture<'_, LayoutActivityRecord> {
let _ = input;
unsupported("This backend draws no layouts.")
}
fn update_layout(&self, input: LayoutUpdateInput) -> BackendFuture<'_, LayoutActivityRecord> {
let _ = input;
unsupported("This backend draws no layouts.")
}
fn end_layout(&self, input: LayoutEndInput) -> BackendFuture<'_, LayoutActivityRecord> {
let _ = input;
unsupported("This backend draws no layouts.")
}
fn list_layout(&self) -> BackendFuture<'_, Vec<LayoutActivityRecord>> {
Box::pin(async { Ok(Vec::new()) })
}
fn write_image(&self, name: String, png: Vec<u8>) -> BackendFuture<'_, ()> {
let _ = (name, png);
unsupported("This backend stores no images.")
}
fn push_tokens(&self) -> BackendFuture<'_, PushTokens> {
Box::pin(async { Ok(PushTokens::default()) })
}
fn sync_events(&self) -> BackendFuture<'_, ()> {
Box::pin(async { Ok(()) })
}
fn open_promotion_settings(&self) -> BackendFuture<'_, ()> {
unsupported("This backend has no settings of promotion.")
}
}
impl<T: LiveActivityBackend + ?Sized> LiveActivityBackend for Arc<T> {
fn attach(&self, events: BackendContext) -> Result<()> {
(**self).attach(events)
}
fn capabilities(&self) -> BackendFuture<'_, Capabilities> {
(**self).capabilities()
}
fn permissions(&self) -> BackendFuture<'_, Permissions> {
(**self).permissions()
}
fn start(&self, input: StartInput) -> BackendFuture<'_, ActivityRecord> {
(**self).start(input)
}
fn update(&self, input: UpdateInput) -> BackendFuture<'_, ActivityRecord> {
(**self).update(input)
}
fn end(&self, input: EndInput) -> BackendFuture<'_, ActivityRecord> {
(**self).end(input)
}
fn list(&self) -> BackendFuture<'_, Vec<ActivityRecord>> {
(**self).list()
}
fn request_permissions(&self, kind: PermissionKind) -> BackendFuture<'_, Permissions> {
(**self).request_permissions(kind)
}
fn start_layout(&self, input: LayoutStartInput) -> BackendFuture<'_, LayoutActivityRecord> {
(**self).start_layout(input)
}
fn update_layout(&self, input: LayoutUpdateInput) -> BackendFuture<'_, LayoutActivityRecord> {
(**self).update_layout(input)
}
fn end_layout(&self, input: LayoutEndInput) -> BackendFuture<'_, LayoutActivityRecord> {
(**self).end_layout(input)
}
fn list_layout(&self) -> BackendFuture<'_, Vec<LayoutActivityRecord>> {
(**self).list_layout()
}
fn write_image(&self, name: String, png: Vec<u8>) -> BackendFuture<'_, ()> {
(**self).write_image(name, png)
}
fn push_tokens(&self) -> BackendFuture<'_, PushTokens> {
(**self).push_tokens()
}
fn sync_events(&self) -> BackendFuture<'_, ()> {
(**self).sync_events()
}
fn open_promotion_settings(&self) -> BackendFuture<'_, ()> {
(**self).open_promotion_settings()
}
}