Macro for code de-duplication

This commit is contained in:
daladim 2021-11-05 23:05:25 +01:00
parent d6e93d846e
commit 49bdb3d199

View file

@ -19,48 +19,26 @@ pub enum Item {
Task(crate::task::Task), Task(crate::task::Task),
} }
/// Returns `task.$property_name` or `event.$property_name`, depending on whether self is a Task or an Event
macro_rules! synthetise_common_getter {
($property_name:ident, $return_type:ty) => {
pub fn $property_name(&self) -> $return_type {
match self {
Item::Event(e) => e.$property_name(),
Item::Task(t) => t.$property_name(),
}
}
}
}
impl Item { impl Item {
pub fn id(&self) -> &ItemId { synthetise_common_getter!(id, &ItemId);
match self { synthetise_common_getter!(uid, &str);
Item::Event(e) => e.id(), synthetise_common_getter!(name, &str);
Item::Task(t) => t.id(), synthetise_common_getter!(creation_date, Option<&DateTime<Utc>>);
} synthetise_common_getter!(last_modified, &DateTime<Utc>);
} synthetise_common_getter!(sync_status, &SyncStatus);
pub fn uid(&self) -> &str {
match self {
Item::Event(e) => e.uid(),
Item::Task(t) => t.uid(),
}
}
pub fn name(&self) -> &str {
match self {
Item::Event(e) => e.name(),
Item::Task(t) => t.name(),
}
}
pub fn creation_date(&self) -> Option<&DateTime<Utc>> {
match self {
Item::Event(e) => e.creation_date(),
Item::Task(t) => t.creation_date(),
}
}
pub fn last_modified(&self) -> &DateTime<Utc> {
match self {
Item::Event(e) => e.last_modified(),
Item::Task(t) => t.last_modified(),
}
}
pub fn sync_status(&self) -> &SyncStatus {
match self {
Item::Event(e) => e.sync_status(),
Item::Task(t) => t.sync_status(),
}
}
pub fn set_sync_status(&mut self, new_status: SyncStatus) { pub fn set_sync_status(&mut self, new_status: SyncStatus) {
match self { match self {
Item::Event(e) => e.set_sync_status(new_status), Item::Event(e) => e.set_sync_status(new_status),