aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs25
-rw-r--r--src/models.rs88
2 files changed, 66 insertions, 47 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 5d9919a..637dda2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -111,7 +111,7 @@ impl NotionApi {
Ok(NotionApi::make_json_request(
self.client.get(&format!(
"https://api.notion.com/v1/blocks/{block_id}/children",
- block_id = block_id.id().id()
+ block_id = block_id.id()
))
).await?)
}
@@ -123,8 +123,8 @@ mod tests {
use crate::models::search::{
DatabaseQuery, FilterCondition, FilterProperty, FilterValue, NotionSearch, TextCondition,
};
- use crate::models::Object;
- use crate::NotionApi;
+ use crate::models::{Object, BlockId};
+ use crate::{NotionApi, Identifiable};
fn test_token() -> String {
let token = {
@@ -225,19 +225,14 @@ mod tests {
})
.await?;
- for page in search_response.results() {
- let response = api
- .get_block_children(page)
- .await?;
- }
-
+ println!("{:?}", search_response.results.len());
- // let db = response.results()[0].clone();
- //
- // // todo: fix this clone issue
- // let db_result = api.get_database(db.clone()).await?;
- //
- // assert_eq!(db, db_result);
+ for object in search_response.results {
+ let _block = match object {
+ Object::Page { page } => api.get_block_children(BlockId::from(page.id())).await.unwrap(),
+ _ => panic!("Should not have received anything but pages!")
+ };
+ }
Ok(())
}
diff --git a/src/models.rs b/src/models.rs
index cb29e00..9f2cd14 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -135,38 +135,6 @@ pub struct Page {
parent: Parent,
}
-impl Identifiable for String {
- type Type = String;
-
- fn id(&self) -> &Self::Type {
- self
- }
-}
-
-#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Clone)]
-#[serde(transparent)]
-pub struct BlockId(String);
-
-impl BlockId {
- pub fn id(&self) -> &str {
- &self.0
- }
-}
-
-impl Identifiable for BlockId {
- type Type = BlockId;
-
- fn id(&self) -> &Self::Type {
- self
- }
-}
-
-impl Display for BlockId {
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- self.0.fmt(f)
- }
-}
-
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub enum BlockType {
@@ -268,6 +236,34 @@ pub enum Block {
Unsupported {}
}
+impl Identifiable for Block {
+ type Type = BlockId;
+
+ fn id(&self) -> &Self::Type {
+ match self {
+ Block::Paragraph { common, paragraph: _ } => &common.id,
+ Block::Heading1 { common, heading_1: _} => &common.id,
+ Block::Heading2 { common, heading_2: _} => &common.id,
+ Block::Heading3 { common, heading_3: _} => &common.id,
+ Block::BulletedListItem { common, bulleted_list_item: _} => &common.id,
+ Block::NumberedListItem { common, numbered_list_item: _} => &common.id,
+ Block::ToDo { common, to_do: _ } => &common.id,
+ Block::Toggle { common, toggle: _} => &common.id,
+ Block::ChildPage { common, child_page: _} => &common.id,
+ Block::Unsupported {} => { panic!("Trying to reference identifier for unsupported block!") }
+ }
+
+ }
+}
+
+impl Identifiable for Page {
+ type Type = PageId;
+
+ fn id(&self) -> &Self::Type {
+ &self.id
+ }
+}
+
#[derive(Eq, Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(tag = "object")]
#[serde(rename_all = "snake_case")]
@@ -294,6 +290,34 @@ pub enum Object {
},
}
+#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Clone)]
+#[serde(transparent)]
+pub struct BlockId(String);
+
+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 Display for BlockId {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ self.0.fmt(f)
+ }
+}
+
impl Object {
pub fn is_database(&self) -> bool {
matches!(self, Object::Database { .. })