vitium_api/net.rs
1use serde::{de::DeserializeOwned, Deserialize, Serialize};
2
3/// Describes a request.
4/// Any type that implements `Req` should be correctly handled
5/// if sent to the server with specified `PATH` and `METHOD`.
6pub trait Req: Serialize + DeserializeOwned {
7 /// The JSON body of response.
8 type Response: Serialize + DeserializeOwned;
9 /// The path this request should be sent to.
10 fn path(&self) -> String;
11 /// The method this request should be sent with.
12 const METHOD: &'static str;
13}
14
15/// Denotes a payload that is accessed with a REST API.
16pub trait REST: Serialize + DeserializeOwned {
17 /// The index type that identifies individual resource from collection, e.g. username for users.
18 type Index;
19 /// The path on the server to send request to.
20 fn path() -> String;
21}
22
23#[derive(Clone, Serialize, Deserialize)]
24#[cfg_attr(target_family = "wasm", derive(tsify_next::Tsify))]
25#[cfg_attr(
26 target_family = "wasm",
27 tsify(into_wasm_abi, from_wasm_abi, large_number_types_as_bigints)
28)]
29pub struct SignUp {
30 pub user: String,
31 pub pass: String,
32}
33
34impl Req for SignUp {
35 type Response = ();
36
37 fn path(&self) -> String {
38 "/api/auth/signup".into()
39 }
40
41 const METHOD: &'static str = "POST";
42}
43
44#[derive(Clone, Serialize, Deserialize)]
45#[cfg_attr(target_family = "wasm", derive(tsify_next::Tsify))]
46#[cfg_attr(
47 target_family = "wasm",
48 tsify(into_wasm_abi, from_wasm_abi, large_number_types_as_bigints)
49)]
50pub struct Enroll(pub String);
51
52/// A global out-game chat message.
53///
54/// # Time Order
55///
56/// The [`Self::sender`] fields indicates the local timestamp on the client when and where this message is sent.
57/// No guarantee is made about the consistency of the time order of message.
58///
59/// # HTML
60///
61/// The message can either be a plain text message (when [`Self::html`] is set to `false`)
62/// or an HTML message (when [`Self::html`] is set to `true`).
63///
64/// ## Caution
65///
66/// Only constrait to [`Self::content`] is the same as that of [`String`], i.e. to be valid UTF-8,
67/// even if [`Self::html`] is on.
68/// Therefore, no guarantee is made about whether it is valid DOM element or HTML.
69/// Appropriate checks shall be done before attempting to render HTML to avoid potential danger.
70#[derive(Clone, Serialize, Deserialize)]
71#[cfg_attr(target_family = "wasm", derive(tsify_next::Tsify))]
72#[cfg_attr(
73 target_family = "wasm",
74 tsify(
75 into_wasm_abi,
76 from_wasm_abi,
77 missing_as_null,
78 large_number_types_as_bigints
79 )
80)]
81pub struct Message {
82 /// Milisecond UNIX timestamp when the message is sent.
83 #[cfg(target_family = "wasm")] // walkaround for json missing bigint support
84 pub time: f64,
85 /// Milisecond UNIX timestamp when the message is sent.
86 #[cfg(not(target_family = "wasm"))]
87 pub time: u64,
88 /// The user who sends the message.
89 ///
90 /// A [`None`] indicates that this message is a broadcast triggered by a server command.
91 pub sender: Option<String>,
92 /// Content of the chat.
93 /// This field shall be interpreted with respect to [`Self::html`] as plain text or string of HTML.
94 pub content: String,
95 /// Whether HTML is enabled in the content of the message.
96 pub html: bool,
97}
98
99#[derive(Clone, Serialize, Deserialize)]
100#[cfg_attr(target_family = "wasm", derive(tsify_next::Tsify))]
101#[cfg_attr(
102 target_family = "wasm",
103 tsify(into_wasm_abi, from_wasm_abi, large_number_types_as_bigints)
104)]
105pub struct EditPass(pub String);
106
107impl Req for EditPass {
108 type Response = ();
109
110 fn path(&self) -> String {
111 "/api/auth/pass".into()
112 }
113
114 const METHOD: &'static str = "POST";
115}