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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
// Copyright 2018 Phillip Lord, Newcastle University
//
// Licensed under either the Apache License, Version 2.0 or the MIT
// licence at your option. This file may not be copied, modified or
// distributed except according to those terms.
/*!
This crate enables attaching metadata to C-like Enums (or strictly any
Enum). The metadata can be of an arbitrary type, but must be of the
same type for the all variants although can be different values.
This fills the use-case when the Enum variants are flags for something
else -- for example, HTTP error codes, or parts of a syntax tree
associated with some explicit string rendering when concretized.
The crate provides two macros which can be used to add this metadata
to the enum. This can be done at a separate location from the
declaration of the enum. The first macro is for values that are
determined at compile time:
# Syntax
```ignore
meta! {
EnumType, MetaDataType;
VariantOne, "MetadataValue";
VariantTwo, "MetadataValue";
VariantThree, "MetadataValue";
}
```
In this case, the type of the metadata must be defined before hand and
will be either a reference type or a copy type, as the values will be
returned statically. For example:
```rust
#[macro_use] extern crate enum_meta;
use enum_meta::*;
enum Colour
{
Red, Orange, Green
}
meta!{
Colour, &'static str;
Red, "Red";
Orange, "Orange";
Green, "Green";
}
fn main() {
assert_eq!(Colour::Orange.meta(), "Orange");
}
```
A second macro allows the values to be calculated at run time on first
access. The values are calculated only once.
```rust
#[macro_use] extern crate enum_meta;
use enum_meta::*;
pub enum Colour{
Red,
Orange,
Green
}
lazy_meta!{
Colour, String, META_Colour;
Red, format!("{}:{}", 1, "Red");
Orange, format!("{}:{}", 2, "Orange");
Green, format!("{}:{}", 3, "Green");
}
fn main() {
assert_eq!(Colour::Red.meta(), "1:Red");
}
```
In this case, values are stored in a global variable whose name is
provided (`META_Colour2` in this instance). Values returned are
references to the given return type.
Reverse lookup is not supported in-directly, by providing an `all`
method which returns all the enum variants as a vector; this allows
construction of a reverse lookup function; this is hard to achieve in
general, requires putting a lot of constraints on the type of the
metadata and can only sensibly support lookup by direct equality with
the metadata.
```
#[macro_use] extern crate enum_meta;
use enum_meta::*;
// These derives are required by `assert_eq` rather than `lazy_meta`
#[derive(Debug, Eq, PartialEq)]
pub enum Colour{
Red,
Orange,
Green
}
meta!{
Colour, String;
Red, format!("{}:{}", 1, "Red");
Orange, format!("{}:{}", 2, "Orange");
Green, format!("{}:{}", 3, "Green");
}
fn main() {
assert_eq!(Colour::all(),
vec![Colour::Red, Colour::Orange, Colour::Green]);
}
```
*/
#![macro_use]
#[allow(unused_imports)]
#[macro_use] extern crate lazy_static;
pub use lazy_static::*;
pub use std::collections::HashMap;
pub use std::mem::discriminant;
pub use std::mem::Discriminant;
/// Trait for accessing metadata
pub trait Meta<R>
where Self:Sized {
fn meta(&self) -> R;
fn all() -> Vec<Self>;
}
#[macro_export]
macro_rules! meta {
($enum_type:ident, $return_type:ty;
$($enum_variant:ident, $return_value:expr);*
) => {
impl Meta<$return_type> for $enum_type {
fn meta(&self) -> $return_type {
match self {
$(
$enum_type::$enum_variant => {
$return_value
}
)*
}
}
fn all() -> Vec<$enum_type>{
vec![
$(
$enum_type::$enum_variant
),*
]
}
}
};
// Trailing semi
($enum_type:ident, $return_type:ty;
$($enum_variant:ident, $return_value:expr);+ ;
) => {
meta!{
$enum_type, $return_type;
$( $enum_variant, $return_value );*
}
};
}
#[macro_export]
macro_rules! lazy_meta {
($enum_type:ident, $return_type:ty, $storage:ident;
$($enum_variant:ident, $return_expr:expr);*
) => {
lazy_static! {
static ref $storage: HashMap<Discriminant<$enum_type>,$return_type>
= {
let mut m = HashMap::new();
$(
m.insert(discriminant(&$enum_type::$enum_variant),
$return_expr);
)*
m
};
}
impl <'a> Meta<&'a $return_type> for $enum_type {
fn meta(&self) -> &'a $return_type {
$storage.get(&discriminant(&self)).unwrap()
}
fn all() -> Vec<$enum_type>{
vec![
$(
$enum_type::$enum_variant
),*
]
}
}
impl $enum_type {
// This does nothing at all, but will fail if we do not pass all of
// the entities that we need.
#[allow(dead_code)]
fn meta_check(&self) {
match self {
$(
$enum_type::$enum_variant => {}
),*
}
}
}
};
// Trailing semi
($enum_type:ident, $return_type:ty, $storage:ident;
$($enum_variant:ident, $return_expr:expr);+ ;
) => {
lazy_meta!{
$enum_type, $return_type, $storage;
$( $enum_variant, $return_expr );*
}
};
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_meta(){
enum Colour
{
Red,
Orange,
Green
}
meta!{
Colour, &'static str;
Red, "Red";
Orange, "Orange";
Green, "Green"
}
assert_eq!(Colour::Red.meta(), "Red");
assert_eq!(Colour::Orange.meta(), "Orange");
assert_eq!(Colour::Green.meta(), "Green");
}
#[test]
fn test_all(){
#[derive(Debug, Eq, PartialEq)]
enum Colour
{
Red,
Orange,
Green
}
meta!{
Colour, &'static str;
Red, "Red";
Orange, "Orange";
Green, "Green"
}
assert_eq!(vec![Colour::Red,
Colour::Orange,
Colour::Green], Colour::all());
}
#[test]
fn test_meta_complex_return_type(){
enum Colour
{
Red,
Orange,
Green
}
meta!{
Colour, (&'static str, i64);
Red, ("Red", 10);
Orange, ("Orange", 11);
Green, ("Green", 12)
}
assert_eq!(Colour::Red.meta(), ("Red", 10));
assert_eq!(Colour::Orange.meta(), ("Orange", 11));
assert_eq!(Colour::Green.meta(), ("Green", 12));
}
#[test]
fn test_meta_trailing_semi(){
enum Colour
{
Red,
Orange,
Green
}
meta!{
Colour, &'static str;
Red, "Red";
Orange, "Orange";
Green, "Green";
}
assert_eq!(Colour::Red.meta(), "Red");
assert_eq!(Colour::Orange.meta(), "Orange");
assert_eq!(Colour::Green.meta(), "Green");
}
#[test]
fn test_lazy_meta(){
enum Colour
{
Red,
Orange,
Green
}
lazy_meta!{
Colour, String, TEST1;
Red, "Red".to_string();
Orange, "Orange".to_string();
Green, "Green".to_string();
}
assert_eq!(Colour::Red.meta(), "Red");
assert_eq!(Colour::Orange.meta(), "Orange");
assert_eq!(Colour::Green.meta(), "Green");
}
#[test]
fn test_lazy_all(){
#[derive(Debug, Eq, PartialEq)]
enum Colour
{
Red,
Orange,
Green
}
lazy_meta!{
Colour, String, TEST1;
Red, "Red".to_string();
Orange, "Orange".to_string();
Green, "Green".to_string();
}
assert_eq!(Colour::all(),
vec![Colour::Red,
Colour::Orange,
Colour::Green]
);
}
}