hyper_static_server/
runtime_askama.rs

1
2
3use crate::hss;
4
5
6use crate::{
7		
8		hss::ServerResult,
9		hss::Extensions,
10		
11		hss::ResultExtWrap,
12		hss::fail_with_code,
13		hss::fail_with_message,
14		hss::fail_with_format,
15		
16	};
17
18
19
20
21pub trait AskamaContext
22		where Self : Sized
23{
24	fn new_with_defaults () -> ServerResult<Self> {
25		fail_with_message! (0xe41380ce, "context can't be created with defaults!");
26	}
27	
28	fn new_with_extensions (_extensions : &hss::Extensions) -> ServerResult<Self> {
29		Self::new_with_defaults ()
30	}
31	
32	fn new_with_deserialization (_encoding : &str, _data : &[u8], _extensions : &hss::Extensions) -> ServerResult<Self> {
33		fail_with_message! (0x97024f74, "context can't be deserialized!");
34	}
35}
36
37
38impl AskamaContext for () {
39	
40	fn new_with_defaults () -> ServerResult<()> {
41		Ok (())
42	}
43	
44	fn new_with_deserialization (_encoding : &str, _data : &[u8], _extensions : &hss::Extensions) -> ServerResult<Self> {
45		Ok (())
46	}
47}
48
49
50
51
52#[ cfg (feature = "runtime-askama-serde") ]
53pub trait AskamaContextSerde
54		where
55				Self : Sized,
56				Self : ::serde::de::DeserializeOwned,
57{
58	fn new_with_serde <'a> (_encoding : &str, _data : &[u8]) -> ServerResult<Self> {
59		match _encoding {
60			
61			#[ cfg (feature = "toml") ]
62			"toml" =>
63				::toml::from_slice (_data) .or_wrap (0xd02e891d),
64			
65			#[ cfg (feature = "serde_yaml") ]
66			"yaml" =>
67				::serde_yaml::from_slice (_data) .or_wrap (0xf6e7147f),
68			
69			#[ cfg (feature = "serde_json") ]
70			"json" =>
71				::serde_json::from_slice (_data) .or_wrap (0xa8d9dccf),
72			
73			"auto" => {
74				let _encoding = if _data.starts_with (b"## toml\n") {
75					"toml"
76				} else if _data.starts_with (b"## yaml\n") {
77					"yaml"
78				} else if _data.starts_with (b"{") || _data.starts_with (b"[") {
79					"json"
80				} else {
81					fail_with_format! (0x164f2b63, "encoding `{}` failed to match", _encoding);
82				};
83				Self::new_with_serde (_encoding, _data)
84			}
85			
86			_ =>
87				fail_with_format! (0x95017b94, "encoding `{}` not supported", _encoding),
88		}
89	}
90}
91
92
93#[ cfg (feature = "runtime-askama-serde") ]
94impl <S : AskamaContextSerde> AskamaContext for S {
95	
96	fn new_with_deserialization (_encoding : &str, _data : &[u8], _extensions : &hss::Extensions) -> ServerResult<Self> {
97		Self::new_with_serde (_encoding, _data)
98	}
99}
100
101
102
103
104#[ cfg (feature = "runtime-askama-serde") ]
105#[ derive (Debug, Clone) ]
106#[ derive (serde::Serialize, serde::Deserialize) ]
107pub struct AskamaDocumentMetadata {
108	pub title : Option<String>,
109	pub headings : Option<Vec<AskamaDocumentHeading>>,
110}
111
112
113#[ cfg (feature = "runtime-askama-serde") ]
114#[ derive (serde::Serialize, serde::Deserialize) ]
115#[ derive (Debug, Clone) ]
116pub struct AskamaDocumentHeading {
117	pub level : u8,
118	pub text : Option<String>,
119	pub anchor : Option<String>,
120}
121
122
123#[ cfg (feature = "runtime-askama-serde") ]
124impl AskamaDocumentMetadata {
125	
126	pub fn load_from_json (_json : &str) -> ServerResult<Self> {
127		::serde_json::from_str (_json) .or_wrap (0x6410e85f)
128	}
129}
130
131
132
133
134#[ cfg (not (feature = "runtime-askama-serde")) ]
135#[ derive (Debug, Clone) ]
136pub struct AskamaDocumentMetadata ();
137
138
139#[ cfg (not (feature = "runtime-askama-serde")) ]
140impl AskamaDocumentMetadata {
141	
142	pub fn load_from_json (_json : &str) -> ServerResult<Self> {
143		Ok (Self ())
144	}
145}
146