aopt_help/
block.rs

1use std::borrow::Cow;
2use std::ops::Deref;
3use std::ops::DerefMut;
4
5use crate::format::HelpDisplay;
6use crate::format::HelpPolicy;
7
8#[derive(Debug, Default, Clone)]
9pub struct Block<'a, T> {
10    name: Cow<'a, str>,
11
12    hint: Cow<'a, str>,
13
14    help: Cow<'a, str>,
15
16    foot: Cow<'a, str>,
17
18    head: Cow<'a, str>,
19
20    stores: Vec<T>,
21}
22
23impl<'a, T> Block<'a, T> {
24    pub fn new<S: Into<Cow<'a, str>>>(name: S, hint: S, help: S, head: S, foot: S) -> Self {
25        Self {
26            name: name.into(),
27            hint: hint.into(),
28            help: help.into(),
29            foot: foot.into(),
30            head: head.into(),
31            stores: vec![],
32        }
33    }
34
35    pub fn name(&self) -> Cow<'a, str> {
36        self.name.clone()
37    }
38
39    pub fn hint(&self) -> Cow<'a, str> {
40        self.hint.clone()
41    }
42
43    pub fn help(&self) -> Cow<'a, str> {
44        self.help.clone()
45    }
46
47    pub fn foot(&self) -> Cow<'a, str> {
48        self.foot.clone()
49    }
50
51    pub fn head(&self) -> Cow<'a, str> {
52        self.head.clone()
53    }
54
55    pub fn attach(&mut self, store: T) -> &mut Self {
56        self.stores.push(store);
57        self
58    }
59
60    pub fn set_name<S: Into<Cow<'a, str>>>(&mut self, name: S) -> &mut Self {
61        self.name = name.into();
62        self
63    }
64
65    pub fn set_hint<S: Into<Cow<'a, str>>>(&mut self, hint: S) -> &mut Self {
66        self.hint = hint.into();
67        self
68    }
69
70    pub fn set_help<S: Into<Cow<'a, str>>>(&mut self, help: S) -> &mut Self {
71        self.help = help.into();
72        self
73    }
74
75    pub fn set_foot<S: Into<Cow<'a, str>>>(&mut self, footer: S) -> &mut Self {
76        self.foot = footer.into();
77        self
78    }
79
80    pub fn set_head<S: Into<Cow<'a, str>>>(&mut self, header: S) -> &mut Self {
81        self.head = header.into();
82        self
83    }
84}
85
86impl<T> Deref for Block<'_, T> {
87    type Target = Vec<T>;
88
89    fn deref(&self) -> &Self::Target {
90        &self.stores
91    }
92}
93
94impl<T> DerefMut for Block<'_, T> {
95    fn deref_mut(&mut self) -> &mut Self::Target {
96        &mut self.stores
97    }
98}
99
100impl<T> HelpDisplay for Block<'_, T> {
101    fn gen_help<'a, P>(&self, policy: &P) -> Option<Cow<'a, str>>
102    where
103        Self: 'a,
104        P: HelpPolicy<'a, Self>,
105    {
106        policy.format(self)
107    }
108}