Skip to content

Commit 3d7ae49

Browse files
committed
Fix hang bug, beatify logs
1 parent a5813d0 commit 3d7ae49

File tree

7 files changed

+79
-29
lines changed

7 files changed

+79
-29
lines changed

notion-async-api/src/api.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ impl RequestError {
4141

4242
impl Display for RequestError {
4343
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44-
match self {
45-
RequestError::InvalidRequest(s) => write!(f, "invalid request: {s}"),
46-
RequestError::InvalidResponse(s) => write!(f, "invalid response: {s}"),
47-
RequestError::RetryAfter(s) => write!(f, "retry after: {s}"),
48-
RequestError::Other(e) => write!(f, "request error: {e:?}"),
49-
}
44+
let s = match self {
45+
RequestError::InvalidRequest(s) => format!("invalid request: {s}"),
46+
RequestError::InvalidResponse(s) => format!("invalid response: {s}"),
47+
RequestError::RetryAfter(s) => format!("retry after: {s}"),
48+
RequestError::Other(e) => format!("request error: {e:?}"),
49+
};
50+
Display::fmt(&s, f)
5051
}
5152
}
5253

notion-async-api/src/block.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ use serde::{Deserialize, Serialize};
66
use serde_json::Value;
77
use serde_with::serde_as;
88

9-
use crate::object::{Object, ObjectCommon};
9+
use crate::{
10+
misc::Unquotes,
11+
object::{Object, ObjectCommon},
12+
};
1013

1114
/// Refer to:
1215
/// - [Notion JSON conventions](https://developers.notion.com/reference/intro#json-conventions)
@@ -78,7 +81,7 @@ pub enum BlockType {
7881
impl Display for BlockType {
7982
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8083
let s = serde_json::to_string(self).unwrap_or("".to_owned());
81-
f.write_str(&s)
84+
s.unquotes().fmt(f)
8285
}
8386
}
8487

notion-async-api/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl NotionError {
3333
impl Display for NotionError {
3434
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3535
match self {
36-
NotionError::InvalidObject(s) => write!(f, "invalid notion object: {s}"),
36+
NotionError::InvalidObject(s) => format!("invalid notion object: {s}").fmt(f),
3737
NotionError::RequestFailed(e) => e.fmt(f),
3838
}
3939
}

notion-async-api/src/fetcher.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,17 @@ impl Fetcher {
152152
async move {
153153
let (task_tx, mut task_rx) = channel(10);
154154

155-
this.do_task(task, res_tx.clone(), task_tx).await;
155+
{
156+
let this = this.clone();
157+
let res_tx = res_tx.clone();
158+
spawn(async move {
159+
while let Some(task) = task_rx.next().await {
160+
this.do_task_recurs(task, res_tx.clone()).await;
161+
}
162+
});
163+
}
156164

157-
spawn(async move {
158-
while let Some(task) = task_rx.next().await {
159-
this.do_task_recurs(task, res_tx.clone()).await;
160-
}
161-
});
165+
this.do_task(task, res_tx.clone(), task_tx).await;
162166
}
163167
.boxed()
164168
}

notion-async-api/src/misc.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::BTreeMap;
2+
use std::ops::Deref;
23
use std::{fmt::Display, str::FromStr};
34

45
use chrono::{DateTime, Utc};
@@ -33,7 +34,7 @@ pub enum Icon {
3334
impl Display for Icon {
3435
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3536
let s = serde_json::to_string(self).unwrap();
36-
f.write_str(&s)
37+
s.unquotes().fmt(f)
3738
}
3839
}
3940

@@ -47,7 +48,7 @@ pub enum NotionFile {
4748
impl Display for NotionFile {
4849
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4950
let s = serde_json::to_string(self).unwrap();
50-
f.write_str(&s)
51+
s.unquotes().fmt(f)
5152
}
5253
}
5354

@@ -69,7 +70,7 @@ impl Display for NotionFileType {
6970
NotionFileType::File => "file",
7071
NotionFileType::External => "external",
7172
};
72-
f.write_str(s)
73+
s.fmt(f)
7374
}
7475
}
7576

@@ -105,3 +106,29 @@ pub struct DateProperty {
105106
pub struct UrlData {
106107
url: String,
107108
}
109+
110+
pub(crate) trait Unquotes {
111+
fn unquotes(&self) -> &str;
112+
}
113+
114+
// impl Unquotes for &str {
115+
// fn unquotes(&self) -> String {
116+
// let s = match self.strip_prefix("\"").and_then(|x| x.strip_suffix("\"")) {
117+
// Some(ss) => ss,
118+
// None => self,
119+
// };
120+
// s.to_owned()
121+
// }
122+
// }
123+
124+
impl<T> Unquotes for T
125+
where
126+
T: Deref<Target = str>,
127+
{
128+
fn unquotes(&self) -> &str {
129+
match self.strip_prefix("\"").and_then(|x| x.strip_suffix("\"")) {
130+
Some(ss) => ss,
131+
None => self,
132+
}
133+
}
134+
}

notion-async-api/src/object.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
77
use serde_with::serde_as;
88
use thiserror::Error;
99

10+
use crate::misc::Unquotes;
1011
use crate::user::User;
1112

1213
pub trait Object: Send {
@@ -58,7 +59,15 @@ pub enum ObjectType {
5859

5960
impl Display for ObjectType {
6061
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61-
f.write_str(&serde_json::to_string(self).unwrap())
62+
let s = match self {
63+
ObjectType::Block => "block",
64+
ObjectType::Page => "page",
65+
ObjectType::Database => "database",
66+
ObjectType::User => "user",
67+
ObjectType::Comment => "comment",
68+
ObjectType::List => "list",
69+
};
70+
s.fmt(f)
6271
}
6372
}
6473

@@ -150,7 +159,8 @@ pub enum ParentType {
150159

151160
impl Display for ParentType {
152161
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
153-
f.write_str(&serde_json::to_string(self).unwrap())
162+
let s = serde_json::to_string(self).unwrap();
163+
s.unquotes().fmt(f)
154164
}
155165
}
156166

@@ -159,7 +169,7 @@ pub struct ImpossibleParseError;
159169

160170
impl Display for ImpossibleParseError {
161171
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
162-
f.write_str("ImpossibleParseError")
172+
"ImpossibleParseError".fmt(f)
163173
}
164174
}
165175

@@ -172,6 +182,6 @@ pub struct JsonObject {
172182
impl Display for JsonObject {
173183
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
174184
let s = serde_json::to_string(self).unwrap();
175-
f.write_str(&s)
185+
s.unquotes().fmt(f)
176186
}
177187
}

src/main.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,33 +130,38 @@ async fn run_sync(token: &str, page_id: &str, db: &mut SqliteConnection) {
130130
// println!("⏬ {} {}", obj.object_type(), obj.id());
131131
// save_object(&obj, "testdata").await?;
132132
} else {
133-
println!("🔁 repeated {} {}", obj.object_type(), obj.id());
133+
eprintln!("➡️ 🔁 repeated {} {}", obj.object_type(), obj.id());
134134
}
135135

136136
match obj {
137137
notion_async_api::AnyObject::Block(block) => {
138-
println!("⏬ 🆎 block {} {}", block.id(), block.block_type);
138+
println!(
139+
"✔  {:8} {} {}",
140+
block.object_type(),
141+
block.id(),
142+
block.block_type
143+
);
139144
insert_or_update_block(db, block).await.unwrap();
140145
}
141146
notion_async_api::AnyObject::Page(page) => {
142-
println!(" 📃 page {}", page.id());
147+
println!(" 📃 {:8} {}", page.object_type(), page.id());
143148
insert_or_update_page(db, page).await.unwrap();
144149
}
145150
notion_async_api::AnyObject::Database(database) => {
146-
println!("⏬ 🗐 database {}", database.id());
151+
println!("✔  {:8} {}", database.object_type(), database.id());
147152
insert_or_update_database(db, database).await.unwrap();
148153
}
149154
notion_async_api::AnyObject::User(user) => {
150-
println!(" 👤 user {}", user.id());
155+
println!("✔️ 👤 {:8} {}", user.object_type(), user.id());
151156
}
152157
notion_async_api::AnyObject::Comment(comment) => {
153-
println!("⏬ 📝 comment {}", comment.id(),);
158+
println!("✔  {:8} {}", comment.object_type(), comment.id(),);
154159
insert_or_update_comment(db, comment).await.unwrap();
155160
}
156161
};
157162
}
158163
Err(e) => {
159-
println!("❌ {e}");
164+
eprintln!("❌ error {e}");
160165
}
161166
}
162167
}

0 commit comments

Comments
 (0)