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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use clap;
use cursive::view::View;
use cursive::views;
use serde_json::value::Value;
use std::rc::Rc;
use validators::{Required, Validator};
pub(crate) mod autocomplete;
mod checkbox;
pub(crate) mod multiselect;
mod text;
pub use self::autocomplete::Autocomplete;
pub use self::checkbox::Checkbox;
pub use self::multiselect::Multiselect;
pub use self::text::Text;
pub trait WidgetManager {
fn build_value_view(&self, value: &str) -> views::ViewBox;
fn get_value(&self, view: &views::ViewBox) -> String;
#[deprecated(
since = "1.0.0",
note = "Errors should be transferred to `Field`. Use `Field.set_error`"
)]
fn set_error(&self, viewbox: &mut views::ViewBox, error: &str) {
let layout: &mut views::LinearLayout = (**viewbox).as_any_mut().downcast_mut().unwrap();
let child: &mut View = (*layout).get_child_mut(2).unwrap();
let text: &mut views::TextView = (*child).as_any_mut().downcast_mut().unwrap();
text.set_content(error);
}
#[deprecated(
since = "1.0.0",
note = "Values like `help` or `label` should be stored on `FormField`. Details in documentation of `WidgetManager.build_widget`"
)]
fn build_widget(&self, label: &str, help: &str, initial: &str) -> views::ViewBox;
}
#[derive(Clone)]
pub struct Field<W: WidgetManager, T> {
label: String,
help: String,
initial: T,
validators: Vec<Rc<Validator>>,
widget_manager: W,
}
impl<W: WidgetManager, T> Field<W, T> {
pub fn new<IS: Into<String>>(label: IS, widget_manager: W, initial: T) -> Self {
Field {
label: label.into(),
help: "".into(),
initial: initial,
validators: vec![],
widget_manager: widget_manager,
}
}
pub fn help<IS: Into<String>>(mut self, msg: IS) -> Self {
self.help = msg.into();
self
}
pub fn validator<V: Validator + 'static>(mut self, validator: V) -> Self {
self.validators.push(Rc::new(validator));
self
}
pub fn is_required(&self) -> bool {
self.validators
.iter()
.any(|&ref x| (**x).as_any().downcast_ref::<Required>().is_some())
}
}
pub type FieldErrors = Vec<String>;
pub trait FormField {
fn build_widget(&self) -> views::ViewBox {
let view = self
.get_widget_manager()
.build_value_view(&self.get_initial());
label_with_help_layout(view, self.get_label(), &self.get_help())
}
fn validate(&self, data: &str) -> Result<Value, FieldErrors>;
fn get_label(&self) -> &str;
fn get_help(&self) -> &str;
fn get_initial(&self) -> String;
fn get_widget_manager(&self) -> &WidgetManager;
fn clap_arg(&self) -> clap::Arg;
fn clap_args2str(&self, args: &clap::ArgMatches) -> String;
fn is_required(&self) -> bool;
fn set_error(&self, viewbox: &mut views::ViewBox, error: &str) {
let layout: &mut views::LinearLayout = (**viewbox).as_any_mut().downcast_mut().unwrap();
let child: &mut View = (*layout).get_child_mut(2).unwrap();
let text: &mut views::TextView = (*child).as_any_mut().downcast_mut().unwrap();
text.set_content(error);
}
}
fn format_annotation(label: &str, help: &str) -> String {
if help.len() > 0 {
format!("{:20}: {}", label, help)
} else {
format!("{:20}", label)
}
}
pub fn label_with_help_layout(view_box: views::ViewBox, label: &str, help: &str) -> views::ViewBox {
let text = format_annotation(label, help);
let widget = views::LinearLayout::vertical()
.child(views::TextView::new(text))
.child(view_box)
.child(views::TextView::new(""))
.child(views::DummyView);
views::ViewBox::new(Box::new(widget))
}
pub fn value_view_from_layout(layout: &views::ViewBox) -> &views::ViewBox {
let layout: &views::LinearLayout = (**layout).as_any().downcast_ref().unwrap();
let value_view: &View = layout.get_child(1).unwrap();
(*value_view).as_any().downcast_ref().unwrap()
}