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
extern crate rayon;
extern crate boow;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate variant_name;
mod dst;
mod export;
mod transform;
pub use dst::*;
pub use export::*;
pub use transform::*;
pub use self::variant_name::VariantName;
pub trait DefaultFor: VariantName {
fn default_for(variant_name: &str) -> Self;
}
pub trait EditableVariants: VariantName {
fn editable_variants() -> &'static [&'static str];
fn editable(variant_name: &str) -> bool {
Self::editable_variants().contains(&variant_name)
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! cake_fn {
($fn_name: ident<$enum_name: ident, $err_type: ty>() $fn_block: block) => {
fn $fn_name(
_: Vec<::std::borrow::Cow<$enum_name>>,
) -> Vec<Result<$enum_name, $err_type>> {
$fn_block
}
};
($fn_name: ident<$enum_name: ident, $err_type: ty>($($x: ident: $x_type: ident),*) $fn_block: block) => {
fn $fn_name(
input: Vec<::std::borrow::Cow<$enum_name>>,
) -> Vec<Result<$enum_name, $err_type>> {
#[allow(non_camel_case_types)]
enum Args { $($x,)* }
if let ($(&$enum_name::$x_type(ref $x), )*) = ($(&*input[Args::$x as usize], )*) {
$fn_block
} else {
panic!("Unexpected argument!")
}
}
};
}
#[macro_export]
macro_rules! cake_transform {
($description: expr, $fn_name: ident<$enum_name: ident, $err_type: ty>($($x: ident: $x_type: ident $(= $x_default_val: expr), *),*) -> $($out_type: ident),* $fn_block: block) => {{
cake_fn!{$fn_name<$enum_name, $err_type>($($x: $x_type),*) $fn_block}
use std::borrow::Cow;
$crate::Transformation {
name: stringify!($fn_name),
description: Cow::Borrowed($description),
input: vec![$((stringify!($x_type), {
cake_some_first_value!($( $enum_name::$x_type($x_default_val) ),*)
}), )*],
output: vec![$(stringify!($out_type), )*],
algorithm: $crate::Algorithm::Function($fn_name),
}
}};
}
#[macro_export]
macro_rules! cake_constant {
($const_name: ident, $($x: expr),*) => {{
use std::borrow::Cow;
use $crate::VariantName;
let constant = vec![$($x, )*];
$crate::Transformation {
name: stringify!($const_name),
description: Cow::Borrowed(
concat!("Constant variable of type '", stringify!($const_name), "'")
),
input: vec![],
output: constant.iter().map(|c| c.variant_name()).collect(),
algorithm: $crate::Algorithm::Constant(constant),
}
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! cake_some_first_value {
() => {
None
};
($x:expr) => {
Some($x)
};
($x:expr, $($xs:expr)+) => {
compile_error!("Only zero or one value is expected.")
};
}