|
1 | | -use crate::core::{ConstLookup, LintResult}; |
2 | | -use crate::core::{LintError, LintErrorPos}; |
| 1 | +use crate::core::{ConstLookup, LintError}; |
3 | 2 | use rusty_common::Positioned; |
4 | | -use rusty_parser::{Expression, ExpressionPos, TypeQualifier}; |
| 3 | +use rusty_parser::{Expression, TypeQualifier}; |
5 | 4 | use rusty_variant::{Variant, MAX_INTEGER}; |
6 | 5 |
|
7 | | -pub fn validate_string_length( |
8 | | - expr_pos: &ExpressionPos, |
9 | | - lookup: &impl ConstLookup, |
10 | | -) -> Result<u16, LintErrorPos> { |
11 | | - let Positioned { element: expr, pos } = expr_pos; |
12 | | - do_validate_string_length(expr, lookup).with_err_at(pos) |
| 6 | +pub trait ValidateStringLength<E> { |
| 7 | + fn validate_string_length(&self, const_lookup: &impl ConstLookup) -> Result<u16, E>; |
13 | 8 | } |
14 | 9 |
|
15 | | -fn do_validate_string_length( |
16 | | - expr: &Expression, |
17 | | - lookup: &impl ConstLookup, |
18 | | -) -> Result<u16, LintError> { |
19 | | - match expr { |
20 | | - Expression::IntegerLiteral(i) => { |
21 | | - if (1..=MAX_INTEGER).contains(i) { |
22 | | - Ok(*i as u16) |
23 | | - } else { |
24 | | - Err(LintError::InvalidConstant) |
25 | | - } |
26 | | - } |
27 | | - Expression::Variable(name, _) => { |
28 | | - if let Some(qualifier) = name.qualifier() { |
29 | | - if qualifier != TypeQualifier::PercentInteger { |
30 | | - return Err(LintError::InvalidConstant); |
31 | | - } |
32 | | - } |
| 10 | +impl<T, E> ValidateStringLength<Positioned<E>> for Positioned<T> |
| 11 | +where |
| 12 | + T: ValidateStringLength<E>, |
| 13 | +{ |
| 14 | + fn validate_string_length( |
| 15 | + &self, |
| 16 | + const_lookup: &impl ConstLookup, |
| 17 | + ) -> Result<u16, Positioned<E>> { |
| 18 | + let Positioned { element, pos } = self; |
| 19 | + element |
| 20 | + .validate_string_length(const_lookup) |
| 21 | + .map_err(|e| Positioned::new(e, *pos)) |
| 22 | + } |
| 23 | +} |
33 | 24 |
|
34 | | - if let Some(Variant::VInteger(i)) = lookup.get_resolved_constant(name.bare_name()) { |
| 25 | +impl ValidateStringLength<LintError> for Expression { |
| 26 | + fn validate_string_length(&self, const_lookup: &impl ConstLookup) -> Result<u16, LintError> { |
| 27 | + match self { |
| 28 | + Self::IntegerLiteral(i) => { |
35 | 29 | if (1..=MAX_INTEGER).contains(i) { |
36 | | - return Ok(*i as u16); |
| 30 | + Ok(*i as u16) |
| 31 | + } else { |
| 32 | + Err(LintError::InvalidConstant) |
37 | 33 | } |
38 | 34 | } |
| 35 | + Self::Variable(name, _) => { |
| 36 | + if let Some(qualifier) = name.qualifier() { |
| 37 | + if qualifier != TypeQualifier::PercentInteger { |
| 38 | + return Err(LintError::InvalidConstant); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if let Some(Variant::VInteger(i)) = |
| 43 | + const_lookup.get_resolved_constant(name.bare_name()) |
| 44 | + { |
| 45 | + if (1..=MAX_INTEGER).contains(i) { |
| 46 | + return Ok(*i as u16); |
| 47 | + } |
| 48 | + } |
39 | 49 |
|
40 | | - Err(LintError::InvalidConstant) |
| 50 | + Err(LintError::InvalidConstant) |
| 51 | + } |
| 52 | + _ => Err(LintError::InvalidConstant), |
41 | 53 | } |
42 | | - _ => Err(LintError::InvalidConstant), |
43 | 54 | } |
44 | 55 | } |
0 commit comments