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
use std::{
borrow::Cow,
fmt::{self, Display},
};
use super::{Cfg, I18n};
#[derive(Clone)]
pub struct Opt<'a> {
names: &'a [&'a str],
required: bool,
values: Option<Vec<Cow<'a, str>>>,
default: Option<&'a Display>,
docs: Cow<'a, str>,
}
impl<'a> Opt<'a> {
pub fn new(names: &'a [&'a str], required: bool, values: Option<Vec<Cow<'a, str>>>, default: Option<&'a Display>, docs: Cow<'a, str>)
-> Self {
Self {
names,
required,
values,
default,
docs,
}
}
pub fn format(&self, cfg: &Cfg, i18n: &I18n, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
let tab = cfg.tab_len().saturating_mul(cfg.tab_level().into());
f.write_str(&super::format(&format!("{:?}", self.names), tab, cfg.columns()))?;
let tab = cfg.tab_len().saturating_mul(cfg.tab_level().saturating_add(1).into());
f.write_str(&super::format(&format!("{}: {}", i18n.required, self.required), tab, cfg.columns()))?;
if let Some(values) = self.values.as_ref() {
let mut tmp = String::new();
for (i, v) in values.iter().enumerate() {
if i > 0 {
tmp.push_str(", ");
}
tmp.push_str(&v);
}
f.write_str(&super::format(&format!("{}: {}", i18n.values, tmp), tab, cfg.columns()))?;
}
if let Some(default) = self.default {
f.write_str(&super::format(&format!("{}: {}", i18n.default, default), tab, cfg.columns()))?;
}
f.write_str(super::LINE_BREAK)?;
f.write_str(&super::format(&self.docs, tab, cfg.columns()))?;
f.write_str(super::LINE_BREAK)?;
Ok(())
}
}
impl<'a> From<Opt<'a>> for Cow<'a, Opt<'a>> {
fn from(opt: Opt<'a>) -> Self {
Cow::Owned(opt)
}
}
impl<'a> From<&'a Opt<'a>> for Cow<'a, Opt<'a>> {
fn from(opt: &'a Opt<'a>) -> Self {
Cow::Borrowed(opt)
}
}