| 1 | macro_rules! storage_of { |
| 2 | ($id:ident, true) => { bool }; |
| 3 | ($id:ident, false) => { bool }; |
| 4 | ($id:ident, [ $($value: expr),* ]) => { std::collections::HashSet<String> }; |
| 5 | ($id:ident, $(=int $check: expr;)+ $_: expr) => { u64 }; |
| 6 | ($id:ident, $(=enum $k: ident;)+ $_: ident) => { $crate::defaults::enums::$id }; |
| 7 | ($id:ident, None) => { Option<Box<str>> }; |
| 8 | ($id:ident, $_: expr) => { Box<str> }; |
| 9 | } |
| 10 | |
| 11 | macro_rules! referent_of { |
| 12 | ($id:ident, true) => { bool }; |
| 13 | ($id:ident, false) => { bool }; |
| 14 | ($id:ident, [ $($value: expr),* ]) => { &std::collections::HashSet<String> }; |
| 15 | ($id:ident, $(=int $check: expr;)+ $_: expr) => { u64 }; |
| 16 | ($id:ident, $(=enum $k: ident;)+ $_: ident) => { $crate::defaults::enums::$id }; |
| 17 | ($id:ident, None) => { Option<&str> }; |
| 18 | ($id:ident, $_: expr) => { &str }; |
| 19 | } |
| 20 | |
| 21 | macro_rules! initializer_of { |
| 22 | ($id:ident, true) => { true }; |
| 23 | ($id:ident, false) => { false }; |
| 24 | ($id:ident, [ $($value: expr),* ]) => { [$($value),*].into_iter().map(|s: &str| s.to_string()).collect::<std::collections::HashSet<_>>() }; |
| 25 | ($id:ident, $(=int $check: expr;)+ $value: expr) => { $value }; |
| 26 | ($id:ident, $(=enum $k: ident;)+ $value: ident) => { $crate::defaults::enums::$id::$value }; |
| 27 | ($id:ident, None) => { None }; |
| 28 | ($id:ident, $value: expr) => { $value.into() }; |
| 29 | ($id:ident, $($_: tt)*) => { return None }; |
| 30 | } |
| 31 | |
| 32 | macro_rules! result_of { |
| 33 | ($id:expr, true) => { |
| 34 | $id |
| 35 | }; |
| 36 | ($id:expr, false) => { |
| 37 | $id |
| 38 | }; |
| 39 | ($id:expr, [ $($value: expr),* ]) => { |
| 40 | &$id |
| 41 | }; |
| 42 | ($id:expr, $(=value $k: expr;)+ $_: expr) => { |
| 43 | $id |
| 44 | }; |
| 45 | ($id:expr, None) => { |
| 46 | $id.as_deref() |
| 47 | }; |
| 48 | ($id:expr, $_: expr) => { |
| 49 | $id.as_ref() |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | macro_rules! modifier_of { |
| 54 | ($id:ident, true) => { |
| 55 | $crate::defaults::SettingKind::Flag(Box::new(move |obj: &mut Settings| obj.$id = true)) |
| 56 | }; |
| 57 | ($id:ident, false) => { |
| 58 | $crate::defaults::SettingKind::Flag(Box::new(move |obj: &mut Settings| obj.$id = true)) |
| 59 | }; |
| 60 | ($id:ident, [ $($value: expr),* ]) => { |
| 61 | $crate::defaults::SettingKind::List(|mode, list| { |
| 62 | Box::new(move |obj: &mut Settings| match mode { |
| 63 | ListMode::Set => obj.$id = list.into_iter().collect(), |
| 64 | ListMode::Add => obj.$id.extend(list), |
| 65 | ListMode::Del => { |
| 66 | for key in list { |
| 67 | obj.$id.remove(&key); |
| 68 | } |
| 69 | } |
| 70 | }) |
| 71 | }) |
| 72 | }; |
| 73 | ($id:ident, =int $first:literal ..= $last: literal $(@ $radix: literal)?; $value: expr) => { |
| 74 | #[allow(clippy::from_str_radix_10)] |
| 75 | $crate::defaults::SettingKind::Integer(|text| { |
| 76 | u64::from_str_radix(text, 10$(*0 + $radix)?) |
| 77 | .ok() |
| 78 | .filter(|val| ($first ..= $last).contains(val)) |
| 79 | .map(|i| { |
| 80 | Box::new(move |obj: &mut Settings| obj.$id = i) as SettingsModifier |
| 81 | }) |
| 82 | }) |
| 83 | }; |
| 84 | ($id:ident, =int $fn: expr; $value: expr) => { |
| 85 | $crate::defaults::SettingKind::Integer(|text| { |
| 86 | $fn(&text).map(|i| { |
| 87 | Box::new(move |obj: &mut Settings| obj.$id = i) as SettingsModifier |
| 88 | }) |
| 89 | }) |
| 90 | }; |
| 91 | ($id:ident, $(=int $check: expr;)+ $value: expr) => { compile_error!("bla") }; |
| 92 | ($id:ident, $(=enum $key: ident;)+ $value: ident) => { |
| 93 | $crate::defaults::SettingKind::Text(|key| match key { |
| 94 | $( |
| 95 | stringify!($key) => { |
| 96 | Some(Box::new(move |obj: &mut Settings| obj.$id = $crate::defaults::enums::$id::$key)) |
| 97 | }, |
| 98 | )* |
| 99 | _ => None, |
| 100 | }) |
| 101 | }; |
| 102 | ($id:ident, None) => { |
| 103 | $crate::defaults::SettingKind::Text(|text| { |
| 104 | let text = text.into(); |
| 105 | Some(Box::new(move |obj: &mut Settings| obj.$id = Some(text))) |
| 106 | }) |
| 107 | }; |
| 108 | ($id:ident, $value: expr) => { |
| 109 | $crate::defaults::SettingKind::Text(|text| { |
| 110 | let text = text.into(); |
| 111 | Some(Box::new(move |obj: &mut Settings| obj.$id = text)) |
| 112 | }) |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | macro_rules! has_standard_negator { |
| 117 | (true) => { |
| 118 | true |
| 119 | }; |
| 120 | (false) => { |
| 121 | true |
| 122 | }; |
| 123 | ([$($_: expr),*]) => { |
| 124 | true |
| 125 | }; |
| 126 | ($($_: tt)*) => { |
| 127 | false |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | // this macro allows us to help the compiler generate more efficient code in 'fn negate' |
| 132 | // and enables the way 'fn set' is made |
| 133 | macro_rules! ifdef { |
| 134 | (; $then: expr; $else: expr) => { |
| 135 | $else |
| 136 | }; |
| 137 | ($($_: expr)+; $then: expr; $else: expr) => { |
| 138 | $then |
| 139 | }; |
| 140 | } |
| 141 | |
| 142 | macro_rules! emit { |
| 143 | (ignored; $($def: tt)*) => { }; |
| 144 | ( ; $($def: tt)*) => { $($def)* }; |
| 145 | } |
| 146 | |
| 147 | macro_rules! defaults { |
| 148 | ($($name:ident = $value:tt $((!= $negate:tt))? $([$($key:ident),*])? $([$first:literal ..= $last:literal$(; radix: $radix: expr)?])? $({$fn: expr})? $(#$attribute:ident)?)*) => { |
| 149 | #[allow(non_camel_case_types)] |
| 150 | mod enums { |
| 151 | $($( |
| 152 | #[derive(Clone,Copy,Debug,Default)] |
| 153 | #[cfg_attr(test, derive(PartialEq, Eq))] |
| 154 | pub enum $name { #[default] $($key),* } |
| 155 | )?)* |
| 156 | } |
| 157 | |
| 158 | #[derive(Clone)] |
| 159 | pub struct Settings { |
| 160 | $($name: storage_of!($name, $(=int $fn;)?$(=int $first;)?$($(=enum $key;)*)? $value)),* |
| 161 | } |
| 162 | |
| 163 | // we add setters to make sure the settings-object is read only, and to generate 'unused variable' warnings |
| 164 | impl Settings { |
| 165 | $( |
| 166 | emit! { $($attribute)?; |
| 167 | pub fn $name(&self) -> referent_of!($name, $(=int $fn;)?$(=int $first;)?$($(=enum $key;)*)? $value) { |
| 168 | result_of!(self.$name, $(=value $fn;)?$(=value $first;)?$($(=value $key;)*)? $value) |
| 169 | } |
| 170 | } |
| 171 | )* |
| 172 | } |
| 173 | |
| 174 | impl Default for Settings { |
| 175 | fn default() -> Self { |
| 176 | Self { |
| 177 | $($name: initializer_of!($name, $(=int $fn;)?$(=int $first;)?$($(=enum $key;)*)? $value)),* |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | pub fn negate(name: &str) -> Option<SettingsModifier> { |
| 183 | match name { |
| 184 | $( |
| 185 | stringify!($name) if ifdef!($($negate)?; true; has_standard_negator!($value)) => { |
| 186 | let value = ifdef!($($negate)?; |
| 187 | // this setting has an explicit negation; use that |
| 188 | initializer_of!($name, $(=int $fn;)?$(=int $first;)?$($(=enum $key;)*)? $($negate)?); |
| 189 | // for bool and sets, false/empty works (for other types this is dead code) |
| 190 | Default::default() |
| 191 | ); |
| 192 | Some(Box::new(move |obj: &mut Settings| obj.$name = value)) |
| 193 | }, |
| 194 | )* |
| 195 | _ => None |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | pub fn set(name: &str) -> Option<SettingKind> { |
| 200 | match name { |
| 201 | $( |
| 202 | stringify!($name) => Some(modifier_of!($name, $(=int $fn;)?$(=int $first ..= $last $(@ $radix)?;)?$($(=enum $key;)*)? $value)), |
| 203 | )* |
| 204 | _ => None, |
| 205 | } |
| 206 | } |
| 207 | }; |
| 208 | } |
| 209 | |
| 210 | pub(super) use defaults; |
| 211 | pub(super) use emit; |
| 212 | pub(super) use has_standard_negator; |
| 213 | pub(super) use ifdef; |
| 214 | pub(super) use initializer_of; |
| 215 | pub(super) use modifier_of; |
| 216 | pub(super) use referent_of; |
| 217 | pub(super) use result_of; |
| 218 | pub(super) use storage_of; |