Use an associated type instead of Generic.

This commit is contained in:
Dessalines 2020-04-24 17:30:27 -04:00
parent 33c5c21a57
commit 8a25f0f816
4 changed files with 23 additions and 10 deletions

View file

@ -5,7 +5,9 @@ pub struct CommunityQuery {
community_name: String, community_name: String,
} }
impl ToApub<GroupExt> 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. // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
fn to_apub(&self, conn: &PgConnection) -> Result<GroupExt, Error> { fn to_apub(&self, conn: &PgConnection) -> Result<GroupExt, Error> {
let mut group = Group::default(); let mut group = Group::default();
@ -52,7 +54,9 @@ impl ActorType for Community {
} }
} }
impl FromApub<GroupExt> for CommunityForm { impl FromApub for CommunityForm {
type ApubType = GroupExt;
/// Parse an ActivityPub group received from another instance into a Lemmy community. /// Parse an ActivityPub group received from another instance into a Lemmy community.
fn from_apub(group: &GroupExt, conn: &PgConnection) -> Result<Self, Error> { fn from_apub(group: &GroupExt, conn: &PgConnection) -> Result<Self, Error> {
let oprops = &group.base.base.object_props; let oprops = &group.base.base.object_props;

View file

@ -126,12 +126,14 @@ fn is_apub_id_valid(apub_id: &Url) -> bool {
} }
// TODO Not sure good names for these // TODO Not sure good names for these
pub trait ToApub<Response> { pub trait ToApub {
fn to_apub(&self, conn: &PgConnection) -> Result<Response, Error>; type Response;
fn to_apub(&self, conn: &PgConnection) -> Result<Self::Response, Error>;
} }
pub trait FromApub<ApubType> { pub trait FromApub {
fn from_apub(apub: &ApubType, conn: &PgConnection) -> Result<Self, Error> type ApubType;
fn from_apub(apub: &Self::ApubType, conn: &PgConnection) -> Result<Self, Error>
where where
Self: Sized; Self: Sized;
} }

View file

@ -15,7 +15,9 @@ pub async fn get_apub_post(
Ok(create_apub_response(&post.to_apub(&db.get().unwrap())?)) Ok(create_apub_response(&post.to_apub(&db.get().unwrap())?))
} }
impl ToApub<Page> 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. // Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
fn to_apub(&self, conn: &PgConnection) -> Result<Page, Error> { fn to_apub(&self, conn: &PgConnection) -> Result<Page, Error> {
let mut page = Page::default(); let mut page = Page::default();
@ -53,7 +55,9 @@ impl ToApub<Page> for Post {
} }
} }
impl FromApub<Page> for PostForm { impl FromApub for PostForm {
type ApubType = Page;
/// Parse an ActivityPub page received from another instance into a Lemmy post. /// Parse an ActivityPub page received from another instance into a Lemmy post.
fn from_apub(page: &Page, conn: &PgConnection) -> Result<PostForm, Error> { fn from_apub(page: &Page, conn: &PgConnection) -> Result<PostForm, Error> {
let oprops = &page.object_props; let oprops = &page.object_props;

View file

@ -5,7 +5,9 @@ pub struct UserQuery {
user_name: String, user_name: String,
} }
impl ToApub<PersonExt> 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. // Turn a Lemmy Community into an ActivityPub group that can be sent out over the network.
fn to_apub(&self, _conn: &PgConnection) -> Result<PersonExt, Error> { fn to_apub(&self, _conn: &PgConnection) -> Result<PersonExt, Error> {
// TODO go through all these to_string and to_owned() // TODO go through all these to_string and to_owned()
@ -50,7 +52,8 @@ impl ActorType for User_ {
} }
} }
impl FromApub<PersonExt> for UserForm { impl FromApub for UserForm {
type ApubType = PersonExt;
/// Parse an ActivityPub person received from another instance into a Lemmy user. /// Parse an ActivityPub person received from another instance into a Lemmy user.
fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> { fn from_apub(person: &PersonExt, _conn: &PgConnection) -> Result<Self, Error> {
let oprops = &person.base.base.object_props; let oprops = &person.base.base.object_props;