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
use super::{TemplateState, TemplateStateWithType};
use serde::{de::DeserializeOwned, Serialize};
/// The output of the build seed system, which should be generated by a user
/// function for each template.
#[derive(Debug)]
pub struct BuildPaths {
/// The paths to render underneath this template, without the template name
/// or leading forward slashes.
pub paths: Vec<String>,
/// Any additional state, of an arbitrary type, to be passed to all future
/// state generation. This can be used to avoid unnecessary duplicate
/// filesystem reads, or the like.
///
/// The exact type information from this is deliberately discarded.
pub extra: TemplateState,
}
/// The information any function that generates state will be provided.
///
/// This must be able to be shared safely between threads.
#[derive(Clone, Debug)]
pub struct StateGeneratorInfo<B: Serialize + DeserializeOwned + Send + Sync> {
/// The path it is generating for, not including the template name or
/// locale.
///
/// **Warning:** previous versions of Perseus used to prefix this with the
/// template name, and this is no longer done, for convenience of
/// handling.
pub path: String,
/// The locale it is generating for.
pub locale: String,
/// Any extra data from the template's build seed.
pub(crate) extra: TemplateStateWithType<B>,
}
impl<B: Serialize + DeserializeOwned + Send + Sync + 'static> StateGeneratorInfo<B> {
/// Transform the underlying [`TemplateStateWithType`] into one with a
/// different type. Once this is done, `.to_concrete()` can be used to
/// get this type out of the container.
#[cfg(engine)] // Just to silence clippy (if you need to remove this, do)
pub(crate) fn change_type<U: Serialize + DeserializeOwned + Send + Sync>(
self,
) -> StateGeneratorInfo<U> {
StateGeneratorInfo {
path: self.path,
locale: self.locale,
extra: self.extra.change_type(),
}
}
/// Get the extra build state as an owned type.
///
/// # Panics
/// Hypothetically, if there were a failure in the Perseus core such that
/// your extra build state ended up being malformed, this would panic.
/// However, this should never happen, as there are multiplr layers of
/// checks before this that should catch such an event. If this panics,
/// and if keeps panicking after `perseus clean`, please report it as a
/// bug (assuming all your types are correct).
pub fn get_extra(&self) -> B {
match B::deserialize(&self.extra.state) {
Ok(extra) => extra,
// This should never happen...
Err(err) => panic!(
"unrecoverable extra build state extraction failure: {:#?}",
err
),
}
}
}