Skip to content

Commit ba010db

Browse files
committed
refactor: Converted function validate_string_length into a trait
1 parent 20fa338 commit ba010db

4 files changed

Lines changed: 48 additions & 37 deletions

File tree

rusty_linter/src/converter/dim_rules/dim_type_rules.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::converter::common::DimNameState;
22
use crate::converter::common::*;
3-
use crate::core::validate_string_length;
43
use crate::core::IntoTypeQualifier;
4+
use crate::core::ValidateStringLength;
55
use crate::core::{LintError, LintErrorPos};
66
use rusty_common::*;
77
use rusty_parser::*;
@@ -97,7 +97,7 @@ fn fixed_length_string_to_dim_type(
9797
length_expression: &ExpressionPos,
9898
) -> Result<DimType, LintErrorPos> {
9999
require_extended_can_be_defined(ctx, bare_name, length_expression.pos)?;
100-
let string_length: u16 = validate_string_length(length_expression, &ctx.names)?;
100+
let string_length: u16 = length_expression.validate_string_length(&ctx.names)?;
101101
Ok(DimType::fixed_length_string(
102102
string_length,
103103
length_expression.pos(),

rusty_linter/src/converter/dim_rules/redim.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::converter::common::Context;
22
use crate::converter::common::Convertible;
33
use crate::converter::common::DimNameState;
4-
use crate::core::validate_string_length;
54
use crate::core::IntoTypeQualifier;
5+
use crate::core::ValidateStringLength;
66
use crate::core::{LintError, LintErrorPos};
77
use rusty_common::*;
88
use rusty_parser::*;
@@ -188,7 +188,7 @@ fn fixed_length_string_to_dim_type(
188188
array_dimensions: &ArrayDimensions,
189189
length_expression: &ExpressionPos,
190190
) -> Result<DimType, LintErrorPos> {
191-
let string_length: u16 = validate_string_length(length_expression, &ctx.names)?;
191+
let string_length: u16 = length_expression.validate_string_length(&ctx.names)?;
192192
ctx.names
193193
.find_name_or_shared_in_parent(bare_name)
194194
.into_iter()
Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,55 @@
1-
use crate::core::{ConstLookup, LintResult};
2-
use crate::core::{LintError, LintErrorPos};
1+
use crate::core::{ConstLookup, LintError};
32
use rusty_common::Positioned;
4-
use rusty_parser::{Expression, ExpressionPos, TypeQualifier};
3+
use rusty_parser::{Expression, TypeQualifier};
54
use rusty_variant::{Variant, MAX_INTEGER};
65

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>;
138
}
149

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+
}
3324

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) => {
3529
if (1..=MAX_INTEGER).contains(i) {
36-
return Ok(*i as u16);
30+
Ok(*i as u16)
31+
} else {
32+
Err(LintError::InvalidConstant)
3733
}
3834
}
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+
}
3949

40-
Err(LintError::InvalidConstant)
50+
Err(LintError::InvalidConstant)
51+
}
52+
_ => Err(LintError::InvalidConstant),
4153
}
42-
_ => Err(LintError::InvalidConstant),
4354
}
4455
}

rusty_linter/src/pre_linter/user_defined_type_rules.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::core::validate_string_length;
1+
use crate::core::ValidateStringLength;
22
use crate::core::{LintError, LintErrorPos};
33
use crate::pre_linter::ConstantMap;
44
use rusty_common::{AtPos, Position, Positioned};
@@ -39,7 +39,7 @@ pub fn user_defined_type(
3939
ElementType::Single => ElementType::Single,
4040
ElementType::Double => ElementType::Double,
4141
ElementType::FixedLengthString(str_len_expression_pos, _) => {
42-
let l: u16 = validate_string_length(str_len_expression_pos, global_constants)?;
42+
let l: u16 = str_len_expression_pos.validate_string_length(global_constants)?;
4343
ElementType::FixedLengthString(
4444
Expression::IntegerLiteral(l as i32).at(str_len_expression_pos),
4545
l,

0 commit comments

Comments
 (0)