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
// Copyright (C) 2020 Stephane Raux. Distributed under the MIT license.

use crate::{Block, BlockProducer, Environment, Style};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Styled {
    #[serde(default)]
    style: Style,
    producer: Box<BlockProducer>,
}

impl Styled {
    pub fn new(producer: BlockProducer) -> Self {
        Styled {
            style: Default::default(),
            producer: Box::new(producer),
        }
    }

    pub fn with_style<T>(self, style: T) -> Self
    where
        T: Into<Style>,
    {
        Self {
            style: style.into(),
            ..self
        }
    }

    pub fn produce(&self, environment: &Environment) -> Vec<Block> {
        let mut blocks = self.producer.produce(environment);
        for block in &mut blocks {
            block.style = block.style.or(&self.style);
        }
        blocks
    }
}