From edda34d4652596227da79ea451ac6cd5d130dbe1 Mon Sep 17 00:00:00 2001 From: Jake Swenson Date: Mon, 17 May 2021 08:22:18 -0700 Subject: [PATCH] refactor: Rename Indentifible trait to AsIdentifier and try to improve the usability --- src/lib.rs | 21 ++++++++------------- src/models.rs | 50 ++++++++++++++++++-------------------------------- 2 files changed, 26 insertions(+), 45 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e059a48..82e395f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,10 +27,8 @@ pub enum Error { JsonParseError { source: serde_json::Error }, } -pub trait Identifiable { - // There should only be one way to identify an object - type Type; - fn id(&self) -> &Self::Type; +pub trait AsIdentifier { + fn id(&self) -> ById; } pub struct NotionApi { @@ -97,7 +95,7 @@ impl NotionApi { .await?) } - pub async fn get_database>( + pub async fn get_database>( &self, database_id: T, ) -> Result { @@ -115,7 +113,7 @@ impl NotionApi { ) -> Result, Error> where T: Into, - D: Identifiable, + D: AsIdentifier, { Ok(NotionApi::make_json_request( self.client @@ -128,7 +126,7 @@ impl NotionApi { .await?) } - pub async fn get_block_children>( + pub async fn get_block_children>( &self, block_id: T, ) -> Result, Error> { @@ -146,8 +144,8 @@ mod tests { use crate::models::search::{ DatabaseQuery, FilterCondition, FilterProperty, FilterValue, NotionSearch, TextCondition, }; - use crate::models::{BlockId, Object}; - use crate::{Identifiable, NotionApi}; + use crate::models::Object; + use crate::NotionApi; fn test_token() -> String { let token = { @@ -252,10 +250,7 @@ mod tests { for object in search_response.results { match object { - Object::Page { page } => api - .get_block_children(BlockId::from(page.id())) - .await - .unwrap(), + Object::Page { page } => api.get_block_children(page).await.unwrap(), _ => panic!("Should not have received anything but pages!"), }; } diff --git a/src/models.rs b/src/models.rs index 99ee203..734ece5 100644 --- a/src/models.rs +++ b/src/models.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use std::collections::HashMap; use crate::models::paging::PagingCursor; -use crate::Identifiable; +use crate::AsIdentifier; pub use chrono::{DateTime, Utc}; pub use serde_json::value::Number; use std::fmt::{Display, Formatter}; @@ -32,14 +32,6 @@ impl DatabaseId { } } -impl Identifiable for DatabaseId { - type Type = DatabaseId; - - fn id(&self) -> &Self::Type { - self - } -} - impl Display for DatabaseId { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) @@ -68,11 +60,9 @@ pub struct Database { properties: HashMap, } -impl Identifiable for Database { - type Type = DatabaseId; - - fn id(&self) -> &Self::Type { - &self.id +impl AsIdentifier for Database { + fn id(&self) -> DatabaseId { + self.id.clone() } } @@ -222,10 +212,8 @@ pub enum Block { Unsupported, } -impl Identifiable for Block { - type Type = BlockId; - - fn id(&self) -> &Self::Type { +impl AsIdentifier for Block { + fn id(&self) -> BlockId { use Block::*; match self { Paragraph { common, .. } @@ -236,7 +224,7 @@ impl Identifiable for Block { | NumberedListItem { common, .. } | ToDo { common, .. } | Toggle { common, .. } - | ChildPage { common, .. } => &common.id, + | ChildPage { common, .. } => common.id.clone(), Unsupported {} => { panic!("Trying to reference identifier for unsupported block!") } @@ -244,11 +232,15 @@ impl Identifiable for Block { } } -impl Identifiable for Page { - type Type = PageId; +impl AsIdentifier for Page { + fn id(&self) -> PageId { + self.id.clone() + } +} - fn id(&self) -> &Self::Type { - &self.id +impl AsIdentifier for Page { + fn id(&self) -> BlockId { + self.id.clone().into() } } @@ -286,17 +278,11 @@ impl BlockId { pub fn id(&self) -> &str { &self.0 } - - pub fn from(page_id: &PageId) -> Self { - BlockId(page_id.clone().0) - } } -impl Identifiable for BlockId { - type Type = BlockId; - - fn id(&self) -> &Self::Type { - self +impl From for BlockId { + fn from(page_id: PageId) -> Self { + BlockId(page_id.0.clone()) } }