Skip to content

Commit 634ab1e

Browse files
committed
Refactor
1 parent 0987dd8 commit 634ab1e

12 files changed

Lines changed: 74 additions & 76 deletions

File tree

examples/repl.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use reqlang_expr::{
2020
fn main() -> ExprResult<()> {
2121
let crate_version = env!("CARGO_PKG_VERSION");
2222

23-
eprintln!("reqlang-expr v{}", crate_version);
23+
eprintln!("reqlang-expr v{crate_version}");
2424

2525
let args = Args::parse();
2626

@@ -85,25 +85,25 @@ fn main() -> ExprResult<()> {
8585
commands.extend(
8686
var_keys
8787
.iter()
88-
.map(|key| format!(":{}", key))
88+
.map(|key| format!(":{key}"))
8989
.collect::<Vec<String>>(),
9090
);
9191
commands.extend(
9292
prompt_keys
9393
.iter()
94-
.map(|key| format!("?{}", key))
94+
.map(|key| format!("?{key}"))
9595
.collect::<Vec<String>>(),
9696
);
9797
commands.extend(
9898
secret_keys
9999
.iter()
100-
.map(|key| format!("!{}", key))
100+
.map(|key| format!("!{key}"))
101101
.collect::<Vec<String>>(),
102102
);
103103
commands.extend(
104104
client_keys
105105
.iter()
106-
.map(|key| format!("@{}", key))
106+
.map(|key| format!("@{key}"))
107107
.collect::<Vec<String>>(),
108108
);
109109
}
@@ -168,7 +168,7 @@ fn main() -> ExprResult<()> {
168168
}
169169

170170
if VERSION_PATTERN.is_match(&source) {
171-
println!("{}", crate_version);
171+
println!("{crate_version}");
172172
continue;
173173
}
174174

@@ -340,7 +340,7 @@ fn main() -> ExprResult<()> {
340340
break;
341341
}
342342
x => {
343-
println!("Event: {:?}", x);
343+
println!("Event: {x:?}");
344344
}
345345
}
346346
}

src/ast.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,28 +230,28 @@ pub fn add_type_to_expr(expr: &mut Expr, env: &CompileTimeEnv) {
230230
IdentifierKind::Var => {
231231
let index = env.get_var_index(expr_identifier.lookup_name());
232232

233-
if let Some(_) = index {
233+
if index.is_some() {
234234
expr_identifier.2 = Some(Type::String);
235235
}
236236
}
237237
IdentifierKind::Prompt => {
238238
let index = env.get_prompt_index(expr_identifier.lookup_name());
239239

240-
if let Some(_) = index {
240+
if index.is_some() {
241241
expr_identifier.2 = Some(Type::String);
242242
}
243243
}
244244
IdentifierKind::Secret => {
245245
let index = env.get_secret_index(expr_identifier.lookup_name());
246246

247-
if let Some(_) = index {
247+
if index.is_some() {
248248
expr_identifier.2 = Some(Type::String);
249249
}
250250
}
251251
IdentifierKind::Client => {
252252
let index = env.get_client_context_index(expr_identifier.lookup_name());
253253

254-
if let Some(_) = index {
254+
if index.is_some() {
255255
expr_identifier.2 = Some(Type::String);
256256
}
257257
}

src/builtins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ mod value_tests {
618618
return_type: Type::String,
619619
func: example_builtin,
620620
};
621-
assert_eq!("test_builtin(...rest: String) -> String", format!("{}", f))
621+
assert_eq!("test_builtin(...rest: String) -> String", format!("{f}"))
622622
}
623623

624624
#[test]

src/compiler.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ impl CompileTimeEnv {
8888
pub fn get_builtin_index(&self, name: &str) -> Option<(&BuiltinFn, u8)> {
8989
let index = self.builtins.iter().position(|x| x.name == name);
9090

91-
let result = index.map(|i| (self.builtins.get(i).unwrap(), i as u8));
92-
result
91+
92+
index.map(|i| (self.builtins.get(i).unwrap(), i as u8))
9393
}
9494

9595
pub fn get_user_builtin_index(&self, name: &str) -> Option<(&BuiltinFn, u8)> {
9696
let index = self.user_builtins.iter().position(|x| x.name == name);
9797

98-
let result = index.map(|i| (self.user_builtins.get(i).unwrap(), i as u8));
99-
result
98+
99+
index.map(|i| (self.user_builtins.get(i).unwrap(), i as u8))
100100
}
101101

102102
pub fn add_user_builtins(&mut self, builtins: Vec<BuiltinFn<'static>>) {
@@ -122,38 +122,38 @@ impl CompileTimeEnv {
122122
}
123123

124124
pub fn get_var_index(&self, name: &str) -> Option<usize> {
125-
let index = self
125+
126+
127+
self
126128
.vars
127129
.iter()
128-
.position(|context_name| context_name == name);
129-
130-
index
130+
.position(|context_name| context_name == name)
131131
}
132132

133133
pub fn get_prompt(&self, index: usize) -> Option<&String> {
134134
self.prompts.get(index)
135135
}
136136

137137
pub fn get_prompt_index(&self, name: &str) -> Option<usize> {
138-
let index = self
138+
139+
140+
self
139141
.prompts
140142
.iter()
141-
.position(|context_name| context_name == name);
142-
143-
index
143+
.position(|context_name| context_name == name)
144144
}
145145

146146
pub fn get_secret(&self, index: usize) -> Option<&String> {
147147
self.secrets.get(index)
148148
}
149149

150150
pub fn get_secret_index(&self, name: &str) -> Option<usize> {
151-
let index = self
151+
152+
153+
self
152154
.secrets
153155
.iter()
154-
.position(|context_name| context_name == name);
155-
156-
index
156+
.position(|context_name| context_name == name)
157157
}
158158

159159
pub fn get_client_context(&self, index: usize) -> Option<&String> {
@@ -177,8 +177,8 @@ impl CompileTimeEnv {
177177
.iter()
178178
.position(|context_name| context_name == name);
179179

180-
let result = index.map(|i| (self.client_context.get(i).unwrap(), i as u8));
181-
result
180+
181+
index.map(|i| (self.client_context.get(i).unwrap(), i as u8))
182182
}
183183
}
184184

@@ -292,7 +292,7 @@ fn compile_expr(
292292
codes.push(CONSTANT);
293293
codes.push(index as u8);
294294
} else {
295-
constants.push(Value::Number(number.0.clone()));
295+
constants.push(Value::Number(number.0));
296296
let index = constants.len() - 1;
297297
codes.push(CONSTANT);
298298
codes.push(index as u8);
@@ -367,7 +367,7 @@ fn compile_expr(
367367
}
368368
};
369369

370-
if let None = result {
370+
if result.is_none() {
371371
errs.push(identifier_undefined_err);
372372
}
373373
}

src/disassembler.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,38 +114,38 @@ impl<'bytecode, 'env> Disassembler<'bytecode, 'env> {
114114
let value = match lookup_type {
115115
lookup::BUILTIN => {
116116
let value = self.env.get_builtin(constant_idx).unwrap();
117-
&value.name
117+
value.name
118118
}
119119
lookup::USER_BUILTIN => {
120120
let value = self.env.get_user_builtin(constant_idx).unwrap();
121-
&value.name
121+
value.name
122122
}
123123
lookup::VAR => {
124124
let value = self.env.get_var(constant_idx).unwrap();
125125

126126
value.as_str()
127127
}
128128
lookup::PROMPT => {
129-
let value = self.env.get_prompt(constant_idx).unwrap();
129+
130130

131-
value
131+
self.env.get_prompt(constant_idx).unwrap()
132132
}
133133
lookup::SECRET => {
134-
let value = self.env.get_secret(constant_idx).unwrap();
134+
135135

136-
value
136+
self.env.get_secret(constant_idx).unwrap()
137137
}
138138
lookup::CLIENT_CTX => {
139-
let value = self.env.get_client_context(constant_idx).unwrap();
139+
140140

141-
value
141+
self.env.get_client_context(constant_idx).unwrap()
142142
}
143143
lookup::TYPE => {
144144
let value = self.bytecode.types().get(constant_idx).unwrap();
145145

146146
&value.name()
147147
}
148-
_ => panic!("invalid get lookup code: {}", lookup_type),
148+
_ => panic!("invalid get lookup code: {lookup_type}"),
149149
};
150150

151151
let lookup_type_string = match lookup_type {
@@ -156,7 +156,7 @@ impl<'bytecode, 'env> Disassembler<'bytecode, 'env> {
156156
lookup::SECRET => "SECRET",
157157
lookup::CLIENT_CTX => "CLIENT_CTX",
158158
lookup::TYPE => "TYPE",
159-
_ => panic!("invalid get lookup code: {}", lookup_type),
159+
_ => panic!("invalid get lookup code: {lookup_type}"),
160160
};
161161

162162
let name = "GET";

src/errors.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,11 @@ pub enum LexicalError {
4949
impl diagnostics::AsDiagnostic for LexicalError {
5050
fn as_diagnostic(&self, source: &str, span: &Span) -> ExprDiagnostic {
5151
let error_code = "lexical".to_string();
52-
match self {
53-
_ => ExprDiagnostic {
54-
code: error_code,
55-
range: get_range(source, span),
56-
severity: Some(ExprDiagnosisSeverity::ERROR),
57-
message: format!("{self}"),
58-
},
52+
ExprDiagnostic {
53+
code: error_code,
54+
range: get_range(source, span),
55+
severity: Some(ExprDiagnosisSeverity::ERROR),
56+
message: format!("{self}"),
5957
}
6058
}
6159
}
@@ -265,9 +263,8 @@ pub mod diagnostics {
265263
errs.iter()
266264
.map(|(err, span)| {
267265
let a = err.as_diagnostic(source, span);
268-
let b = a.to_diagnostic(span).with_message(a.message.clone());
269266

270-
b
267+
a.to_diagnostic(span).with_message(a.message.clone())
271268
})
272269
.collect()
273270
}
@@ -310,8 +307,8 @@ pub mod diagnostics {
310307
}
311308

312309
impl ExprDiagnosisSeverity {
313-
fn to_severity(&self) -> Severity {
314-
match *self {
310+
fn to_severity(self) -> Severity {
311+
match self {
315312
ExprDiagnosisSeverity::HINT => Severity::Help,
316313
ExprDiagnosisSeverity::INFORMATION => Severity::Note,
317314
ExprDiagnosisSeverity::WARNING => Severity::Warning,

src/lexer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010

1111
/// Parse source code in to a list of [`Token`].
1212
pub fn lex(source: &str) -> Vec<Result<(usize, Token, usize), ExprErrorS>> {
13-
let lexer: Lexer<'_> = Lexer::new(&source);
13+
let lexer: Lexer<'_> = Lexer::new(source);
1414
let tokens: Vec<Result<(usize, Token, usize), ExprErrorS>> = lexer.collect::<Vec<_>>();
1515

1616
tokens

src/types.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,13 @@ impl Type {
9191
format!("Fn({args}) -> {returns}")
9292
}
9393
Type::Bool => "Bool".to_string(),
94-
Type::Type(ty) => format!("{}", ty.name()),
94+
Type::Type(ty) => ty.name().to_string(),
9595
Type::Unknown => "Unknown".to_string(),
9696
}
9797
}
9898

9999
pub fn is_type(&self) -> bool {
100-
match self {
101-
Type::Type(_) => true,
102-
_ => false,
103-
}
100+
matches!(self, Type::Type(_))
104101
}
105102
}
106103

@@ -152,13 +149,13 @@ impl From<BuiltinFn<'static>> for Type {
152149
let args: Vec<Type> = value
153150
.args
154151
.iter()
155-
.filter(|x| x.variadic == false)
152+
.filter(|x| !x.variadic)
156153
.map(|x| x.ty.clone())
157154
.collect();
158155
let varg = value
159156
.args
160157
.iter()
161-
.find(|x| x.variadic == true)
158+
.find(|x| x.variadic)
162159
.map(|x| Box::new(x.ty.clone()));
163160
let returns = value.return_type.clone();
164161

src/value.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ impl Value {
7272
impl Display for Value {
7373
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
7474
match self {
75-
Value::String(string) => write!(f, "`{}`", string),
76-
Value::Number(value) => write!(f, "{}", value),
75+
Value::String(string) => write!(f, "`{string}`"),
76+
Value::Number(value) => write!(f, "{value}"),
7777
Value::Fn(builtin) => write!(f, "{builtin:?}"),
78-
Value::Bool(value) => write!(f, "{}", value),
79-
Value::Type(ty) => write!(f, "Type<{}>", ty),
78+
Value::Bool(value) => write!(f, "{value}"),
79+
Value::Type(ty) => write!(f, "Type<{ty}>"),
8080
}
8181
}
8282
}

0 commit comments

Comments
 (0)