Skip to content

Commit

Permalink
feat: Paging improvements and ergonomics
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeswenson committed Sep 22, 2021
1 parent 50faf77 commit f34ba4d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 10 deletions.
17 changes: 17 additions & 0 deletions src/models.rs
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/models/paging.rs
Expand Up @@ -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;
}
8 changes: 4 additions & 4 deletions src/models/properties.rs
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -290,7 +290,7 @@ pub enum PropertyValue {
},
Rollup {
id: PropertyId,
relation: Rollup,
relation: Option<Rollup>,
},
People {
id: PropertyId,
Expand All @@ -310,7 +310,7 @@ pub enum PropertyValue {
},
Email {
id: PropertyId,
email: String,
email: Option<String>,
},
PhoneNumber {
id: PropertyId,
Expand Down
6 changes: 6 additions & 0 deletions src/models/properties/tests.rs
Expand Up @@ -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();
}
25 changes: 25 additions & 0 deletions 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/"
}
]
}
9 changes: 6 additions & 3 deletions 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;
Expand Down Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/text.rs
Expand Up @@ -45,15 +45,15 @@ 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,
}

#[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.
Expand Down

0 comments on commit f34ba4d

Please sign in to comment.