1use std::fmt::Debug;
2
3pub trait Entity<IdType>: Clone + Debug {
7 fn get_id(&self) -> IdType;
8}
9
10pub trait Create<I, E: Entity<I>> {
13 type Error;
14 fn save(&mut self, entity: &E) -> Result<(), Self::Error>;
15}
16
17pub trait Read<I, E: Entity<I>> {
20 type Error;
21 fn find_by_id(&self, id: &I) -> Result<E, Self::Error>;
22}
23
24pub trait ReadWithPaginationAndSort<I, E: Entity<I>> {
27 type Error;
28 fn find_all_with_page(&self, page: &Page) -> Result<Vec<E>, Self::Error>;
29 fn find_all_with_page_and_sort(&self, page: &Page, sort: &Sort) -> Result<Vec<E>, Self::Error>;
30}
31
32pub trait Update<I, E: Entity<I>> {
35 type Error;
36 fn update(&mut self, entity: &E) -> Result<(), Self::Error>;
37}
38
39pub trait Delete<I, E: Entity<I>> {
42 type Error;
43 fn remove_by_id(&mut self, id: &I) -> Result<(), Self::Error>;
44 fn remove(&mut self, entity: &E) -> Result<(), Self::Error>;
45}
46
47pub trait Crud<I, E: Entity<I>>:
48 Create<I, E> + Read<I, E> + ReadWithPaginationAndSort<I, E> + Update<I, E> + Delete<I, E>
49{
50}
51
52pub struct Page {
54 pub number: u32,
56 pub size: u32,
58}
59
60impl Page {
61 pub fn new(number: u32, size: u32) -> Self {
63 Self { number, size }
64 }
65
66 pub fn offset(&self) -> u32 {
68 self.number * self.size
69 }
70}
71
72#[derive(PartialEq)]
74pub enum Sort {
75 ASCENDING,
76 DESCENDING,
77}