|
| 1 | +/// Macro for declaring a character property. |
| 2 | +/// |
| 3 | +/// # Syntax (Enumerated Property) |
| 4 | +/// |
| 5 | +/// ``` |
| 6 | +/// # #[macro_use] extern crate unic_utils; |
| 7 | +/// # fn main() {} |
| 8 | +/// char_property! { |
| 9 | +/// /// Zero or more attributes |
| 10 | +/// pub enum PropertyName { |
| 11 | +/// /// Exactly one attribute |
| 12 | +/// RustName { |
| 13 | +/// abbr => AbbrName, |
| 14 | +/// long => Long_Name, |
| 15 | +/// display => "&'static str that is a nicer presentation of the name", |
| 16 | +/// } |
| 17 | +/// |
| 18 | +/// /// All annotations on the variant are optional* |
| 19 | +/// Variant2 { |
| 20 | +/// abbr => V2, // *abbr is required for Enumerated Properties |
| 21 | +/// } |
| 22 | +/// } |
| 23 | +/// |
| 24 | +/// /// Zero or more attributes |
| 25 | +/// pub mod abbr_names for abbr; |
| 26 | +/// |
| 27 | +/// /// Zero or more attributes |
| 28 | +/// pub mod long_names for long; |
| 29 | +/// } |
| 30 | +/// |
| 31 | +/// // You must impl (Partial/Complete)CharProperty manually. |
| 32 | +/// # impl unic_utils::char_property::PartialCharProperty for PropertyName { |
| 33 | +/// # fn of(_: char) -> Option<Self> { None } |
| 34 | +/// # } |
| 35 | +/// ``` |
| 36 | +/// |
| 37 | +/// # Effect |
| 38 | +/// |
| 39 | +/// - Implements `CharProperty` with the `abbr` and `long` presented in the appropriate method |
| 40 | +/// - Implements `FromStr` accepting any of the rust, abbr, or long names |
| 41 | +/// - Implements `Display` using the given string, falling back when not provided on |
| 42 | +/// the long name, the short name, and the rust name, in that order |
| 43 | +/// - Populates the module `abbr_names` with `pub use` bindings of variants to their abbr names |
| 44 | +/// - Populates the module `long_names` with `pub use` bindings of variants to their long names |
| 45 | +/// - Maintains all documentation comments and other `#[attributes]` as would be expected |
| 46 | +/// (with some caveats, listed below) |
| 47 | +/// |
| 48 | +/// # Limitations |
| 49 | +/// |
| 50 | +/// Due to [rust-lang/rust/#24189](https://github.com/rust-lang/rust/issues/24189), (fixed in |
| 51 | +/// [rust-lang/rust/#42913](https://github.com/rust-lang/rust/pull/42913), landing in 1.20), |
| 52 | +/// exactly one attribute line must be used on each variant. On 1.20 or higher, one or more may |
| 53 | +/// be used, and the restriction can be relaxed back the intended zero or more by replacing |
| 54 | +/// `$(#[$variant_meta:meta])+` with `$(#[$variant_meta:meta])*`, and |
| 55 | +/// `$(#[$variant_meta])+` with `$(#[$variant_meta])*`, and |
| 56 | +/// `$(#[$ident_meta:meta])+` with `$(#[$ident_meta:meta])*` and |
| 57 | +/// `$(#[$ident_meta])+` with `$(#[$ident_meta])*`, and |
| 58 | +/// `$(#[$rest_meta:meta])+` with `$(#[$rest_meta:meta])*`, and |
| 59 | +/// `$(#[$rest_meta])+` with `$(#[$rest_meta])*`, and |
| 60 | +/// `$(#[$queue_meta:meta])+` with `$(#[$queue_meta:meta])*`, and |
| 61 | +/// `$(#[$queue_meta])+` with `$(#[$queue_meta])*` |
| 62 | +// TODO: Once adopting 1.20, fix the macro to work with zero attributes on variants (see above) |
| 63 | +#[macro_export] |
| 64 | +macro_rules! char_property { |
| 65 | + ( |
| 66 | + $(#[$name_meta:meta])* pub enum $name:ident { |
| 67 | + $( $(#[$variant_meta:meta])+ $variant:ident $tt:tt )* |
| 68 | + } |
| 69 | + |
| 70 | + $(#[$abbr_names_meta:meta])* pub mod $abbr_names:ident for abbr; |
| 71 | + $(#[$long_names_meta:meta])* pub mod $long_names:ident for long; |
| 72 | + ) => { |
| 73 | + __char_property_internal! { |
| 74 | + $(#[$name_meta])* pub enum $name |
| 75 | + $(#[$abbr_names_meta])* pub mod $abbr_names |
| 76 | + $(#[$long_names_meta])* pub mod $long_names |
| 77 | + |
| 78 | + variant [ ] |
| 79 | + abbr [ ] |
| 80 | + long [ ] |
| 81 | + display [ ] |
| 82 | + |
| 83 | + buffer [ ] |
| 84 | + queue [ $( $(#[$variant_meta])+ $variant $tt )* ] |
| 85 | + } |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +#[macro_export] |
| 90 | +macro_rules! __char_property_internal { |
| 91 | + // == Queue => Buffer == // |
| 92 | + ( |
| 93 | + $(#[$name_meta:meta])* pub enum $name:ident |
| 94 | + $(#[$abbr_names_meta:meta])* pub mod $abbr_names:ident |
| 95 | + $(#[$long_names_meta:meta])* pub mod $long_names:ident |
| 96 | + |
| 97 | + variant [ $( $(#[$variant_meta:meta])+ $variant:ident ; )* ] |
| 98 | + abbr [ $( $abbr_variant:ident $abbr:ident ; )* ] |
| 99 | + long [ $( $long_variant:ident $long:ident ; )* ] |
| 100 | + display [ $( $display_variant:ident $display:expr ; )* ] |
| 101 | + |
| 102 | + buffer [ ] |
| 103 | + queue [ |
| 104 | + $(#[$ident_meta:meta])+ $ident:ident $ident_tt:tt |
| 105 | + $( $(#[$rest_meta:meta])+ $rest:ident $rest_tt:tt )* |
| 106 | + ] |
| 107 | + ) => { |
| 108 | + __char_property_internal! { |
| 109 | + $(#[$name_meta])* pub enum $name |
| 110 | + $(#[$abbr_names_meta])* pub mod $abbr_names |
| 111 | + $(#[$long_names_meta])* pub mod $long_names |
| 112 | + |
| 113 | + variant [ |
| 114 | + $( $(#[$variant_meta])+ $variant ; )* |
| 115 | + $(#[$ident_meta])+ $ident ; |
| 116 | + ] |
| 117 | + abbr [ $( $abbr_variant $abbr ; )* ] |
| 118 | + long [ $( $long_variant $long ; )* ] |
| 119 | + display [ $( $display_variant $display ; )* ] |
| 120 | + |
| 121 | + buffer [ $ident $ident_tt ] |
| 122 | + queue [ $( $(#[$rest_meta])+ $rest $rest_tt )* ] |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + // == Buffer -- Abbr Name == // |
| 127 | + ( |
| 128 | + $(#[$name_meta:meta])* pub enum $name:ident |
| 129 | + $(#[$abbr_names_meta:meta])* pub mod $abbr_names:ident |
| 130 | + $(#[$long_names_meta:meta])* pub mod $long_names:ident |
| 131 | + |
| 132 | + variant [ $( $(#[$variant_meta:meta])+ $variant:ident ; )* ] |
| 133 | + abbr [ $( $abbr_variant:ident $abbr:ident ; )* ] |
| 134 | + long [ $( $long_variant:ident $long:ident ; )* ] |
| 135 | + display [ $( $display_variant:ident $display:expr ; )* ] |
| 136 | + |
| 137 | + buffer [ $ident:ident { |
| 138 | + abbr => $ident_abbr:ident , |
| 139 | + $( $rest:tt )* |
| 140 | + } ] |
| 141 | + queue [ $( $(#[$queue_meta:meta])+ $queue:ident $queue_tt:tt )* ] |
| 142 | + ) => { |
| 143 | + __char_property_internal! { |
| 144 | + $(#[$name_meta])* pub enum $name |
| 145 | + $(#[$abbr_names_meta])* pub mod $abbr_names |
| 146 | + $(#[$long_names_meta])* pub mod $long_names |
| 147 | + |
| 148 | + variant [ $( $(#[$variant_meta])+ $variant ; )* ] |
| 149 | + abbr [ |
| 150 | + $( $abbr_variant $abbr ; )* |
| 151 | + $ident $ident_abbr ; |
| 152 | + ] |
| 153 | + long [ $( $long_variant $long ; )* ] |
| 154 | + display [ $( $display_variant $display ; )* ] |
| 155 | + |
| 156 | + buffer [ $ident { $( $rest )* } ] |
| 157 | + queue [ $( $(#[$queue_meta])+ $queue $queue_tt )* ] |
| 158 | + } |
| 159 | + }; |
| 160 | + |
| 161 | + // == Buffer -- Long Name == // |
| 162 | + ( |
| 163 | + $(#[$name_meta:meta])* pub enum $name:ident |
| 164 | + $(#[$abbr_names_meta:meta])* pub mod $abbr_names:ident |
| 165 | + $(#[$long_names_meta:meta])* pub mod $long_names:ident |
| 166 | + |
| 167 | + variant [ $( $(#[$variant_meta:meta])+ $variant:ident ; )* ] |
| 168 | + abbr [ $( $abbr_variant:ident $abbr:ident ; )* ] |
| 169 | + long [ $( $long_variant:ident $long:ident ; )* ] |
| 170 | + display [ $( $display_variant:ident $display:expr ; )* ] |
| 171 | + |
| 172 | + buffer [ $ident:ident { |
| 173 | + long => $ident_long:ident , |
| 174 | + $( $rest:tt )* |
| 175 | + } ] |
| 176 | + queue [ $( $(#[$queue_meta:meta])+ $queue:ident $queue_tt:tt )* ] |
| 177 | + ) => { |
| 178 | + __char_property_internal! { |
| 179 | + $(#[$name_meta])* pub enum $name |
| 180 | + $(#[$abbr_names_meta])* pub mod $abbr_names |
| 181 | + $(#[$long_names_meta])* pub mod $long_names |
| 182 | + |
| 183 | + variant [ $( $(#[$variant_meta])+ $variant ; )* ] |
| 184 | + abbr [ $( $abbr_variant $abbr ; )* ] |
| 185 | + long [ |
| 186 | + $( $long_variant $long ; )* |
| 187 | + $ident $ident_long ; |
| 188 | + ] |
| 189 | + display [ $( $display_variant $display ; )* ] |
| 190 | + |
| 191 | + buffer [ $ident { $( $rest )* } ] |
| 192 | + queue [ $( $(#[$queue_meta])+ $queue $queue_tt )* ] |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + // == Buffer -- Display // |
| 197 | + ( |
| 198 | + $(#[$name_meta:meta])* pub enum $name:ident |
| 199 | + $(#[$abbr_names_meta:meta])* pub mod $abbr_names:ident |
| 200 | + $(#[$long_names_meta:meta])* pub mod $long_names:ident |
| 201 | + |
| 202 | + variant [ $( $(#[$variant_meta:meta])+ $variant:ident ; )* ] |
| 203 | + abbr [ $( $abbr_variant:ident $abbr:ident ; )* ] |
| 204 | + long [ $( $long_variant:ident $long:ident ; )* ] |
| 205 | + display [ $( $display_variant:ident $display:expr ; )* ] |
| 206 | + |
| 207 | + buffer [ $ident:ident { |
| 208 | + display => $ident_display:expr , |
| 209 | + $( $rest:tt )* |
| 210 | + } ] |
| 211 | + queue [ $( $(#[$queue_meta:meta])+ $queue:ident $queue_tt:tt )* ] |
| 212 | + ) => { |
| 213 | + __char_property_internal! { |
| 214 | + $(#[$name_meta])* pub enum $name |
| 215 | + $(#[$abbr_names_meta])* pub mod $abbr_names |
| 216 | + $(#[$long_names_meta])* pub mod $long_names |
| 217 | + |
| 218 | + variant [ $( $(#[$variant_meta])+ $variant ; )* ] |
| 219 | + abbr [ $( $abbr_variant $abbr ; )* ] |
| 220 | + long [ $( $long_variant $long ; )* ] |
| 221 | + display [ |
| 222 | + $( $display_variant $display ; )* |
| 223 | + $ident $ident_display ; |
| 224 | + ] |
| 225 | + |
| 226 | + buffer [ $ident { $( $rest )* } ] |
| 227 | + queue [ $( $(#[$queue_meta])+ $queue $queue_tt )* ] |
| 228 | + } |
| 229 | + }; |
| 230 | + |
| 231 | + // == Buffer -- Empty == // |
| 232 | + ( |
| 233 | + $(#[$name_meta:meta])* pub enum $name:ident |
| 234 | + $(#[$abbr_names_meta:meta])* pub mod $abbr_names:ident |
| 235 | + $(#[$long_names_meta:meta])* pub mod $long_names:ident |
| 236 | + |
| 237 | + variant [ $( $(#[$variant_meta:meta])+ $variant:ident ; )* ] |
| 238 | + abbr [ $( $abbr_variant:ident $abbr:ident ; )* ] |
| 239 | + long [ $( $long_variant:ident $long:ident ; )* ] |
| 240 | + display [ $( $display_variant:ident $display:expr ; )* ] |
| 241 | + |
| 242 | + buffer [ $ident:ident {} ] |
| 243 | + queue [ $( $(#[$queue_meta:meta])+ $queue:ident $queue_tt:tt )* ] |
| 244 | + ) => { |
| 245 | + __char_property_internal! { |
| 246 | + $(#[$name_meta])* pub enum $name |
| 247 | + $(#[$abbr_names_meta])* pub mod $abbr_names |
| 248 | + $(#[$long_names_meta])* pub mod $long_names |
| 249 | + |
| 250 | + variant [ $( $(#[$variant_meta])+ $variant ; )* ] |
| 251 | + abbr [ $( $abbr_variant $abbr ; )* ] |
| 252 | + long [ $( $long_variant $long ; )* ] |
| 253 | + display [ $( $display_variant $display ; )* ] |
| 254 | + |
| 255 | + buffer [ ] |
| 256 | + queue [ $( $(#[$queue_meta])+ $queue $queue_tt )* ] |
| 257 | + } |
| 258 | + }; |
| 259 | + |
| 260 | + // == Final formatting == // |
| 261 | + ( |
| 262 | + $(#[$name_meta:meta])* pub enum $name:ident |
| 263 | + $(#[$abbr_names_meta:meta])* pub mod $abbr_names:ident |
| 264 | + $(#[$long_names_meta:meta])* pub mod $long_names:ident |
| 265 | + |
| 266 | + variant [ $( $(#[$variant_meta:meta])+ $variant:ident ; )* ] |
| 267 | + abbr [ $( $abbr_variant:ident $abbr:ident ; )* ] |
| 268 | + long [ $( $long_variant:ident $long:ident ; )* ] |
| 269 | + display [ $( $display_variant:ident $display:expr ; )* ] |
| 270 | + |
| 271 | + buffer [ ] |
| 272 | + queue [ ] |
| 273 | + ) => { |
| 274 | + $(#[$name_meta])* |
| 275 | + #[allow(bad_style)] |
| 276 | + #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] |
| 277 | + pub enum $name { |
| 278 | + $( $(#[$variant_meta])+ $variant, )* |
| 279 | + } |
| 280 | + |
| 281 | + $(#[$abbr_names_meta])* |
| 282 | + #[allow(bad_style)] |
| 283 | + pub mod $abbr_names { |
| 284 | + $( pub use super::$name::$abbr_variant as $abbr; )* |
| 285 | + } |
| 286 | + |
| 287 | + $(#[$long_names_meta])* |
| 288 | + #[allow(bad_style)] |
| 289 | + pub mod $long_names { |
| 290 | + $( pub use super::$name::$long_variant as $long; )* |
| 291 | + } |
| 292 | + |
| 293 | + #[allow(bad_style)] |
| 294 | + #[allow(unreachable_patterns)] |
| 295 | + impl ::std::str::FromStr for $name { |
| 296 | + type Err = (); |
| 297 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 298 | + match s { |
| 299 | + $( stringify!($variant) => Ok($name::$variant), )* |
| 300 | + $( stringify!($abbr) => Ok($name::$abbr_variant), )* |
| 301 | + $( stringify!($long) => Ok($name::$long_variant), )* |
| 302 | + _ => Err(()), |
| 303 | + } |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + #[allow(bad_style)] |
| 308 | + #[allow(unreachable_patterns)] |
| 309 | + impl ::std::fmt::Display for $name { |
| 310 | + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
| 311 | + match *self { |
| 312 | + $( $name::$display_variant => write!(f, "{}", $display), )* |
| 313 | + $( $name::$long_variant => write!(f, "{}", stringify!($long).replace('_', " ")), )* |
| 314 | + _ => write!(f, "{}", match *self { |
| 315 | + $( $name::$abbr_variant => stringify!($abbr), )* |
| 316 | + $( $name::$variant => stringify!($variant), )* |
| 317 | + }) |
| 318 | + } |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + #[allow(bad_style)] |
| 323 | + impl $crate::char_property::EnumeratedCharProperty for $name { |
| 324 | + fn abbr_name(&self) -> &'static str { |
| 325 | + match *self { |
| 326 | + $( $name::$abbr_variant => stringify!($abbr), )* |
| 327 | + // No catch all variant |
| 328 | + // Abbr name is required on Enumerated properties |
| 329 | + } |
| 330 | + } |
| 331 | + |
| 332 | + fn all_values() -> &'static [$name] { |
| 333 | + const VALUES: &[$name] = &[ |
| 334 | + $($name::$variant,)+ |
| 335 | + ]; |
| 336 | + VALUES |
| 337 | + } |
| 338 | + } |
| 339 | + }; |
| 340 | +} |
0 commit comments