Skip to content

Commit be0cac1

Browse files
committed
Converted Signature to an enum
1 parent 333f94d commit be0cac1

3 files changed

Lines changed: 25 additions & 33 deletions

File tree

rusty_linter/src/built_ins/data.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ use rusty_parser::{Expression, ExpressionPos, Expressions};
33

44
use crate::core::{LintError, LintErrorPos, NameScope};
55

6-
pub fn lint(
7-
args: &Expressions,
8-
name_scope: NameScope,
9-
pos: Position,
10-
) -> Result<(), LintErrorPos> {
6+
pub fn lint(args: &Expressions, name_scope: NameScope, pos: Position) -> Result<(), LintErrorPos> {
117
if name_scope == NameScope::Global {
128
args.iter().try_for_each(require_constant)
139
} else {

rusty_linter/src/core/signature.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,28 @@ use crate::core::ResolvedParamTypes;
88
/// The signature of a FUNCTION or SUB.
99
/// Consists of the resolved parameter types and, in case of a FUNCTION, the return type.
1010
#[derive(PartialEq)]
11-
pub struct Signature {
12-
/// The return type of a FUNCTION, or None if this is the signature of a SUB.
13-
q: Option<TypeQualifier>,
11+
pub enum Signature {
12+
/// The signature of a FUNCTION consists of the resolved parameter types
13+
/// and the return type.
14+
Function(ResolvedParamTypes, TypeQualifier),
1415

15-
/// The resolved parameter types.
16-
param_types: ResolvedParamTypes,
16+
/// The signature of a SUB consists of the resolved parameter types.
17+
Sub(ResolvedParamTypes),
1718
}
1819

1920
impl Signature {
20-
pub fn new_sub(param_types: ResolvedParamTypes) -> Self {
21-
Self {
22-
q: None,
23-
param_types,
24-
}
25-
}
26-
27-
pub fn new_function(q: TypeQualifier, param_types: ResolvedParamTypes) -> Self {
28-
Self {
29-
q: Some(q),
30-
param_types,
31-
}
32-
}
33-
3421
pub fn qualifier(&self) -> Option<TypeQualifier> {
35-
self.q.as_ref().copied()
22+
match self {
23+
Self::Function(_, q) => Some(*q),
24+
Self::Sub(_) => None,
25+
}
3626
}
3727

3828
pub fn param_types(&self) -> &ResolvedParamTypes {
39-
&self.param_types
29+
match self {
30+
Self::Function(param_types, _) => param_types,
31+
Self::Sub(param_types) => param_types,
32+
}
4033
}
4134
}
4235

@@ -46,9 +39,12 @@ impl Signature {
4639

4740
impl PartialEq<TypeQualifier> for Signature {
4841
fn eq(&self, other: &TypeQualifier) -> bool {
49-
match &self.q {
50-
Some(q) => q == other,
51-
_ => false,
42+
if let Self::Function(_, q) = self
43+
&& q == other
44+
{
45+
true
46+
} else {
47+
false
5248
}
5349
}
5450
}

rusty_linter/src/pre_linter/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Visitor<FunctionDeclaration> for MainContext {
5555
let param_types: ResolvedParamTypes = self.resolve_parameters(params)?;
5656
let bare_name = name.as_bare_name();
5757
let q = name.qualify(&self.resolver);
58-
let signature = Signature::new_function(q, param_types);
58+
let signature = Signature::Function(param_types, q);
5959
self.functions
6060
.add_declaration(bare_name.clone(), signature.at_pos(self.declaration_pos))
6161
}
@@ -71,7 +71,7 @@ impl Visitor<FunctionImplementation> for MainContext {
7171
let param_types: ResolvedParamTypes = self.resolve_parameters(params)?;
7272
let bare_name = name.as_bare_name();
7373
let q = name.qualify(&self.resolver);
74-
let signature = Signature::new_function(q, param_types);
74+
let signature = Signature::Function(param_types, q);
7575
self.functions
7676
.add_implementation(bare_name.clone(), signature.at_pos(self.declaration_pos))
7777
}
@@ -86,7 +86,7 @@ impl Visitor<SubDeclaration> for MainContext {
8686
parameters: params,
8787
} = s;
8888
let param_types: ResolvedParamTypes = self.resolve_parameters(params)?;
89-
let signature = Signature::new_sub(param_types);
89+
let signature = Signature::Sub(param_types);
9090
self.subs
9191
.add_declaration(bare_name.clone(), signature.at_pos(self.declaration_pos))
9292
}
@@ -102,7 +102,7 @@ impl Visitor<SubImplementation> for MainContext {
102102
..
103103
} = s;
104104
let param_types: ResolvedParamTypes = self.resolve_parameters(params)?;
105-
let signature = Signature::new_sub(param_types);
105+
let signature = Signature::Sub(param_types);
106106
self.subs
107107
.add_implementation(bare_name.clone(), signature.at_pos(self.declaration_pos))
108108
}

0 commit comments

Comments
 (0)