Skip to main content

float_pigment_mlp/
context.rs

1use crate::tree::Tree;
2
3#[derive(Default, Debug)]
4pub struct Config;
5#[derive(Debug)]
6pub struct Context {
7    config: Config,
8    tree: Option<Tree>,
9}
10
11impl Context {
12    pub fn create(cfg: Option<Config>) -> Self {
13        Self {
14            config: cfg.unwrap_or_default(),
15            tree: None,
16        }
17    }
18    pub fn tree(&self) -> Option<&Tree> {
19        self.tree.as_ref()
20    }
21    pub fn config(&self) -> &Config {
22        &self.config
23    }
24}
25pub trait Parse<T> {
26    fn parse(&mut self, input: T);
27}
28
29impl Parse<&str> for Context {
30    fn parse(&mut self, input: &str) {
31        self.tree = Some(Tree::from(input));
32    }
33}
34
35impl Parse<String> for Context {
36    fn parse(&mut self, input: String) {
37        self.tree = Some(Tree::from(&input));
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    #[test]
45    fn parse() {
46        let raw = r#"
47          <div id="123">hello
48          world
49          </div>
50        "#;
51        let mut ctx = Context::create(None);
52        ctx.parse(raw);
53        if let Some(t) = ctx.tree() {
54            println!("{t:?}");
55        }
56    }
57}