cob: improve Comment generics

`Infallible` is a more appropriate type in comparsion to `()` for
comments that do not require a `CodeLocation`.

That is because if the type for `location` is `Option<()>`, this means
that `Some(())` can be constructed, however, we never intend to
construct that variant. On the other hand, `Infallible` can never be
constructed, and so, only `None` can be used.

This requires updates to some functions to use the generic `T` and
also handle the different kinds of `Comment`s -- `Infallible` and
`CodeLocation`.

X-Co-Authored-By: Fintan Halpenny <fintan.halpenny@gmail.com>
This commit is contained in:
Sebastian Martinez
2023-12-22 14:09:02 +01:00
parent 5bd4e171e4
commit 1a63024a58
2 changed files with 33 additions and 4 deletions
+31 -4
View File
@@ -103,7 +103,7 @@ pub(crate) fn issue(id: IssueId, issue: Issue, aliases: &impl AliasStore) -> Val
"title": issue.title(),
"state": issue.state(),
"assignees": issue.assignees().collect::<Vec<_>>(),
"discussion": issue.comments().map(|(id, c)| comment(id, c, aliases)).collect::<Vec<_>>(),
"discussion": issue.comments().map(|(id, c)| issue_comment(id, c, aliases)).collect::<Vec<_>>(),
"labels": issue.labels().collect::<Vec<_>>(),
})
}
@@ -132,7 +132,7 @@ pub(crate) fn patch(
"base": rev.base(),
"oid": rev.head(),
"refs": get_refs(repo, patch.author().id(), &rev.head()).unwrap_or_default(),
"discussions": rev.discussion().comments().map(|(id, c)| comment(id, c, aliases)).collect::<Vec<_>>(),
"discussions": rev.discussion().comments().map(|(id, c)| patch_comment(id, c, aliases)).collect::<Vec<_>>(),
"timestamp": rev.timestamp().as_secs(),
"reviews": rev.reviews().map(|(nid, r)| review(nid, r, aliases)).collect::<Vec<_>>(),
})
@@ -172,8 +172,8 @@ fn review(nid: &NodeId, review: &Review, aliases: &impl AliasStore) -> Value {
})
}
/// Returns JSON for a `Comment`.
fn comment(id: &CommentId, comment: &Comment, aliases: &impl AliasStore) -> Value {
/// Returns JSON for a Issue `Comment`.
fn issue_comment(id: &CommentId, comment: &Comment, aliases: &impl AliasStore) -> Value {
json!({
"id": *id,
"author": author(&Author::from(comment.author()), aliases.alias(&comment.author())),
@@ -194,6 +194,33 @@ fn comment(id: &CommentId, comment: &Comment, aliases: &impl AliasStore) -> Valu
})
}
/// Returns JSON for a Patch `Comment`.
fn patch_comment(
id: &CommentId,
comment: &Comment<CodeLocation>,
aliases: &impl AliasStore,
) -> Value {
json!({
"id": *id,
"author": author(&Author::from(comment.author()), aliases.alias(&comment.author())),
"body": comment.body(),
"edits": comment.edits().map(|edit| {
json!({
"author": author(&Author::from(edit.author), aliases.alias(&edit.author)),
"body": edit.body,
"timestamp": edit.timestamp.as_secs(),
"embeds": edit.embeds,
})
}).collect::<Vec<_>>(),
"embeds": comment.embeds().to_vec(),
"reactions": comment.reactions().collect::<Vec<_>>(),
"timestamp": comment.timestamp().as_secs(),
"replyTo": comment.reply_to(),
"location": comment.location(),
"resolved": comment.resolved(),
})
}
/// Returns JSON for a `Review`.
fn review_comment(
id: &CommentId,
+2
View File
@@ -3041,6 +3041,7 @@ mod routes {
"reactions": [["z6Mkk7oqY4pPxhMmGEotDYsFo97vhCj85BLY1H256HrJmjN8","🚀"]],
"timestamp": TIMESTAMP,
"replyTo": null,
"location": null,
"resolved": false,
},
{
@@ -3063,6 +3064,7 @@ mod routes {
"reactions": [],
"timestamp": TIMESTAMP,
"replyTo": comment_id,
"location": null,
"resolved": false,
},
],