polysite/compiler/
metadata.rs

1use crate::{builder::metadata::Value, *};
2use serde::Serialize;
3use std::collections::HashMap;
4
5/// [`Metadata`] can used for setting metadata
6#[derive(Clone)]
7pub struct SetMetadata {
8    compiling: HashMap<String, Value>,
9    global: HashMap<String, Value>,
10}
11impl SetMetadata {
12    pub fn new() -> Self {
13        Self {
14            compiling: HashMap::new(),
15            global: HashMap::new(),
16        }
17    }
18    pub fn global(mut self, k: impl AsRef<str>, v: impl Serialize) -> Result<Self, Error> {
19        self.global
20            .insert(k.as_ref().to_owned(), Metadata::to_value(v)?);
21        Ok(self)
22    }
23    pub fn local(mut self, k: impl AsRef<str>, v: impl Serialize) -> Result<Self, Error> {
24        self.compiling
25            .insert(k.as_ref().to_owned(), Metadata::to_value(v)?);
26        Ok(self)
27    }
28}
29impl Compiler for SetMetadata {
30    #[tracing::instrument(skip(self, ctx))]
31    fn next_step(&mut self, mut ctx: Context) -> CompilerReturn {
32        let s = self.clone();
33        compile!({
34            for (k, v) in s.global.into_iter() {
35                ctx.metadata().insert_global(k, v).await;
36            }
37            for (k, v) in s.compiling.into_iter() {
38                ctx.metadata_mut().insert_local(k, v);
39            }
40            Ok(CompileStep::Completed(ctx))
41        })
42    }
43}