Skip to content

Commit

Permalink
Simplify bounds handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jongiddy committed May 11, 2024
1 parent cf9bddc commit 18eaa49
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 162 deletions.
40 changes: 36 additions & 4 deletions src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::ops::Bound;

use proc_macro2::Span;
use syn::spanned::Spanned as _;
use syn::{Error, Expr, Result, Stmt};
use syn::{Error, Expr, Stmt};

#[allow(clippy::single_match)]
pub fn evaluate_bool(expr: &Expr) -> Result<bool> {
pub fn evaluate_bool(expr: &Expr) -> syn::Result<bool> {
match expr {
Expr::Binary(binary) => match binary.op {
syn::BinOp::Eq(_) => {
Expand Down Expand Up @@ -54,7 +57,12 @@ pub fn evaluate_bool(expr: &Expr) -> Result<bool> {
Err(Error::new(expr.span(), "unsupported boolean expression"))
}

pub fn evaluate_range(expr: &Expr) -> Option<&syn::ExprRange> {
// None -> Not a range
// Bound<Err(Span)> -> Span for a bound that is not an integer
#[allow(clippy::type_complexity)]
pub fn evaluate_range(
expr: &Expr,
) -> Option<(Bound<Result<usize, Span>>, Bound<Result<usize, Span>>)> {
match expr {
Expr::Block(block) => {
let mut stmts = block.block.stmts.iter().fuse();
Expand All @@ -63,7 +71,31 @@ pub fn evaluate_range(expr: &Expr) -> Option<&syn::ExprRange> {
}
}
Expr::Range(range) => {
return Some(range);
let start = range
.start
.as_ref()
.map(|start_expr| {
Bound::Included(
evaluate_usize(start_expr)
.map(Ok)
.unwrap_or_else(|| Err(start_expr.span())),
)
})
.unwrap_or(Bound::Unbounded);
let end = range
.end
.as_ref()
.map(|end_expr| {
let end = evaluate_usize(end_expr)
.map(Ok)
.unwrap_or_else(|| Err(end_expr.span()));
match range.limits {
syn::RangeLimits::HalfOpen(_) => Bound::Excluded(end),
syn::RangeLimits::Closed(_) => Bound::Included(end),
}
})
.unwrap_or(Bound::Unbounded);
return Some((start, end));
}
_ => {}
}
Expand Down
Loading

0 comments on commit 18eaa49

Please sign in to comment.