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
use std::ops::Deref;
use std::rc::Rc;

use clap;
use cursive::views::ViewBox;
use serde_json::value::Value;

use feeders::{DummyFeeder, Feeder};
use fields;
use fields::{label_with_help_layout, Field, FieldErrors, FormField, WidgetManager};
use views;

const VALUE_SEP: &'static str = ",";

/// Convienient wrapper around `Field<MultiselectManager, Vec<String>>`.
pub struct Multiselect;

impl Multiselect {
    /// Creates a new `Field<MultiselectManager, Vec<String>>`.
    pub fn new<IS: Into<String>, F: Feeder>(
        label: IS,
        feeder: F,
    ) -> Field<MultiselectManager, Vec<String>> {
        let mngr = MultiselectManager::with_feeder(feeder);
        Field::new(label, mngr, Vec::new())
    }
}

#[derive(Clone)]
pub struct MultiselectManager {
    feeder: Rc<Feeder>,
    view_factory: Option<Rc<Fn() -> views::Multiselect>>,
}

impl MultiselectManager {
    /// Creates an instance with a customized [Feeder].
    ///
    /// If you want to control creation of [views::Multiselect]
    /// use [with_factory_view].
    ///
    /// [Feeder]: ../../feeders/index.html
    /// [views::Multiselect]: ../../views/struct.Multiselect.html
    /// [with_factory_view]: struct.MultiselectManager.html#method.with_factory_view
    pub fn with_feeder<T: Feeder>(feeder: T) -> Self {
        MultiselectManager {
            feeder: Rc::new(feeder),
            view_factory: None,
        }
    }
    /// Creates an instance with customized [views::Multiselect].
    ///
    /// If you want to specify only a [Feeder] (and use a default [views::Multiselect])
    /// use [with_feeder].
    ///
    /// [Feeder]: ../../feeders/index.html
    /// [views::Multiselect]: ../../views/struct.Multiselect.html
    /// [with_feeder]: struct.MultiselectManager.html#method.with_feeder
    pub fn with_factory_view(factory: Rc<Fn() -> views::Multiselect>) -> Self {
        MultiselectManager {
            // it should be an option of Rc :)
            feeder: Rc::new(DummyFeeder),
            view_factory: Some(factory),
        }
    }

    fn get_view(&self) -> views::Multiselect {
        let view = if let Some(ref fun) = self.view_factory {
            fun()
        } else {
            views::Multiselect::new(Rc::clone(&self.feeder))
        };
        view
    }
}

impl WidgetManager for MultiselectManager {
    fn build_value_view(&self, initial: &str) -> ViewBox {
        let mut widget = self.get_view();
        if initial.trim() != "" {
            let items = initial
                .split(VALUE_SEP)
                .map(|x| x.to_owned())
                .collect::<Vec<String>>();
            widget.select_items(items);
        }
        ViewBox::new(Box::new(widget))
    }
    fn build_widget(&self, label: &str, help: &str, initial: &str) -> ViewBox {
        let view = self.build_value_view(initial);
        label_with_help_layout(view, label, help)
    }
    fn get_value(&self, view_box: &ViewBox) -> String {
        let view_box = fields::value_view_from_layout(view_box);
        let ms: &views::Multiselect = (**view_box).as_any().downcast_ref().unwrap();

        let result: Vec<String> = ms
            .get_selected_items()
            .iter()
            .map(|x| (*x).to_owned())
            .collect();
        result.join(VALUE_SEP)
    }
}

impl FormField for Field<MultiselectManager, Vec<String>> {
    fn get_widget_manager(&self) -> &WidgetManager {
        &self.widget_manager
    }
    fn validate(&self, data: &str) -> Result<Value, FieldErrors> {
        let mut errors = FieldErrors::new();
        let items = data.split(VALUE_SEP).collect::<Vec<&str>>();
        for item in items.iter() {
            for v in &self.validators {
                if let Some(e) = v.validate(item) {
                    errors.push(e);
                }
            }
        }
        if errors.len() > 0 {
            Err(errors)
        } else {
            let vec_str = items
                .iter()
                .map(|x| Value::String(x.to_string()))
                .collect::<Vec<Value>>();
            let val_of_vec = Value::Array(vec_str);
            Ok(val_of_vec)
        }
    }
    fn get_label(&self) -> &str {
        &self.label
    }

    /// Gets help of the field
    fn get_help(&self) -> &str {
        self.help.as_ref()
    }

    fn get_initial(&self) -> String {
        self.initial.join(VALUE_SEP)
    }

    fn clap_arg(&self) -> clap::Arg {
        clap::Arg::with_name(&self.label)
            .long(&self.label)
            .help(&self.help)
            .required(self.is_required())
            .multiple(true)
            .takes_value(true)
    }

    fn clap_args2str(&self, args: &clap::ArgMatches) -> String {
        let values = args
            .values_of(&self.label)
            .unwrap_or(clap::Values::default());
        values.collect::<Vec<&str>>().join(VALUE_SEP)
    }
    fn is_required(&self) -> bool {
        self.is_required()
    }
}

impl<W: WidgetManager> Field<W, Vec<String>> {
    /// Sets initial `value` of `field`.
    pub fn initial<U: Deref<Target = str>>(mut self, initial: Vec<U>) -> Self {
        self.initial = initial
            .iter()
            .map(|x| (*x).to_string())
            .collect::<Vec<String>>();
        self
    }
}