Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Rename Indentifible trait to AsIdentifier #11

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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> {
Expand All @@ -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
Expand All @@ -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> {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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!"),
};
}
Expand Down
50 changes: 18 additions & 32 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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, .. }
Expand All @@ -236,19 +224,23 @@ 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!")
}
}
}
}

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()
}
}

Expand Down Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this now that you can identify a BlockId directly from the Page object?

fn from(page_id: PageId) -> Self {
BlockId(page_id.0.clone())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was noticing that page_id didn't have a to_string() method whereas database_id did. What is the 0 accessing?

}
}

Expand Down