perseus/template/states.rs
1use crate::errors::*;
2use crate::state::TemplateState;
3
4/// Represents all the different states that can be generated for a single
5/// template, allowing amalgamation logic to be run with the knowledge
6/// of what did what (rather than blindly working on a vector).
7#[derive(Debug)]
8pub(crate) struct States {
9 /// Any state generated by the *build state* strategy.
10 pub build_state: TemplateState,
11 /// Any state generated by the *request state* strategy.
12 pub request_state: TemplateState,
13}
14impl States {
15 /// Checks if both request state and build state are defined.
16 pub fn both_defined(&self) -> bool {
17 !self.build_state.is_empty() && !self.request_state.is_empty()
18 }
19 /// Gets the only defined state if only one is defined. If no states are
20 /// defined, this will just return `None`. If both are defined,
21 /// this will return an error. (Under no other conditions may this
22 /// ever return an error.)
23 pub fn get_defined(&self) -> Result<TemplateState, ServeError> {
24 if self.both_defined() {
25 return Err(ServeError::BothStatesDefined);
26 }
27
28 if !self.build_state.is_empty() {
29 Ok(self.build_state.clone())
30 } else if !self.request_state.is_empty() {
31 Ok(self.request_state.clone())
32 } else {
33 Ok(TemplateState::empty())
34 }
35 }
36}