1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! The crate defines main Application components for uniui_* crates family.
//!
//! * [Service](service::Service) - server handler
//! * [UiPage](ui_page::UiPage) - main UI component

/// server related stuff
pub mod service;

/// UI related stuff
pub mod ui_page;

#[doc(hidden)]
pub mod net {
	pub use uni_net::*;
}

#[doc(hidden)]
pub mod log {
	pub use log::*;
}

#[doc(hidden)]
pub mod serde_json {
	pub use serde_json::*;
}

#[doc(hidden)]
pub mod serde {
	pub use serde::*;
}

#[doc(hidden)]
pub mod uniui_core {
	pub use uniui_core::*;
}

pub use uni_components_macro::*;

/// Simplified representation of HTTP response
///
/// The struct is still in progress. Cookies and Headers are coming soon
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)]
pub struct Response {
	pub code: u16,
	pub body: Option<Vec<u8>>,
	/* 	cookies: ???,
	 * 	headers: ???, */
}

/// The trait to convert to [Response]
pub trait AsResponse:
	'static + serde::de::DeserializeOwned + serde::Serialize + Send + Sync
{
	fn to_response(self) -> Response;
}

/// Supported kinds of HTTP requests
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)]
pub enum Kind {
	/// HTTP-POST request
	Post,

	/// HTTP-GET request
	Get,
}

/// Generic error enum. Will be redesigned soon.
#[derive(thiserror::Error, Debug)]
pub enum CallError {
	#[error("Couldn't convert parameter to string")]
	SerdeJsonError(#[from] serde_json::Error),

	#[error("Couldn't convert parameter to string")]
	SerdeQsSerError(#[from] serde_urlencoded::ser::Error),

	#[error("Couldn't convert string to parameter")]
	SerdeQsDeError(#[from] serde_urlencoded::de::Error),

	#[error("Complicated net error")]
	UniNetError(#[from] uni_net::UniNetError),

	#[error("Not available for current platform. Yet?")]
	NotAvailableForPlatform,

	#[error("Incorrect input")]
	IncorrectInput,
}

fn to_path<T>(
	path: &str,
	t: &T,
) -> Result<String, CallError>
where
	T: 'static + serde::de::DeserializeOwned + serde::Serialize,
{
	let query_params = serde_urlencoded::to_string(t)?;
	let result = format!("{}?{}", path, query_params);
	return Ok(result);
}