]> Untitled Git - lemmy.git/blob - server/src/api/mod.rs
afd62aff8011a524cb45e0b748fb96c3af8c5c81
[lemmy.git] / server / src / api / mod.rs
1 use crate::{
2   db::{community::*, community_view::*, moderator::*, site::*, user::*, user_view::*},
3   websocket::WebsocketInfo,
4 };
5 use diesel::{
6   r2d2::{ConnectionManager, Pool},
7   PgConnection,
8 };
9 use failure::Error;
10
11 pub mod comment;
12 pub mod community;
13 pub mod post;
14 pub mod site;
15 pub mod user;
16
17 #[derive(Fail, Debug)]
18 #[fail(display = "{{\"error\":\"{}\"}}", message)]
19 pub struct APIError {
20   pub message: String,
21 }
22
23 impl APIError {
24   pub fn err(msg: &str) -> Self {
25     APIError {
26       message: msg.to_string(),
27     }
28   }
29 }
30
31 pub struct Oper<T> {
32   data: T,
33 }
34
35 impl<Data> Oper<Data> {
36   pub fn new(data: Data) -> Oper<Data> {
37     Oper { data }
38   }
39 }
40
41 pub trait Perform {
42   type Response: serde::ser::Serialize + Send;
43
44   fn perform(
45     &self,
46     pool: Pool<ConnectionManager<PgConnection>>,
47     websocket_info: Option<WebsocketInfo>,
48   ) -> Result<Self::Response, Error>;
49 }