macro_tools/attr_prop/
singletone_optional.rs

1//! A generic `Option<  bool  >` attribute property which consists of only keyword.
2//! Defaults to `None`.
3//!
4//! This property can have three states: `None`, `Some( true )`, or `Some( false )`.
5//! It parses `on` and `off` keywords to represent `Some( true )` and `Some( false )` respectively.
6//!
7//! # Example
8//!
9//! ```ignore
10//! #[ attribute( on) ]
11//! #[ attribute( off ) ]
12//! ```
13//!
14//! This is useful for attributes that need to enable or disable features or flags.
15use core ::marker ::PhantomData;
16
17use crate :: *;
18// use component_model_types ::Assign;
19
20/// Default marker for `AttributePropertyOptionalSingletone`.
21/// Used if no marker is defined as parameter.
22#[ derive( Debug, Default, Clone, Copy ) ]
23pub struct AttributePropertyOptionalSingletoneMarker;
24
25/// A generic attribute property for switching on/off.
26/// Has 3 states: `None`, `Some( true )`, `Some( false )`.
27/// Defaults to `None`.
28///
29/// Unlike [`AttributePropertyOptionalBoolean`], it "understands" `on`, `off` keywords during parsing.
30/// For example: `#[ attribute( on ) ]` and `#[ attribute( off ) ]`.
31/// As a consequence, the property has two keywords.
32#[ derive( Debug, Default, Clone, Copy ) ]
33pub struct AttributePropertyOptionalSingletone< Marker = AttributePropertyOptionalSingletoneMarker >(
34  Option< bool >,
35  ::core ::marker ::PhantomData< Marker >,
36);
37
38impl< Marker > AttributePropertyOptionalSingletone< Marker > 
39{
40  /// Return bool value: on/off, use argument as default if it's `None`.
41  /// # Panics
42  /// qqq: doc
43  #[ inline ]
44  #[ must_use ]
45  pub fn value(self, default: bool) -> bool
46  {
47  if self.0.is_none() 
48  {
49   return default;
50 }
51  self.0.unwrap()
52 }
53
54  /// Unwraps and returns the internal optional boolean value.
55  #[ inline( always ) ]
56  #[ must_use ]
57  pub fn internal(self) -> Option< bool >
58  {
59  self.0
60 }
61
62  /// Returns a reference to the internal optional boolean value.
63  #[ must_use ]
64  #[ inline( always ) ]
65  pub fn ref_internal( &self ) -> Option< &bool >
66  {
67  self.0.as_ref()
68 }
69}
70
71impl< Marker, IntoT > Assign< AttributePropertyOptionalSingletone<Marker >, IntoT> for AttributePropertyOptionalSingletone< Marker >
72where
73  IntoT: Into< AttributePropertyOptionalSingletone<Marker >>,
74{
75  /// Inserts value of another instance into the option if it is None, then returns a mutable reference to the contained value.
76  /// If another instance does is None then do nothing.
77  #[ inline( always ) ]
78  #[ allow( clippy ::single_match ) ]
79  fn assign(&mut self, component: IntoT) 
80  {
81  let component = component.into();
82  match component.0 
83  {
84   Some(val) =>
85  {
86  self.0 = Some(val);
87 }
88   None => {}
89 }
90 }
91}
92
93impl< Marker > AttributePropertyComponent for AttributePropertyOptionalSingletone< Marker >
94where
95  Marker: AttributePropertyComponent,
96{
97  const KEYWORD: &'static str = Marker ::KEYWORD;
98}
99
100impl< Marker > From< bool > for AttributePropertyOptionalSingletone< Marker > 
101{
102  #[ inline( always ) ]
103  #[ allow( clippy ::default_constructed_unit_structs ) ]
104  fn from(src: bool) -> Self 
105  {
106  Self(Some(src), PhantomData ::default())
107 }
108}
109
110impl< Marker > From< Option< bool >> for AttributePropertyOptionalSingletone< Marker > 
111{
112  #[ inline( always ) ]
113  #[ allow( clippy ::default_constructed_unit_structs ) ]
114  fn from(src: Option< bool >) -> Self 
115  {
116  Self(src, PhantomData ::default())
117 }
118}
119
120impl< Marker > From< AttributePropertyOptionalSingletone<Marker >> for Option< bool > 
121{
122  #[ inline( always ) ]
123  fn from(src: AttributePropertyOptionalSingletone< Marker >) -> Self 
124  {
125  src.0
126 }
127}
128
129impl< Marker > core ::ops ::Deref for AttributePropertyOptionalSingletone< Marker > 
130{
131  type Target = Option< bool >;
132
133  #[ inline( always ) ]
134  fn deref( &self ) -> &Option< bool > 
135  {
136  &self.0
137 }
138}
139
140impl< Marker > AsRef< Option< bool >> for AttributePropertyOptionalSingletone< Marker > 
141{
142  #[ inline( always ) ]
143  fn as_ref( &self ) -> &Option< bool > 
144  {
145  &self.0
146 }
147}