Skip to main content

lafere_api/
request.rs

1use crate::error::ApiError;
2use crate::message::Action;
3#[cfg(feature = "connection")]
4use crate::{
5	error::Error,
6	message::Message,
7	requestor::Requestor,
8	server::{Data, Session},
9};
10
11#[cfg(feature = "connection")]
12use lafere::standalone_util::PinnedFuture;
13
14/// Basic request definition.
15pub trait Request {
16	type Action: Action;
17	type Response;
18	type Error: ApiError;
19
20	const ACTION: Self::Action;
21}
22
23#[cfg(feature = "connection")]
24pub trait RequestHandler<B> {
25	type Action: Action;
26
27	fn action() -> Self::Action
28	where
29		Self: Sized;
30
31	/// if the data is not available just panic
32	fn validate_data(&self, data: &Data);
33
34	/// handles a message with Self::ACTION as action.
35	///
36	/// if None is returned the request is abandoned and
37	/// the requestor receives a RequestDropped error
38	fn handle<'a>(
39		&'a self,
40		msg: Message<Self::Action, B>,
41		data: &'a Data,
42		session: &'a Session,
43	) -> PinnedFuture<'a, Result<Message<Self::Action, B>, Error>>;
44}
45
46#[cfg(feature = "connection")]
47pub trait EnableServerRequestsHandler<B> {
48	type Action: Action;
49
50	/// if the data is not available just panic
51	fn validate_data(&self, data: &Data);
52
53	/// handles a message with Self::ACTION as action.
54	///
55	/// if None is returned the request is abandoned and
56	/// the requestor receives a RequestDropped error
57	fn handle<'a>(
58		&'a self,
59		sender: Requestor<Self::Action, B>,
60		data: &'a Data,
61		session: &'a Session,
62	) -> PinnedFuture<'a, Result<(), Error>>;
63}