1use crate::{Flow, LinkType, LinksConstants};
2use std::{borrow::Cow, error, io};
3
4#[derive(thiserror::Error, Debug)]
5pub enum Error<'a, T: LinkType> {
6 #[error("link {0} does not exist.")]
7 NotExists(T),
8
9 #[error("link {0:?} has dependencies")]
10 HasUsages(Vec<Cow<'a, [T]>>),
11
12 #[error("link {0} already exists")]
13 AlreadyExists(Cow<'a, T>),
14
15 #[error("limit for the number of links in the storage has been reached: {0}")]
16 LimitReached(T),
17
18 #[error("unable to allocate memory for links storage: `{0}`")]
19 AllocFailed(#[from] io::Error),
20
21 #[error("other internal error: `{0}`")]
22 Other(#[from] Box<dyn error::Error + Sync + Send>),
23}
24
25pub type ReadHandler<'a, T> = &'a mut dyn FnMut(&[T]) -> Flow;
26
27pub type WriteHandler<'a, T> = &'a mut dyn FnMut(&[T], &[T]) -> Flow;
28
29pub trait Links<T: LinkType> {
30 fn constants_links(&self) -> LinksConstants<T>;
31
32 fn count_links(&self, query: &[T]) -> T;
33
34 fn create_links(
35 &mut self,
36 query: &[T],
37 handler: WriteHandler<'_, T>,
38 ) -> Result<Flow, Error<'_, T>>;
39
40 fn each_links(&self, query: &[T], handler: ReadHandler<'_, T>) -> Result<Flow, Error<'_, T>>;
41
42 fn update_links(
43 &mut self,
44 query: &[T],
45 replacement: &[T],
46 handler: WriteHandler<'_, T>,
47 ) -> Result<Flow, Error<'_, T>>;
48
49 fn delete_links(
50 &mut self,
51 query: &[T],
52 handler: WriteHandler<'_, T>,
53 ) -> Result<Flow, Error<'_, T>>;
54}