wit_encoder/
flags.rs

1use crate::{Docs, ident::Ident};
2
3#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
6pub struct Flags {
7    pub(crate) flags: Vec<Flag>,
8}
9
10impl Flags {
11    pub fn new(flags: impl IntoIterator<Item = impl Into<Flag>>) -> Self {
12        Self {
13            flags: flags.into_iter().map(|f| f.into()).collect(),
14        }
15    }
16
17    pub fn flag(&mut self, flag: impl Into<Flag>) {
18        self.flags.push(flag.into());
19    }
20
21    pub fn flags(&self) -> &[Flag] {
22        &self.flags
23    }
24
25    pub fn flags_mut(&mut self) -> &mut Vec<Flag> {
26        &mut self.flags
27    }
28}
29
30#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
31#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
32#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
33pub struct Flag {
34    pub(crate) name: Ident,
35    pub(crate) docs: Option<Docs>,
36}
37
38impl Flag {
39    pub fn new(name: impl Into<Ident>) -> Self {
40        Flag {
41            name: name.into(),
42            docs: None,
43        }
44    }
45
46    pub fn name(&self) -> &Ident {
47        &self.name
48    }
49
50    pub fn set_name(&mut self, name: impl Into<Ident>) {
51        self.name = name.into();
52    }
53
54    pub fn docs(&self) -> &Option<Docs> {
55        &self.docs
56    }
57
58    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
59        self.docs = docs.map(|d| d.into());
60    }
61}
62
63impl<T> Into<Flag> for (T,)
64where
65    T: Into<Ident>,
66{
67    fn into(self) -> Flag {
68        Flag::new(self.0)
69    }
70}
71
72impl<T, D> Into<Flag> for (T, D)
73where
74    T: Into<Ident>,
75    D: Into<Docs>,
76{
77    fn into(self) -> Flag {
78        let mut flag = Flag::new(self.0);
79        flag.set_docs(Some(self.1));
80        flag
81    }
82}