uni_components/
lib.rs

1//! The crate defines main Application components for uniui_* crates family.
2//!
3//! * [Service](service::Service) - server handler
4//! * [UiPage](ui_page::UiPage) - main UI component
5
6/// server related stuff
7pub mod service;
8
9/// UI related stuff
10pub mod ui_page;
11
12#[doc(hidden)]
13pub const APPLICATION_PACKAGE: &'static str = "com.uniuirust";
14
15#[doc(hidden)]
16pub const ANDROID_INTENT_KEY: &'static str = "com.uniuirust.data";
17
18#[doc(hidden)]
19pub fn android_activity_name_for(path: &str) -> String {
20	return format!(
21		"activity_{}",
22		path.replace("_", "__").replace("/", "_").replace(".", "_")
23	);
24}
25
26#[doc(hidden)]
27pub mod net {
28	pub use uni_net::*;
29}
30
31#[doc(hidden)]
32pub mod log {
33	pub use log::*;
34}
35
36#[doc(hidden)]
37pub mod serde_json {
38	pub use serde_json::*;
39}
40
41#[doc(hidden)]
42pub mod serde {
43	pub use serde::*;
44}
45
46#[doc(hidden)]
47pub mod uniui_core {
48	pub use uniui_core::*;
49}
50
51pub use uni_components_macro::*;
52
53/// Simplified representation of HTTP response
54///
55/// The struct is still in progress. Cookies and Headers are coming soon
56#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)]
57pub struct Response {
58	pub code: u16,
59	pub body: Option<Vec<u8>>,
60	/* 	cookies: ???,
61	 * 	headers: ???, */
62}
63
64/// The trait to convert to [Response]
65pub trait AsResponse:
66	'static + serde::de::DeserializeOwned + serde::Serialize + Send + Sync
67{
68	fn to_response(self) -> Response;
69}
70
71/// Supported kinds of HTTP requests
72#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Deserialize, serde::Serialize)]
73pub enum Kind {
74	/// HTTP-POST request
75	Post,
76
77	/// HTTP-GET request
78	Get,
79}
80
81/// Generic error enum. Will be redesigned soon.
82#[non_exhaustive]
83#[derive(thiserror::Error, Debug)]
84pub enum CallError {
85	#[error("Couldn't convert parameter to string")]
86	SerdeJsonError(#[from] serde_json::Error),
87
88	#[error("Couldn't convert parameter to string")]
89	SerdeQsSerError(#[from] serde_urlencoded::ser::Error),
90
91	#[error("Couldn't convert string to parameter")]
92	SerdeQsDeError(#[from] serde_urlencoded::de::Error),
93
94	#[error("Complicated net error")]
95	UniNetError(#[from] uni_net::UniNetError),
96
97	#[cfg(target_os = "android")]
98	#[error("Complicated jni error")]
99	UniJniError(#[from] uni_jnihelpers::JniError),
100
101	#[error("Not available for current platform. Yet?")]
102	NotAvailableForPlatform,
103
104	#[error("Incorrect input")]
105	IncorrectInput,
106}
107
108fn to_path<T>(
109	path: &str,
110	t: &T,
111) -> Result<String, CallError>
112where
113	T: 'static + serde::de::DeserializeOwned + serde::Serialize,
114{
115	let query_params = serde_urlencoded::to_string(t)?;
116	let result = format!("{}?{}", path, query_params);
117	return Ok(result);
118}