forked from
tranquil.farm/tranquil-pds
Our Personal Data Server from scratch!
1use std::fmt;
2use std::str::FromStr;
3
4use async_trait::async_trait;
5use tranquil_types::{AtUri, Nsid};
6use uuid::Uuid;
7
8use crate::DbError;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum BacklinkPath {
12 Subject,
13 SubjectUri,
14}
15
16impl BacklinkPath {
17 pub fn as_str(&self) -> &'static str {
18 match self {
19 Self::Subject => "subject",
20 Self::SubjectUri => "subject.uri",
21 }
22 }
23}
24
25impl fmt::Display for BacklinkPath {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 f.write_str(self.as_str())
28 }
29}
30
31#[derive(Debug, Clone)]
32pub struct BacklinkPathParseError(String);
33
34impl fmt::Display for BacklinkPathParseError {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 write!(f, "unknown backlink path: {}", self.0)
37 }
38}
39
40impl std::error::Error for BacklinkPathParseError {}
41
42impl FromStr for BacklinkPath {
43 type Err = BacklinkPathParseError;
44
45 fn from_str(s: &str) -> Result<Self, Self::Err> {
46 match s {
47 "subject" => Ok(Self::Subject),
48 "subject.uri" => Ok(Self::SubjectUri),
49 _ => Err(BacklinkPathParseError(s.to_owned())),
50 }
51 }
52}
53
54#[derive(Debug, Clone)]
55pub struct Backlink {
56 pub uri: AtUri,
57 pub path: BacklinkPath,
58 pub link_to: String,
59}
60
61#[async_trait]
62pub trait BacklinkRepository: Send + Sync {
63 async fn get_backlink_conflicts(
64 &self,
65 repo_id: Uuid,
66 collection: &Nsid,
67 backlinks: &[Backlink],
68 ) -> Result<Vec<AtUri>, DbError>;
69
70 async fn add_backlinks(&self, repo_id: Uuid, backlinks: &[Backlink]) -> Result<(), DbError>;
71
72 async fn remove_backlinks_by_uri(&self, uri: &AtUri) -> Result<(), DbError>;
73
74 async fn remove_backlinks_by_repo(&self, repo_id: Uuid) -> Result<(), DbError>;
75}