| 1 |
use 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"))] |
| 6 |
pub struct Flags { |
| 7 |
pub(crate) flags: Vec<Flag>, |
| 8 |
} |
| 9 |
|
| 10 |
impl 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"))] |
| 33 |
pub struct Flag { |
| 34 |
pub(crate) name: Ident, |
| 35 |
pub(crate) docs: Option<Docs>, |
| 36 |
} |
| 37 |
|
| 38 |
impl 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 |
|
| 63 |
impl<T> Into<Flag> for (T,) |
| 64 |
where |
| 65 |
T: Into<Ident>, |
| 66 |
{ |
| 67 |
fn into(self) -> Flag { |
| 68 |
Flag::new(self.0) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl<T, D> Into<Flag> for (T, D) |
| 73 |
where |
| 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 |
} |