summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jake Swenson <jakeswenson@users.noreply.github.com> 2021-05-17 08:26:10 -0700
committerGravatar GitHub <noreply@github.com> 2021-05-17 15:26:10 +0000
commit59613bea6ae9d5d1055e551ce4887d8b0ebd74bb (patch)
tree513ab07a78755085d5ba0bbe3f42e68f68d902b9
parent68819a887e9498c8786baaa6c3f098801b91e354 (diff)
downloadnotion-59613bea6ae9d5d1055e551ce4887d8b0ebd74bb.tar.gz
notion-59613bea6ae9d5d1055e551ce4887d8b0ebd74bb.tar.zst
notion-59613bea6ae9d5d1055e551ce4887d8b0ebd74bb.zip
refactor: Rename Indentifible trait to AsIdentifier and try to improve the usability (#11)
-rw-r--r--src/lib.rs21
-rw-r--r--src/models.rs50
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<ById> {
+ fn id(&self) -> ById;
}
pub struct NotionApi {
@@ -97,7 +95,7 @@ impl NotionApi {
.await?)
}
- pub async fn get_database<T: Identifiable<Type = DatabaseId>>(
+ pub async fn get_database<T: AsIdentifier<DatabaseId>>(
&self,
database_id: T,
) -> Result<Database, Error> {
@@ -115,7 +113,7 @@ impl NotionApi {
) -> Result<ListResponse<Page>, Error>
where
T: Into<DatabaseQuery>,
- D: Identifiable<Type = DatabaseId>,
+ D: AsIdentifier<DatabaseId>,
{
Ok(NotionApi::make_json_request(
self.client
@@ -128,7 +126,7 @@ impl NotionApi {
.await?)
}
- pub async fn get_block_children<T: Identifiable<Type = BlockId>>(
+ pub async fn get_block_children<T: AsIdentifier<BlockId>>(
&self,
block_id: T,
) -> Result<ListResponse<Block>, 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<String, PropertyConfiguration>,
}
-impl Identifiable for Database {
- type Type = DatabaseId;
-
- fn id(&self) -> &Self::Type {
- &self.id
+impl AsIdentifier<DatabaseId> 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<BlockId> 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<PageId> for Page {
+ fn id(&self) -> PageId {
+ self.id.clone()
+ }
+}
- fn id(&self) -> &Self::Type {
- &self.id
+impl AsIdentifier<BlockId> 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<PageId> for BlockId {
+ fn from(page_id: PageId) -> Self {
+ BlockId(page_id.0.clone())
}
}
n class='insertions'>+165 2023-07-29typo spawn.md (#3875)Gravatar Jhorman Tito 1-1/+1 2023-07-28Update nodejs-apis.mdGravatar Jarred Sumner 1-4/+4 2023-07-28Update nodejs-apis.mdGravatar Jarred Sumner 1-5/+5 2023-07-28Defer task destructionbun-v0.7.1Gravatar Jarred Sumner 1-1/+17 2023-07-28Stat largefile test (#3870)Gravatar Jarred Sumner 1-0/+22 2023-07-28Ignore when printing to stdout errors (#3869)Gravatar Jarred Sumner 1-2/+4 2023-07-28Fix bug with `/path/to/absolute/bun.lockb`Gravatar Jarred Sumner 1-10/+13 2023-07-28markBindingGravatar Dylan Conway 1-0/+1 2023-07-28optional parameterGravatar Dylan Conway 1-3/+3 2023-07-28Fixes #3868Gravatar Jarred Sumner 1-2/+3 2023-07-28Fix assertion failure and possible infinite loop when printing as yarn lock f...Gravatar Jarred Sumner 2-3/+16 2023-07-28Mark broken test as todoGravatar Jarred Sumner 2-1/+5 2023-07-28fix types and add message channel/port gc testGravatar Dylan Conway 2-62/+18 2023-07-28`MessageChannel` and `MessagePort` (#3860)Gravatar Dylan Conway 57-247/+3752 2023-07-28mark tests as todoGravatar Jarred Sumner 2-81/+75 2023-07-28add fork to child_process (#3851)Gravatar Vlad Sirenko 7-19/+446 2023-07-28feat(bun/test): Impl. expect().pass() & expect().fail() (#3843)Gravatar Tiramify (A.K. Daniel) 6-5/+212 2023-07-28fix the chunk boundary (`node:stream:createReadStream`) (#3853)Gravatar Ai Hoshino 3-8/+90 2023-07-28Support file: URLs in `fetch` (#3858)Gravatar Jarred Sumner 6-183/+281 2023-07-28fix(tls) exposes native canonicalizeIP and fix rootCertificates (#3866)Gravatar Ciro Spaciari 7-29/+125 2023-07-28Fixes #3795 (#3856)Gravatar Jarred Sumner 11-6/+140 2023-07-28Add todoGravatar Jarred Sumner 2-0/+8 2023-07-28Update pull_request_template.mdGravatar Jarred Sumner 1-1/+1 2023-07-28Update pull_request_template.mdGravatar Jarred Sumner 1-11/+16 2023-07-27Fix bug with // @bun annotation in main thread (#3855)Gravatar Jarred Sumner 4-2/+53 2023-07-27Add `Bun.isMainThread`Gravatar Jarred Sumner 7-3/+48 2023-07-27Uncomment testGravatar Jarred Sumner 1-1/+1 2023-07-27Resolve watch directories outside main thread + async iterator and symlink fi...Gravatar Ciro Spaciari 8-197/+523 2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-1/+1 2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-1/+1 2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-1/+1 2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-2/+2 2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-0/+4 2023-07-27Update pull_request_template.mdGravatar Jarred Sumner 1-5/+11