use core::fmt;
use alloc::{
string::{String, ToString},
vec,
vec::Vec,
};
use secrecy::SecretString;
use serde::{Serialize, Serializer};
use thiserror::Error;
use crate::{
calendars::{JMAP_CALENDARS_CAPABILITY, calendar::JmapCalendar},
coroutine::*,
jmap_try,
rfc8620::{JMAP_CORE_CAPABILITY, get::*, session::JmapSession},
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum JmapCalendarProperty {
Id,
Name,
Description,
Color,
SortOrder,
IsDefault,
IsSubscribed,
IsVisible,
IncludeInAvailability,
DefaultAlertsWithTime,
DefaultAlertsWithoutTime,
TimeZone,
ShareWith,
MyRights,
}
impl fmt::Display for JmapCalendarProperty {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Id => "id",
Self::Name => "name",
Self::Description => "description",
Self::Color => "color",
Self::SortOrder => "sortOrder",
Self::IsDefault => "isDefault",
Self::IsSubscribed => "isSubscribed",
Self::IsVisible => "isVisible",
Self::IncludeInAvailability => "includeInAvailability",
Self::DefaultAlertsWithTime => "defaultAlertsWithTime",
Self::DefaultAlertsWithoutTime => "defaultAlertsWithoutTime",
Self::TimeZone => "timeZone",
Self::ShareWith => "shareWith",
Self::MyRights => "myRights",
})
}
}
impl Serialize for JmapCalendarProperty {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.collect_str(self)
}
}
#[derive(Debug, Error)]
pub enum JmapCalendarGetError {
#[error("JMAP Calendar/get failed: {0}")]
Get(#[from] JmapGetError),
}
#[derive(Clone, Debug, Default)]
pub struct JmapCalendarGetOptions {
pub ids: Option<Vec<String>>,
pub properties: Option<Vec<JmapCalendarProperty>>,
}
#[derive(Clone, Debug)]
pub struct JmapCalendarGetOutput {
pub calendars: Vec<JmapCalendar>,
pub not_found: Vec<String>,
pub new_state: String,
pub keep_alive: bool,
}
pub struct JmapCalendarGet {
state: State,
}
impl JmapCalendarGet {
pub fn new(
session: &JmapSession,
http_auth: &SecretString,
opts: JmapCalendarGetOptions,
) -> Result<Self, JmapCalendarGetError> {
let account_id = session
.primary_accounts
.get(JMAP_CALENDARS_CAPABILITY)
.cloned()
.unwrap_or_default();
let api_url = &session.api_url;
let properties = opts
.properties
.map(|props| props.iter().map(ToString::to_string).collect());
Ok(Self {
state: State::Get(JmapGet::new(
account_id,
http_auth,
api_url,
"Calendar/get",
vec![
JMAP_CORE_CAPABILITY.into(),
JMAP_CALENDARS_CAPABILITY.into(),
],
JmapGetOptions {
ids: opts.ids,
properties,
},
)?),
})
}
}
impl JmapCoroutine for JmapCalendarGet {
type Yield = JmapYield;
type Return = Result<JmapCalendarGetOutput, JmapCalendarGetError>;
fn resume(&mut self, arg: Option<&[u8]>) -> JmapCoroutineState<Self::Yield, Self::Return> {
match &mut self.state {
State::Get(get) => {
let JmapGetOutput {
list,
not_found,
state,
keep_alive,
} = jmap_try!(get, arg);
JmapCoroutineState::Complete(Ok(JmapCalendarGetOutput {
calendars: list,
not_found,
new_state: state,
keep_alive,
}))
}
}
}
}
enum State {
Get(JmapGet<JmapCalendar>),
}