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
/// Block metadata
pub struct BlockMeta {
    type_name: String,
    instance_name: Option<String>,
    blocking: bool,
}

impl BlockMeta {
    fn new(type_name: String, blocking: bool) -> BlockMeta {
        BlockMeta {
            type_name,
            instance_name: None,
            blocking,
        }
    }
    /// Name of block type
    pub fn type_name(&self) -> &str {
        &self.type_name
    }
    /// Instance name
    pub fn instance_name(&self) -> Option<&str> {
        self.instance_name.as_ref().map(|x| x as _)
    }
    /// Set instance name
    pub fn set_instance_name(&mut self, name: impl Into<String>) {
        self.instance_name = Some(name.into());
    }
    /// Is block blocking
    ///
    /// Blocking blocks will be spawned on a separate thread.
    pub fn is_blocking(&self) -> bool {
        self.blocking
    }
}

/// Block metadata buidler
pub struct BlockMetaBuilder {
    name: String,
    blocking: bool,
}

impl BlockMetaBuilder {
    /// Create builder
    pub fn new(name: impl Into<String>) -> BlockMetaBuilder {
        BlockMetaBuilder {
            name: name.into(),
            blocking: false,
        }
    }
    /// Mark block as blocking
    ///
    /// Blocking blocks will be spawned on a separate thread.
    #[must_use]
    pub fn blocking(mut self) -> Self {
        self.blocking = true;
        self
    }
    /// Build block metadata
    pub fn build(self) -> BlockMeta {
        BlockMeta::new(self.name, self.blocking)
    }
}