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
use crate::{ident::Ident, Docs};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Flags {
    pub(crate) flags: Vec<Flag>,
}

impl Flags {
    pub fn new(flags: impl IntoIterator<Item = impl Into<Flag>>) -> Self {
        Self {
            flags: flags.into_iter().map(|f| f.into()).collect(),
        }
    }

    pub fn flags(&self) -> &[Flag] {
        &self.flags
    }

    pub fn flags_mut(&mut self) -> &mut Vec<Flag> {
        &mut self.flags
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Flag {
    pub(crate) name: Ident,
    pub(crate) docs: Option<Docs>,
}

impl Flag {
    pub fn new(name: impl Into<Ident>) -> Self {
        Flag {
            name: name.into(),
            docs: None,
        }
    }

    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
        self.docs = docs.map(|d| d.into());
    }
}

impl<T> Into<Flag> for (T,)
where
    T: Into<Ident>,
{
    fn into(self) -> Flag {
        Flag::new(self.0)
    }
}

impl<T, D> Into<Flag> for (T, D)
where
    T: Into<Ident>,
    D: Into<Docs>,
{
    fn into(self) -> Flag {
        let mut flag = Flag::new(self.0);
        flag.docs(Some(self.1));
        flag
    }
}