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
use std::collections::{HashMap, HashSet};
use convert_case::{Case, Casing};
use proc_macro_error::{abort, emit_error};
use quote::format_ident;
use syn::parse_quote;
use crate::params::{NumberArg, NumberKind};
use super::{attr_params::AttrParams, NumberValue};
#[derive(Debug)]
pub struct ExactVariant {
pub ident: syn::Ident,
pub value: NumberValue,
}
impl PartialEq for ExactVariant {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl Eq for ExactVariant {}
impl std::hash::Hash for ExactVariant {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}
#[derive(Debug)]
pub struct RangeVariant {
pub ident: syn::Ident,
pub start: Option<NumberValue>,
pub end: Option<NumberValue>,
pub half_open: bool,
}
pub struct Variants {
pub vis: syn::Visibility,
pub name: syn::Ident,
pub mod_name: syn::Ident,
pub value_name: syn::Ident,
pub exacts: HashSet<ExactVariant>,
pub ranges: Vec<RangeVariant>,
pub catchall: Option<syn::Ident>,
}
impl Variants {
pub fn from_item(params: &AttrParams, item: &mut syn::Item) -> Self {
let data;
if let syn::Item::Enum(d) = item {
data = d;
} else {
abort! {
item,
"Can only be applied `Specific` for enums"
}
}
if params.as_soft_or_hard.is_some() {
abort! {
item,
"The `as Soft` and `as Hard` parameters are not allowed on enums"
}
}
let vis = data.vis.clone();
let name = data.ident.clone();
let mod_name = format_ident!("clamped_{}", name.to_string().to_case(Case::Snake));
let value_name = format_ident!("{}Value", name);
data.vis = parse_quote!(pub);
let ty = ¶ms.integer;
let mut exacts = HashMap::new();
let mut ranges = Vec::new();
let mut catchall = None;
for variant in &mut data.variants {
match &variant.fields {
syn::Fields::Unit => {}
_ => {
abort! {
variant,
"Each variant must be a unit field"
}
}
}
let mut to_remove = vec![];
for (i, attr) in variant.attrs.iter_mut().enumerate() {
let p;
if let Some(path) = attr.path().get_ident() {
p = path;
} else {
continue;
}
match p.to_string().as_str() {
"eq" => {
to_remove.push(i);
struct NumberArgList(pub Vec<NumberArg>);
impl syn::parse::Parse for NumberArgList {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
Ok(Self(
syn::punctuated::Punctuated::<NumberArg, syn::Token![,]>::parse_terminated(input)?
.into_iter()
.collect(),
))
}
}
if let Ok(list) = attr.parse_args::<NumberArgList>() {
for val in list.0 {
let n = val.into_value(params.kind());
if let Some(prev) = exacts.insert(n, variant.ident.clone()) {
emit_error! {
attr,
"The value `{}` is already used by variant `{}`",
n,
prev;
hint = prev.span() => "Conflicting variant here";
}
}
params.abort_if_out_of_bounds(attr, n);
variant.fields = syn::Fields::Unnamed(parse_quote! {
(#value_name<#ty>)
});
}
} else {
emit_error! {
attr,
"The `#[eq]` attribute must be one or more integer literals"
}
}
}
"range" => {
to_remove.push(i);
if let Ok(val) = attr.parse_args::<syn::ExprRange>() {
let half_open = match val.limits {
syn::RangeLimits::HalfOpen(_) => true,
syn::RangeLimits::Closed(_) => false,
};
fn parse_val(
val: &Option<impl AsRef<syn::Expr>>,
kind: NumberKind,
) -> Option<NumberValue> {
let val = match &val {
Some(v) => v.as_ref(),
None => return None,
};
let val: NumberArg = parse_quote!(#val);
Some(val.into_value(kind))
}
let start = parse_val(&val.start, params.kind());
let end = parse_val(&val.end, params.kind());
if start.is_none() && end.is_none() {
emit_error! {
val,
"The range must have at least one bound. use `#[other]` for catchall"
}
continue;
}
if end.is_none() && half_open {
emit_error! {
val,
"The range must be closed if it has only one bound"
}
continue;
}
if let Some(start) = start {
params.abort_if_out_of_bounds(attr, start);
}
if let Some(end) = end {
params.abort_if_out_of_bounds(attr, end);
}
ranges.push((start, end, half_open, variant.ident.clone()));
let wrapper_name = format_ident!("{}Value", &variant.ident);
variant.fields = syn::Fields::Unnamed(parse_quote! {
(#wrapper_name)
});
} else {
emit_error! {
attr,
"The `#[range]` attribute must be a range literal"
}
}
}
"other" => {
to_remove.push(i);
if catchall.is_some() {
abort! {
attr,
"Only one `#[other]` attribute is allowed per enum"
}
}
catchall = Some(variant.ident.clone());
variant.fields = syn::Fields::Unnamed(parse_quote! {
(#value_name<#ty>)
});
}
_ => {}
}
}
for i in to_remove.into_iter().rev() {
variant.attrs.remove(i);
}
}
// check that all possible values between `params.lower_limit_value()` and `params.upper_limit_value()` are covered
let has_catchall = catchall.is_some();
let lower_limit = params.lower_limit_value();
let upper_limit = params.upper_limit_value();
let mut covered = if !has_catchall {
HashSet::with_capacity((upper_limit.clone() - lower_limit + 1).into_usize())
} else {
HashSet::new()
};
let this = Self {
vis,
name,
mod_name,
value_name,
exacts: exacts
.into_iter()
.map(|(n, v)| {
if !has_catchall {
covered.insert(n);
}
ExactVariant { ident: v, value: n }
})
.collect(),
ranges: ranges
.into_iter()
.map(|(s, e, h, v)| {
if !has_catchall {
match (s, e) {
(Some(s), Some(e)) => {
if h {
for n in s.range(e) {
covered.insert(n);
}
} else {
for n in s.range(e + 1) {
covered.insert(n);
}
}
}
(Some(s), None) => {
if h {
let upper_limit = upper_limit;
for n in s.range(upper_limit) {
covered.insert(n);
}
} else {
let upper_limit = upper_limit;
for n in s.range(upper_limit + 1) {
covered.insert(n);
}
}
}
(None, Some(e)) => {
if h {
let lower_limit = lower_limit;
for n in lower_limit.range(e) {
covered.insert(n);
}
} else {
let lower_limit = lower_limit;
for n in lower_limit.range(e + 1) {
covered.insert(n);
}
}
}
(None, None) => unreachable!("At least one bound must be present"),
}
}
RangeVariant {
ident: v,
start: s,
end: e,
half_open: h,
}
})
.collect(),
catchall,
};
if !has_catchall {
for n in lower_limit.range(upper_limit + 1) {
if !covered.contains(&n) {
emit_error! {
item,
"The value `{}` is not covered by any variant",
n;
hint = "Add a catchall variant with `#[other]` attribute";
}
}
}
}
this
}
}