pub use crate::generated::services::schedules::{ListScheduleEntriesParams, SchedulesService};
use crate::error::Error;
use crate::generated::types::{ReplaceScheduleEntryRequestContent, ScheduleEntry};
use crate::types::FlexibleTime;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct UpdateScheduleEntryRequest {
pub summary: Option<String>,
pub description: Option<String>,
pub all_day: Option<bool>,
pub starts_at: Option<FlexibleTime>,
pub ends_at: Option<FlexibleTime>,
pub participant_ids: Option<Vec<i64>>,
pub url: Option<String>,
pub highlighted: Option<bool>,
pub notify: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScheduleEntryFields {
pub summary: String,
pub description: String,
pub all_day: bool,
pub starts_at: FlexibleTime,
pub ends_at: FlexibleTime,
participant_ids: Option<Vec<i64>>,
url: Option<String>,
highlighted: Option<bool>,
notify: Option<bool>,
addressed: Addressed,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct Addressed {
participant_ids: bool,
url: bool,
highlighted: bool,
notify: bool,
}
impl ScheduleEntryFields {
pub fn participant_ids(&self) -> Option<&[i64]> {
self.participant_ids.as_deref()
}
pub fn url(&self) -> Option<&str> {
self.url.as_deref()
}
pub fn highlighted(&self) -> Option<bool> {
self.highlighted
}
pub fn notify(&self) -> Option<bool> {
self.notify
}
pub fn set_participant_ids(&mut self, participant_ids: Vec<i64>) {
self.participant_ids = Some(participant_ids);
self.addressed.participant_ids = true;
}
pub fn set_url(&mut self, url: impl Into<String>) {
self.url = Some(url.into());
self.addressed.url = true;
}
pub fn set_highlighted(&mut self, highlighted: bool) {
self.highlighted = Some(highlighted);
self.addressed.highlighted = true;
}
pub fn set_notify(&mut self, notify: bool) {
self.notify = Some(notify);
self.addressed.notify = true;
}
fn from_entry(entry: &ScheduleEntry) -> Result<ScheduleEntryFields, Error> {
require_rendered(&entry.summary, "summary")?;
require_rendered(entry.starts_at.as_str(), "starts_at")?;
require_rendered(entry.ends_at.as_str(), "ends_at")?;
Ok(ScheduleEntryFields {
summary: entry.summary.clone(),
description: entry.description.clone().unwrap_or_default(),
all_day: entry.all_day,
starts_at: entry.starts_at.clone(),
ends_at: entry.ends_at.clone(),
participant_ids: entry
.participants
.as_ref()
.map(|people| people.iter().map(|person| person.id).collect()),
url: entry.join_url.clone(),
highlighted: entry.highlighted,
notify: None,
addressed: Addressed::default(),
})
}
fn apply(&mut self, request: &UpdateScheduleEntryRequest) {
if let Some(summary) = &request.summary {
self.summary.clone_from(summary);
}
if let Some(description) = &request.description {
self.description.clone_from(description);
}
if let Some(all_day) = request.all_day {
self.all_day = all_day;
}
if let Some(starts_at) = &request.starts_at {
self.starts_at.clone_from(starts_at);
}
if let Some(ends_at) = &request.ends_at {
self.ends_at.clone_from(ends_at);
}
if let Some(participant_ids) = &request.participant_ids {
self.set_participant_ids(participant_ids.clone());
}
if let Some(url) = &request.url {
self.set_url(url.clone());
}
if let Some(highlighted) = request.highlighted {
self.set_highlighted(highlighted);
}
if let Some(notify) = request.notify {
self.set_notify(notify);
}
}
fn into_body(self) -> ReplaceScheduleEntryRequestContent {
let addressed = self.addressed;
ReplaceScheduleEntryRequestContent {
summary: Some(self.summary),
starts_at: self.starts_at.0,
ends_at: self.ends_at.0,
description: Some(self.description),
participant_ids: addressed
.participant_ids
.then(|| self.participant_ids.unwrap_or_default()),
all_day: Some(self.all_day),
notify: addressed.notify.then_some(self.notify).flatten(),
url: addressed.url.then(|| self.url.unwrap_or_default()),
highlighted: addressed.highlighted.then_some(self.highlighted).flatten(),
}
}
}
fn require_rendered(value: &str, key: &str) -> Result<(), Error> {
if value.trim().is_empty() {
Err(Error::malformed_response(format!(
"GetScheduleEntry returned a schedule entry with a blank \"{key}\", which the API \
never renders"
))
.with_hint(MALFORMED_HINT))
} else {
Ok(())
}
}
const MALFORMED_HINT: &str = "The merge-safe update_entry and edit_entry resend this \
record's fields verbatim, so a malformed response cannot be \
written back safely. Use replace_entry to write the record \
deliberately.";
impl SchedulesService<'_> {
pub async fn update_entry(
&self,
entry_id: i64,
request: &UpdateScheduleEntryRequest,
) -> Result<ScheduleEntry, Error> {
let mut fields = ScheduleEntryFields::from_entry(&self.get_entry(entry_id).await?)?;
fields.apply(request);
self.replace_entry(entry_id, &fields.into_body()).await
}
pub async fn edit_entry<F>(&self, entry_id: i64, mutate: F) -> Result<ScheduleEntry, Error>
where
F: FnOnce(&mut ScheduleEntryFields) -> Result<(), Error>,
{
let mut fields = ScheduleEntryFields::from_entry(&self.get_entry(entry_id).await?)?;
mutate(&mut fields)?;
self.replace_entry(entry_id, &fields.into_body()).await
}
}