chord_flow/model/
app.rs

1use std::borrow::Borrow;
2use std::collections::HashMap;
3use std::sync::Arc;
4
5use handlebars::Handlebars;
6
7use chord_core::action::Creator;
8
9use crate::model::helper::register;
10
11pub trait App: Sync + Send {
12    fn get_handlebars(&self) -> &Handlebars;
13
14    fn get_creator_map(&self) -> Arc<HashMap<String, Box<dyn Creator>>>;
15}
16
17pub struct AppStruct<'reg> {
18    handlebars: Handlebars<'reg>,
19    creator_map: Arc<HashMap<String, Box<dyn Creator>>>,
20}
21
22impl<'reg> AppStruct<'reg> {
23    pub fn new(creator_map: HashMap<String, Box<dyn Creator>>) -> AppStruct<'reg> {
24        let mut handlebars = Handlebars::new();
25        handlebars.set_strict_mode(true);
26        handlebars.register_escape_fn(handlebars::no_escape);
27        register(&mut handlebars);
28        AppStruct {
29            handlebars,
30            creator_map: Arc::new(creator_map),
31        }
32    }
33}
34
35impl<'reg> App for AppStruct<'reg> {
36    fn get_handlebars(self: &AppStruct<'reg>) -> &Handlebars<'reg> {
37        self.handlebars.borrow()
38    }
39
40    fn get_creator_map(self: &AppStruct<'reg>) -> Arc<HashMap<String, Box<dyn Creator>>> {
41        self.creator_map.clone()
42    }
43}
44
45pub type RenderContext = handlebars::Context;