From 8a25f0f8165604955933b84c81d5134a80a03e4e Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 24 Apr 2020 17:30:27 -0400 Subject: [PATCH] Use an associated type instead of Generic. --- server/src/apub/community.rs | 8 ++++++-- server/src/apub/mod.rs | 10 ++++++---- server/src/apub/post.rs | 8 ++++++-- server/src/apub/user.rs | 7 +++++-- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/server/src/apub/community.rs b/server/src/apub/community.rs index 920c41ad3..05e004eee 100644 --- a/server/src/apub/community.rs +++ b/server/src/apub/community.rs @@ -5,7 +5,9 @@ pub struct CommunityQuery { community_name: String, } -impl ToApub for Community { +impl ToApub for Community { + type Response = GroupExt; + // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network. fn to_apub(&self, conn: &PgConnection) -> Result { let mut group = Group::default(); @@ -52,7 +54,9 @@ impl ActorType for Community { } } -impl FromApub for CommunityForm { +impl FromApub for CommunityForm { + type ApubType = GroupExt; + /// Parse an ActivityPub group received from another instance into a Lemmy community. fn from_apub(group: &GroupExt, conn: &PgConnection) -> Result { let oprops = &group.base.base.object_props; diff --git a/server/src/apub/mod.rs b/server/src/apub/mod.rs index d691e8390..057929688 100644 --- a/server/src/apub/mod.rs +++ b/server/src/apub/mod.rs @@ -126,12 +126,14 @@ fn is_apub_id_valid(apub_id: &Url) -> bool { } // TODO Not sure good names for these -pub trait ToApub { - fn to_apub(&self, conn: &PgConnection) -> Result; +pub trait ToApub { + type Response; + fn to_apub(&self, conn: &PgConnection) -> Result; } -pub trait FromApub { - fn from_apub(apub: &ApubType, conn: &PgConnection) -> Result +pub trait FromApub { + type ApubType; + fn from_apub(apub: &Self::ApubType, conn: &PgConnection) -> Result where Self: Sized; } diff --git a/server/src/apub/post.rs b/server/src/apub/post.rs index 7ad4394d8..51ba861ef 100644 --- a/server/src/apub/post.rs +++ b/server/src/apub/post.rs @@ -15,7 +15,9 @@ pub async fn get_apub_post( Ok(create_apub_response(&post.to_apub(&db.get().unwrap())?)) } -impl ToApub for Post { +impl ToApub for Post { + type Response = Page; + // Turn a Lemmy post into an ActivityPub page that can be sent out over the network. fn to_apub(&self, conn: &PgConnection) -> Result { let mut page = Page::default(); @@ -53,7 +55,9 @@ impl ToApub for Post { } } -impl FromApub for PostForm { +impl FromApub for PostForm { + type ApubType = Page; + /// Parse an ActivityPub page received from another instance into a Lemmy post. fn from_apub(page: &Page, conn: &PgConnection) -> Result { let oprops = &page.object_props; diff --git a/server/src/apub/user.rs b/server/src/apub/user.rs index 03492f70a..274c70a94 100644 --- a/server/src/apub/user.rs +++ b/server/src/apub/user.rs @@ -5,7 +5,9 @@ pub struct UserQuery { user_name: String, } -impl ToApub for User_ { +impl ToApub for User_ { + type Response = PersonExt; + // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network. fn to_apub(&self, _conn: &PgConnection) -> Result { // TODO go through all these to_string and to_owned() @@ -50,7 +52,8 @@ impl ActorType for User_ { } } -impl FromApub for UserForm { +impl FromApub for UserForm { + type ApubType = PersonExt; /// Parse an ActivityPub person received from another instance into a Lemmy user. fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result { let oprops = &person.base.base.object_props;