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
// License: see LICENSE file at root directory of `master` branch

//! # Option

use std::{
    borrow::Cow,
    fmt::{self, Display},
};

use super::{Cfg, I18n};

/// # Option
///
/// ## Examples
///
/// ```
/// use std::borrow::Cow;
/// use dia_args::docs::{self, Opt};
///
/// // All these constants are convenient while working with Args.
/// // And you can also use them for Opt.
///
/// const ARG_PORT: &[&str] = &["-p", "--port"];
/// const ARG_PORT_VALUES: &[u16] = &[0, 64009];
/// const ARG_PORT_DEFAULT: u16 = ARG_PORT_VALUES[1];
/// const ARG_PORT_DOCS: Cow<str> = Cow::Borrowed("Port for server.");
///
/// let _opt = Opt::new(
///     ARG_PORT, false,
///     Some(docs::make_cow_strings(ARG_PORT_VALUES)), Some(&ARG_PORT_DEFAULT),
///     ARG_PORT_DOCS,
/// );
/// // Here you can pass this option to Docs::new(...)
/// ```
#[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> {

    /// # Makes new instance
    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,
        }
    }

    /// # Formats self
    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)
    }

}