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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! Contains form related concetps like `FormView`.
use std::collections::HashMap;
use std::rc::Rc;

use clap;
use cursive::event::{Callback, Event, EventResult, Key, MouseButton, MouseEvent};
use cursive::view::{View, ViewWrapper};
use cursive::views::{Dialog, DialogFocus, LinearLayout, ViewBox};
use cursive::Cursive;
use serde_json::map::Map;
use serde_json::value::Value;

use fields::{FieldErrors, FormField};

/// Container for form's errors.
pub type FormErrors = HashMap<String, FieldErrors>;

type OnSubmit = Option<Rc<Fn(&mut Cursive, Value)>>;
type OnCancel = Option<Rc<Fn(&mut Cursive)>>;

/// Aggregates [Fields] and handles process of `submitting` (or `canceling`).
///
/// [Fields]: ../fields/index.html
pub struct FormView {
    view: Dialog,

    fields: Vec<Box<FormField>>,
    on_submit: OnSubmit,
    on_cancel: OnCancel,
}
impl FormView {
    /// Creates a new `FormView` with two buttons `submit` and `cancel`.
    //TODO: take name & desc + name exposed as title
    pub fn new() -> Self {
        let layout = Dialog::new()
            .content(LinearLayout::vertical())
            .button("Cancel", |_| {})
            .button("Submit (Ctrl+f)", |_| {});
        FormView {
            view: layout,
            fields: Vec::new(),
            on_submit: None,
            on_cancel: None,
        }
    }

    /// Appends `field` to field list.
    pub fn field<V: FormField + 'static>(self, field: V) -> Self {
        self.boxed_field(Box::new(field))
    }

    /// Appends boxed `field` to field list.
    pub fn boxed_field(mut self, field: Box<FormField>) -> Self {
        let widget = field.build_widget();
        self.view
            .get_content_mut()
            .as_any_mut()
            .downcast_mut::<LinearLayout>()
            .unwrap()
            .add_child(widget);
        self.fields.push(field);
        self
    }

    /// Sets the function to be called when submit is triggered.
    pub fn set_on_submit<F>(&mut self, callback: F)
    where
        F: Fn(&mut Cursive, Value) + 'static,
    {
        self.on_submit = Some(Rc::new(callback));
    }

    /// Sets the function to be called when submit is triggered.
    ///
    /// Chainable variant.
    pub fn on_submit<F>(mut self, callback: F) -> Self
    where
        F: Fn(&mut Cursive, Value) + 'static,
    {
        self.set_on_submit(callback);
        self
    }

    /// Sets the function to be called when cancel is triggered.
    pub fn set_on_cancel<F>(&mut self, callback: F)
    where
        F: Fn(&mut Cursive) + 'static,
    {
        self.on_cancel = Some(Rc::new(callback));
    }

    /// Sets the function to be called when cancel is triggered.
    ///
    /// Chainable variant.
    pub fn on_cancel<F>(mut self, callback: F) -> Self
    where
        F: Fn(&mut Cursive) + 'static,
    {
        self.set_on_cancel(callback);
        self
    }

    /// Translates form's fields to [clap::Arg].
    ///
    /// [clap::Arg]: ../../clap/struct.Arg.html
    pub fn fields2clap_args(&self) -> Vec<clap::Arg> {
        let mut args = Vec::with_capacity(self.fields.len());
        for field in &self.fields {
            let arg = field.clap_arg();
            args.push(arg);
        }
        return args;
    }

    /// Translates [clap::ArgMatches] to [serde_json::Value] based on fields.
    ///
    /// [clap::ArgMatches]: ../../clap/struct.ArgMatches.html
    /// [serde_json::Value]: ../../serde_json/enum.Value.html
    pub fn clap_arg_matches2value(&self, arg_matches: &clap::ArgMatches) -> Value {
        let mut form_data = Map::with_capacity(self.fields.len());
        for field in self.fields.iter() {
            let data = field.clap_args2str(&arg_matches);
            match field.validate(data.as_ref()) {
                Ok(v) => {
                    form_data.insert(field.get_label().to_string(), v);
                }
                Err(e) => {
                    let msg: Vec<String> = e.iter().map(|s| format!("ERROR: {:?}", s)).collect();
                    eprintln!("{}", msg.join("\n"));
                }
            }
        }
        Value::Object(form_data)
    }

    /// Validates form.
    pub fn validate(&mut self) -> Result<Value, FormErrors> {
        let mut data = Map::with_capacity(self.fields.len());
        let mut errors: FormErrors = HashMap::with_capacity(self.fields.len());

        for (idx, field) in self.fields.iter().enumerate() {
            let view = self
                .view
                .get_content()
                .as_any()
                .downcast_ref::<LinearLayout>()
                .unwrap()
                .get_child(idx)
                .unwrap();
            let view_box: &ViewBox = (*view).as_any().downcast_ref().unwrap();
            let value = field.get_widget_manager().get_value(view_box);
            let label = field.get_label();
            match field.validate(value.as_ref()) {
                Ok(v) => {
                    data.insert(label.to_owned(), v);
                }
                Err(e) => {
                    errors.insert(label.to_owned(), e);
                }
            }
        }

        if errors.is_empty() {
            Ok(Value::Object(data))
        } else {
            self.show_errors(&errors);
            Err(errors)
        }
    }

    fn show_errors(&mut self, form_errors: &FormErrors) {
        for (idx, field) in self.fields.iter().enumerate() {
            let label = field.get_label();
            let error = form_errors
                .get(label)
                .and_then(|field_errors| field_errors.first());
            // can't call method which returns suitable view because of ownership
            //  * such method would get &mut self
            //  * self.field gets &self
            //  so this clash of &mut and &, illegal
            //  possible solution is to use clone on WidgetManager (needs implementation)
            //  or
            //  form should only call field.validate and rest would be handled by field
            //  which should solve this issue?
            let mut view = self
                .view
                .get_content_mut()
                .as_any_mut()
                .downcast_mut::<LinearLayout>()
                .unwrap()
                .get_child_mut(idx)
                .unwrap();
            let viewbox: &mut ViewBox = view.as_any_mut().downcast_mut().unwrap();
            field.set_error(viewbox, error.unwrap_or(&"".to_string()));
        }
    }

    fn event_submit(&mut self) -> EventResult {
        match self.validate() {
            Ok(data_map) => {
                let opt_cb = self
                    .on_submit
                    .clone()
                    .map(|cb| Callback::from_fn(move |c| cb(c, data_map.clone())));
                EventResult::Consumed(opt_cb)
            }
            Err(_) => {
                // TODO: the event focus next required/invalid field?
                EventResult::Consumed(None)
            }
        }
    }

    fn event_cancel(&mut self) -> EventResult {
        let cb = self
            .on_cancel
            .clone()
            .map(|cb| Callback::from_fn(move |c| cb(c)));
        EventResult::Consumed(cb)
    }

    /// Sets `title` of the form on the top of it.
    pub fn title(mut self, title: &str) -> Self {
        self.view.set_title(title);
        self
    }

    /// Gets fields of `FormView`
    pub fn get_fields(&self) -> &[Box<FormField>] {
        &self.fields
    }

    /// Gets value of a field with label equal to `field_label`
    ///
    /// Returns first occurence when a label occures more than once.
    pub fn get_field_value(&self, field_label: &str) -> Option<String> {
        let mut value = None;
        for (idx, form_field) in self.fields.iter().enumerate() {
            if form_field.get_label() == field_label {
                let view = self
                    .view
                    .get_content()
                    .as_any()
                    .downcast_ref::<LinearLayout>()
                    .unwrap()
                    .get_child(idx)
                    .unwrap();
                let view_box: &ViewBox = (*view).as_any().downcast_ref().unwrap();
                value = Some(form_field.get_widget_manager().get_value(view_box));
                break;
            }
        }
        value
    }
}

impl ViewWrapper for FormView {
    wrap_impl!(self.view: Dialog);

    fn wrap_on_event(&mut self, event: Event) -> EventResult {
        match event {
            Event::Mouse {
                offset: _,
                position: _,
                event: MouseEvent::Press(btn),
            } => {
                if btn == MouseButton::Left {
                    self.with_view_mut(|v| v.on_event(event))
                        .unwrap_or(EventResult::Ignored);
                    match self.view.focus() {
                        DialogFocus::Button(0) => self.event_cancel(),
                        DialogFocus::Button(1) => self.event_submit(),
                        _ => EventResult::Ignored,
                    }
                } else {
                    EventResult::Ignored
                }
            }
            Event::Key(Key::Enter) => match self.view.focus() {
                DialogFocus::Button(0) => self.event_cancel(),
                DialogFocus::Button(1) => self.event_submit(),
                _ => self
                    .with_view_mut(|v| v.on_event(event))
                    .unwrap_or(EventResult::Ignored),
            },
            // TODO: ctlr+enter binding?
            Event::CtrlChar('f') => self.event_submit(),
            _ => {
                // default behaviour from ViewWrapper
                self.with_view_mut(|v| v.on_event(event))
                    .unwrap_or(EventResult::Ignored)
            }
        }
    }
}