-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmod.rs
More file actions
50 lines (45 loc) · 1.5 KB
/
mod.rs
File metadata and controls
50 lines (45 loc) · 1.5 KB
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
use anyhow::{Context, Result};
use futures::{future::try_join_all, TryFuture};
/// Helper trait for `Iterator` to add futures::try_await_all() as chain method.
///
/// The same as wrapping it in `try_join_all(...).await`, but as a chained
/// method instead for cleaner readability.
#[allow(async_fn_in_trait)]
pub trait TryJoinAll: IntoIterator
where
Self::Item: TryFuture,
{
async fn try_join_all(
self,
) -> Result<Vec<<Self::Item as TryFuture>::Ok>, <Self::Item as TryFuture>::Error>;
}
impl<I> TryJoinAll for I
where
I: IntoIterator,
I::Item: TryFuture,
{
/// futures::try_join_all() as a iterator chain method
async fn try_join_all(
self,
) -> Result<Vec<<Self::Item as TryFuture>::Ok>, <Self::Item as TryFuture>::Error> {
try_join_all(self).await
}
}
//
// Minijinja strict rendering with error
//
/// Similar to minijinja.render!(), but strictly disallows undefined
/// template variables.
///
/// # Errors
/// If any template variables are undefined, an error will be returned.
/// See docs for `minijinja.render!` for additional error conditions.
pub fn render_strict(template: &str, context: minijinja::Value) -> Result<String> {
let mut strict_env = minijinja::Environment::new();
// error on any undefined template variables
strict_env.set_undefined_behavior(minijinja::UndefinedBehavior::Strict);
let r = strict_env
.render_str(template, context)
.context(format!("could not render template {:?}", template))?;
Ok(r)
}