lib_humus/
view.rs

1use axum::response::IntoResponse;
2use axum::response::Json;
3use axum::response::Response;
4use axum::http::status::StatusCode;
5use serde::Serialize;
6
7use crate::HumusQuerySettings;
8use crate::HumusFormat;
9
10/// 👁 Provides data and logic for the [HumusEngine] and
11/// knows how to put information together.
12///
13/// It is recommended to implement this as an enum carrying additional information.
14/// 
15/// 💡Also have a look at the provided methods, while they may be sane defaults
16/// they are probably not always the desired behavior.
17///
18/// [HumusEngine]: ./struct.HumusEngine.html
19pub trait HumusView<S,F>: Serialize + Sized
20where S: HumusQuerySettings<F>, F: HumusFormat {
21
22	/// Returns the template name that will be used to select
23	/// the template file.
24	/// 
25	/// If the name is "404" for an html response the template
26	/// file "404.html" will be used.
27	///
28	/// Also ends up as the `view` variable in the template.
29	/// Example:
30	/// ```rust,ignore
31	/// fn get_template_name(&self) -> String {
32	/// 	match self {
33	/// 		Self::Index{..} => "index",
34	/// 		Self::Results{..} => "results",
35	/// 		Self::NotFound => "404",
36	/// 		Self::InternalError{..} => "500",
37	/// 	}.to_string()
38	/// }
39	/// ```
40	fn get_template_name(&self) -> String;
41
42	/// Returns the reponse code for the view.
43	///
44	/// The numeric value will be useable as `http_status` in the template.
45	///
46	/// Example:
47	/// ```rust,ignore
48	/// use axum::http::StatusCode;
49	///
50	/// fn get_status_code(&self, settings: &SomeSettings) -> StatusCode {
51	/// 	match self {
52	/// 		Self::NotFound => StatusCode::NOT_FOUND,
53	/// 		Self::InternalError{..} => StatusCode::INTERNAL_SERVER_ERROR,
54	/// 		_ => StatusCode::OK,
55	/// 	}
56	/// }
57	/// ```
58	fn get_status_code(&self, settings: &S) -> StatusCode;
59
60	/// If this returns a String it will be used as the cookie header.
61	///
62	/// Only available when the `axum-view+cookie` feature is enabled.
63	/// 
64	/// See: [axum: Constructing a Cookie](https://docs.rs/axum-extra/0.8.0/axum_extra/extract/cookie/struct.Cookie.html#constructing-a-cookie)
65	///
66	/// ⚠️ *Warning:* When setting cookies in the
67	///              `update_response()` or `get_api_response()`
68	///              this should return `None`, otherise you are in
69	///              undefined behavior territory.
70	#[cfg(feature="axum-view+cookie")]
71	fn get_cookie_header(&self, _settings: &S) -> Option<String> { None }
72
73	/// Update non-API responses after they have been built.
74	///
75	/// Useful for setting extra headers. Does noting by default.
76	fn update_response(&self, _response: &mut Response, _settings: &S) {}
77
78	/// Return an API-Response
79	///
80	/// By default causes the view to Serialize itself
81	/// to a json response using serde.
82	///
83	/// Response code and cookie headers are queried in
84	/// advance and set on the reulting response if it has a status code of 200.
85	/// Otherwise it is assumed that the response genrating logic
86	/// alredy took care of that.
87	///
88	/// You'll need the following imports when implementing:
89	/// ```
90	/// use axum::Json;
91	/// use axum::response::IntoResponse;
92	/// ```
93	fn get_api_response(self, _settings: &S) -> Response {
94		Json(self).into_response()
95	}
96}