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