aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jake Swenson <jake@jakeswenson.com> 2021-09-22 14:13:05 -0700
committerGravatar Jake Swenson <jake@jakeswenson.com> 2021-09-22 14:13:05 -0700
commitf34ba4d79682665603d5ce3192b5422aab3f6800 (patch)
treed2bded634af25a2bfe7e5ab817b14a5759e0f5ef
parent50faf778671358d70fa967735a062b4a6b6dfe64 (diff)
downloadnotion-f34ba4d79682665603d5ce3192b5422aab3f6800.tar.gz
notion-f34ba4d79682665603d5ce3192b5422aab3f6800.tar.zst
notion-f34ba4d79682665603d5ce3192b5422aab3f6800.zip
feat: Paging improvements and ergonomics
-rw-r--r--src/models.rs17
-rw-r--r--src/models/paging.rs2
-rw-r--r--src/models/properties.rs8
-rw-r--r--src/models/properties/tests.rs6
-rw-r--r--src/models/properties/tests/text_with_link.json25
-rw-r--r--src/models/search.rs9
-rw-r--r--src/models/text.rs4
7 files changed, 61 insertions, 10 deletions
diff --git a/src/models.rs b/src/models.rs
index aead105..0007cfb 100644
--- a/src/models.rs
+++ b/src/models.rs
@@ -167,6 +167,17 @@ pub struct Properties {
pub properties: HashMap<String, PropertyValue>,
}
+impl Properties {
+ pub fn title(&self) -> Option<String> {
+ self.properties.values().find_map(|p| match p {
+ PropertyValue::Title { title, .. } => {
+ Some(title.into_iter().map(|t| t.plain_text()).collect())
+ }
+ _ => None,
+ })
+ }
+}
+
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct Page {
pub id: PageId,
@@ -180,6 +191,12 @@ pub struct Page {
pub parent: Parent,
}
+impl Page {
+ pub fn title(&self) -> Option<String> {
+ self.properties.title()
+ }
+}
+
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct BlockCommon {
pub id: BlockId,
diff --git a/src/models/paging.rs b/src/models/paging.rs
index 481c235..0f42f23 100644
--- a/src/models/paging.rs
+++ b/src/models/paging.rs
@@ -13,5 +13,5 @@ pub struct Paging {
}
pub trait Pageable {
- fn start_from(self, starting_point: Paging) -> Self;
+ fn start_from(self, starting_point: Option<PagingCursor>) -> Self;
}
diff --git a/src/models/properties.rs b/src/models/properties.rs
index 95ddef3..b46b7f7 100644
--- a/src/models/properties.rs
+++ b/src/models/properties.rs
@@ -263,7 +263,7 @@ pub enum PropertyValue {
/// <https://developers.notion.com/reference/page#number-property-values>
Number {
id: PropertyId,
- number: Number,
+ number: Option<Number>,
},
/// <https://developers.notion.com/reference/page#select-property-values>
Select {
@@ -276,7 +276,7 @@ pub enum PropertyValue {
},
Date {
id: PropertyId,
- date: DateValue,
+ date: Option<DateValue>,
},
/// <https://developers.notion.com/reference/page#formula-property-values>
Formula {
@@ -290,7 +290,7 @@ pub enum PropertyValue {
},
Rollup {
id: PropertyId,
- relation: Rollup,
+ relation: Option<Rollup>,
},
People {
id: PropertyId,
@@ -310,7 +310,7 @@ pub enum PropertyValue {
},
Email {
id: PropertyId,
- email: String,
+ email: Option<String>,
},
PhoneNumber {
id: PropertyId,
diff --git a/src/models/properties/tests.rs b/src/models/properties/tests.rs
index 23b376d..8fe5b42 100644
--- a/src/models/properties/tests.rs
+++ b/src/models/properties/tests.rs
@@ -26,3 +26,9 @@ fn parse_select_property() {
let _property: PropertyValue =
serde_json::from_str(include_str!("tests/select_property.json")).unwrap();
}
+
+#[test]
+fn parse_text_property_with_link() {
+ let _property: PropertyValue =
+ serde_json::from_str(include_str!("tests/text_with_link.json")).unwrap();
+}
diff --git a/src/models/properties/tests/text_with_link.json b/src/models/properties/tests/text_with_link.json
new file mode 100644
index 0000000..6a8e081
--- /dev/null
+++ b/src/models/properties/tests/text_with_link.json
@@ -0,0 +1,25 @@
+{
+ "id": "6BZi",
+ "type": "rich_text",
+ "rich_text": [
+ {
+ "type": "text",
+ "text": {
+ "content": "https://notion.so/",
+ "link": {
+ "url": "https://notion.so/"
+ }
+ },
+ "annotations": {
+ "bold": false,
+ "italic": false,
+ "strikethrough": false,
+ "underline": false,
+ "code": false,
+ "color": "default"
+ },
+ "plain_text": "https://notion.so/",
+ "href": "https://notion.so/"
+ }
+ ]
+}
diff --git a/src/models/search.rs b/src/models/search.rs
index 22dab76..e7d8189 100644
--- a/src/models/search.rs
+++ b/src/models/search.rs
@@ -1,5 +1,5 @@
use crate::ids::{PageId, UserId};
-use crate::models::paging::{Pageable, Paging};
+use crate::models::paging::{Pageable, Paging, PagingCursor};
use crate::models::Number;
use chrono::{DateTime, Utc};
use serde::ser::SerializeMap;
@@ -301,9 +301,12 @@ pub struct DatabaseQuery {
}
impl Pageable for DatabaseQuery {
- fn start_from(self, starting_point: Paging) -> Self {
+ fn start_from(self, starting_point: Option<PagingCursor>) -> Self {
DatabaseQuery {
- paging: Some(starting_point),
+ paging: Some(Paging {
+ start_cursor: starting_point,
+ page_size: self.paging.and_then(|p| p.page_size),
+ }),
..self
}
}
diff --git a/src/models/text.rs b/src/models/text.rs
index b960b6d..16274ab 100644
--- a/src/models/text.rs
+++ b/src/models/text.rs
@@ -45,7 +45,7 @@ pub struct RichTextCommon {
pub annotations: Option<Annotations>,
}
-#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
+#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct Link {
pub url: String,
}
@@ -53,7 +53,7 @@ pub struct Link {
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct Text {
pub content: String,
- pub link: Option<String>,
+ pub link: Option<Link>,
}
/// Rich text objects contain data for displaying formatted text, mentions, and equations.