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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::borrow::Cow;
use std::ops::Deref;
use std::ops::DerefMut;

use crate::format::HelpDisplay;
use crate::format::HelpPolicy;

#[derive(Debug, Default, Clone)]
pub struct Block<'a, T> {
    name: Cow<'a, str>,

    hint: Cow<'a, str>,

    help: Cow<'a, str>,

    foot: Cow<'a, str>,

    head: Cow<'a, str>,

    stores: Vec<T>,
}

impl<'a, T> Block<'a, T> {
    pub fn new<S: Into<Cow<'a, str>>>(name: S, hint: S, help: S, head: S, foot: S) -> Self {
        Self {
            name: name.into(),
            hint: hint.into(),
            help: help.into(),
            foot: foot.into(),
            head: head.into(),
            stores: vec![],
        }
    }

    pub fn name(&self) -> Cow<'a, str> {
        self.name.clone()
    }

    pub fn hint(&self) -> Cow<'a, str> {
        self.hint.clone()
    }

    pub fn help(&self) -> Cow<'a, str> {
        self.help.clone()
    }

    pub fn foot(&self) -> Cow<'a, str> {
        self.foot.clone()
    }

    pub fn head(&self) -> Cow<'a, str> {
        self.head.clone()
    }

    pub fn attach(&mut self, store: T) -> &mut Self {
        self.stores.push(store);
        self
    }

    pub fn set_name<S: Into<Cow<'a, str>>>(&mut self, name: S) -> &mut Self {
        self.name = name.into();
        self
    }

    pub fn set_hint<S: Into<Cow<'a, str>>>(&mut self, hint: S) -> &mut Self {
        self.hint = hint.into();
        self
    }

    pub fn set_help<S: Into<Cow<'a, str>>>(&mut self, help: S) -> &mut Self {
        self.help = help.into();
        self
    }

    pub fn set_foot<S: Into<Cow<'a, str>>>(&mut self, footer: S) -> &mut Self {
        self.foot = footer.into();
        self
    }

    pub fn set_head<S: Into<Cow<'a, str>>>(&mut self, header: S) -> &mut Self {
        self.head = header.into();
        self
    }
}

impl<'a, T> Deref for Block<'a, T> {
    type Target = Vec<T>;

    fn deref(&self) -> &Self::Target {
        &self.stores
    }
}

impl<'a, T> DerefMut for Block<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.stores
    }
}

impl<'b, T> HelpDisplay for Block<'b, T> {
    fn gen_help<'a, P>(&self, policy: &P) -> Option<Cow<'a, str>>
    where
        Self: 'a,
        P: HelpPolicy<'a, Self>,
    {
        policy.format(self)
    }
}