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
use crate::*;
use anyhow::Result;
use serde::Serialize;
use std::collections::HashMap;

/// [`SetMetadata`] sets metadata to context
///
/// # Example
/// ```
/// use polysite::compiler::metadata::SetMetadata;
/// SetMetadata::new()
///     .global("site_url", "https://example.ccom")
///     .unwrap()
///     .compiling("compiling", vec!["value"])
///     .unwrap();
/// ```
pub struct SetMetadata {
    compiling: HashMap<String, Metadata>,
    global: HashMap<String, Metadata>,
}
impl SetMetadata {
    pub fn new() -> Self {
        Self {
            compiling: HashMap::new(),
            global: HashMap::new(),
        }
    }
    pub fn global(mut self, k: impl ToString, v: impl Serialize) -> Result<Self> {
        self.global
            .insert(k.to_string(), Metadata::from_serializable(v)?);
        Ok(self)
    }
    pub fn compiling(mut self, k: impl ToString, v: impl Serialize) -> Result<Self> {
        self.compiling
            .insert(k.to_string(), Metadata::from_serializable(v)?);
        Ok(self)
    }
}
impl Compiler for SetMetadata {
    fn compile(&self, mut ctx: Context) -> CompilerReturn {
        let compiling = self.compiling.clone();
        let global = self.global.clone();
        compile!({
            for (k, v) in global.into_iter() {
                ctx.insert_global_raw_metadata(k, v).await;
            }
            for (k, v) in compiling.into_iter() {
                ctx.insert_compiling_raw_metadata(k, v)?;
            }
            Ok(ctx)
        })
    }
}