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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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 Separated {
    #[serde(default)]
    separator_style: Style,
    #[serde(default = "default_separator")]
    separator: String,
    producers: Vec<BlockProducer>,
}

impl Separated {
    pub fn new<I>(producers: I) -> Self
    where
        I: IntoIterator<Item = BlockProducer>,
    {
        Self {
            producers: producers.into_iter().collect(),
            ..Default::default()
        }
    }

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

    pub fn with_separator<T>(self, separator: T) -> Self
    where
        T: Into<String>,
    {
        Self {
            separator: separator.into(),
            ..self
        }
    }

    pub fn produce(&self, environment: &Environment) -> Vec<Block> {
        self.producers
            .iter()
            .fold(Vec::<Block>::new(), |mut acc, producer| {
                let blocks = producer.produce(environment);
                if !acc.is_empty() && !blocks.is_empty() {
                    acc.push(Block::new(&self.separator).with_style(&self.separator_style));
                }
                acc.extend(blocks);
                acc
            })
    }
}

impl Default for Separated {
    fn default() -> Self {
        Self {
            separator_style: Default::default(),
            separator: default_separator(),
            producers: Default::default(),
        }
    }
}

fn default_separator() -> String {
    " | ".into()
}