Skip to content

Commit 2f996f7

Browse files
committed
fix clippy lints
1 parent bcac236 commit 2f996f7

File tree

20 files changed

+669
-179
lines changed

20 files changed

+669
-179
lines changed

cmd/src/command.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ pub async fn test(
150150
let records = context.records();
151151
records.iter().for_each(|record| println!("{record}"));
152152
if let Some(ref mut writer) = writer {
153-
writer.write(&records, &name, task as u32, u32::default()).await
153+
writer
154+
.write(&records, &name, task as u32, u32::default())
155+
.await
154156
}
155157
});
156158
}
@@ -177,7 +179,9 @@ pub async fn test(
177179

178180
async fn read_text(path: PathBuf) -> String {
179181
let mut text = Vec::new();
180-
read_bytes(path, &mut text).await.expect("Could not read source file");
182+
read_bytes(path, &mut text)
183+
.await
184+
.expect("Could not read source file");
181185
String::from_utf8(text).expect("Could not decode source file")
182186
}
183187

cmd/tests/command.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ async fn test_command_repl() -> Result<(), Box<dyn std::error::Error>> {
1313
command.stdin(Stdio::piped());
1414
let mut child = command.spawn().expect("Failed to spawn child process");
1515
if let Some(mut stdin) = child.stdin.take() {
16-
stdin.write_all("let x = 1 + 1; println(\"{x}\");\n".as_bytes()).await?;
16+
stdin
17+
.write_all("let x = 1 + 1; println(\"{x}\");\n".as_bytes())
18+
.await?;
1719
stdin.write_all("exit".as_bytes()).await?;
1820
}
1921
let output = child.wait_with_output().await?;
@@ -41,7 +43,11 @@ async fn test_command_eval() -> Result<(), Box<dyn std::error::Error>> {
4143
assert!(output.status.success());
4244
assert_eq!(String::from_utf8(output.stdout)?, "2null\n");
4345
let mut command = new_command();
44-
let output = command.arg("eval").arg(r#""🍀 Hello Basjoofan!""#).output().await?;
46+
let output = command
47+
.arg("eval")
48+
.arg(r#""🍀 Hello Basjoofan!""#)
49+
.output()
50+
.await?;
4551
assert!(output.status.success());
4652
assert_eq!(String::from_utf8(output.stdout)?, "🍀 Hello Basjoofan!\n");
4753
Ok(())
@@ -50,7 +56,9 @@ async fn test_command_eval() -> Result<(), Box<dyn std::error::Error>> {
5056
#[tokio::test]
5157
async fn test_command_test() -> Result<(), Box<dyn std::error::Error>> {
5258
let router = Router::new().route("/hello", get(|| async { "Hello, World!" }));
53-
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 8888)).await.unwrap();
59+
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 8888))
60+
.await
61+
.unwrap();
5462
tokio::spawn(async move {
5563
axum::serve(listener, router).await.unwrap();
5664
});

lib/src/evaluator.rs

Lines changed: 150 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,25 @@ impl Source {
1919
Expr::String(string) => self.eval_string_literal(string),
2020
Expr::Array(items) => Box::pin(self.eval_array_literal(items, context)).await,
2121
Expr::Map(pairs) => Box::pin(self.eval_map_literal(pairs, context)).await,
22-
Expr::Index(value, index) => Box::pin(self.eval_index_expr(value, index, context)).await,
22+
Expr::Index(value, index) => {
23+
Box::pin(self.eval_index_expr(value, index, context)).await
24+
}
2325
Expr::Field(map, field) => Box::pin(self.eval_field_expr(map, field, context)).await,
2426
Expr::Ident(ident) => self.eval_ident_expr(ident, context),
2527
Expr::Let(name, expr) => Box::pin(self.eval_let_expr(name, expr, context)).await,
26-
Expr::Unary(token, right) => Box::pin(self.eval_unary_expr(token, right, context)).await,
27-
Expr::Binary(token, left, right) => Box::pin(self.eval_binary_expr(token, left, right, context)).await,
28+
Expr::Unary(token, right) => {
29+
Box::pin(self.eval_unary_expr(token, right, context)).await
30+
}
31+
Expr::Binary(token, left, right) => {
32+
Box::pin(self.eval_binary_expr(token, left, right, context)).await
33+
}
2834
Expr::Paren(expr) => Box::pin(self.eval_expr(expr, context)).await,
2935
Expr::If(condition, consequence, alternative) => {
3036
Box::pin(self.eval_if_expr(condition, consequence, alternative, context)).await
3137
}
32-
Expr::Call(name, arguments) => Box::pin(self.eval_call_expr(name, arguments, context)).await,
38+
Expr::Call(name, arguments) => {
39+
Box::pin(self.eval_call_expr(name, arguments, context)).await
40+
}
3341
Expr::Send(name) => Box::pin(self.eval_send_expr(name, context)).await,
3442
}
3543
}
@@ -50,11 +58,19 @@ impl Source {
5058
Ok(Value::String(string.to_owned()))
5159
}
5260

53-
async fn eval_array_literal(&self, items: &[Expr], context: &mut Context) -> Result<Value, String> {
61+
async fn eval_array_literal(
62+
&self,
63+
items: &[Expr],
64+
context: &mut Context,
65+
) -> Result<Value, String> {
5466
Ok(Value::Array(self.eval_list(items, context).await?))
5567
}
5668

57-
async fn eval_map_literal(&self, pairs: &Vec<(Expr, Expr)>, context: &mut Context) -> Result<Value, String> {
69+
async fn eval_map_literal(
70+
&self,
71+
pairs: &Vec<(Expr, Expr)>,
72+
context: &mut Context,
73+
) -> Result<Value, String> {
5874
let mut map = HashMap::new();
5975
for (key, value) in pairs {
6076
let key = self.eval_expr(key, context).await?;
@@ -64,7 +80,12 @@ impl Source {
6480
Ok(Value::Map(map))
6581
}
6682

67-
async fn eval_index_expr(&self, value: &Expr, index: &Expr, context: &mut Context) -> Result<Value, String> {
83+
async fn eval_index_expr(
84+
&self,
85+
value: &Expr,
86+
index: &Expr,
87+
context: &mut Context,
88+
) -> Result<Value, String> {
6889
// TODO enhance indent expr get variable use reference
6990
let value = self.eval_expr(value, context).await?;
7091
let index = self.eval_expr(index, context).await?;
@@ -89,7 +110,12 @@ impl Source {
89110
}
90111
}
91112

92-
async fn eval_field_expr(&self, map: &Expr, field: &String, context: &mut Context) -> Result<Value, String> {
113+
async fn eval_field_expr(
114+
&self,
115+
map: &Expr,
116+
field: &String,
117+
context: &mut Context,
118+
) -> Result<Value, String> {
93119
// TODO enhance indent expr get variable use reference
94120
match self.eval_expr(map, context).await? {
95121
Value::Map(mut pairs) => {
@@ -110,16 +136,28 @@ impl Source {
110136
}
111137
}
112138

113-
async fn eval_let_expr(&self, name: &String, expr: &Expr, context: &mut Context) -> Result<Value, String> {
139+
async fn eval_let_expr(
140+
&self,
141+
name: &String,
142+
expr: &Expr,
143+
context: &mut Context,
144+
) -> Result<Value, String> {
114145
let value = self.eval_expr(expr, context).await?;
115146
context.set(name.to_owned(), value.to_owned());
116147
Ok(value)
117148
}
118149

119-
async fn eval_unary_expr(&self, token: &Token, right: &Expr, context: &mut Context) -> Result<Value, String> {
150+
async fn eval_unary_expr(
151+
&self,
152+
token: &Token,
153+
right: &Expr,
154+
context: &mut Context,
155+
) -> Result<Value, String> {
120156
let right = self.eval_expr(right, context).await?;
121157
match (token.kind, right) {
122-
(Kind::Not, Value::Boolean(false)) | (Kind::Not, Value::Null) => Ok(Value::Boolean(true)),
158+
(Kind::Not, Value::Boolean(false)) | (Kind::Not, Value::Null) => {
159+
Ok(Value::Boolean(true))
160+
}
123161
(Kind::Not, Value::Integer(integer)) => Ok(Value::Integer(!integer)),
124162
(Kind::Not, _) => Ok(Value::Boolean(false)),
125163
(Kind::Sub, Value::Integer(integer)) => Ok(Value::Integer(-integer)),
@@ -128,18 +166,44 @@ impl Source {
128166
}
129167
}
130168

131-
async fn eval_binary_expr(&self, token: &Token, left: &Expr, right: &Expr, context: &mut Context) -> Result<Value, String> {
169+
async fn eval_binary_expr(
170+
&self,
171+
token: &Token,
172+
left: &Expr,
173+
right: &Expr,
174+
context: &mut Context,
175+
) -> Result<Value, String> {
132176
match token.kind {
133-
Kind::Add => self.eval_expr(left, context).await? + self.eval_expr(right, context).await?,
134-
Kind::Sub => self.eval_expr(left, context).await? - self.eval_expr(right, context).await?,
135-
Kind::Mul => self.eval_expr(left, context).await? * self.eval_expr(right, context).await?,
136-
Kind::Div => self.eval_expr(left, context).await? / self.eval_expr(right, context).await?,
137-
Kind::Rem => self.eval_expr(left, context).await? % self.eval_expr(right, context).await?,
138-
Kind::Bx => self.eval_expr(left, context).await? ^ self.eval_expr(right, context).await?,
139-
Kind::Bo => self.eval_expr(left, context).await? | self.eval_expr(right, context).await?,
140-
Kind::Ba => self.eval_expr(left, context).await? & self.eval_expr(right, context).await?,
141-
Kind::Sl => self.eval_expr(left, context).await? << self.eval_expr(right, context).await?,
142-
Kind::Sr => self.eval_expr(left, context).await? >> self.eval_expr(right, context).await?,
177+
Kind::Add => {
178+
self.eval_expr(left, context).await? + self.eval_expr(right, context).await?
179+
}
180+
Kind::Sub => {
181+
self.eval_expr(left, context).await? - self.eval_expr(right, context).await?
182+
}
183+
Kind::Mul => {
184+
self.eval_expr(left, context).await? * self.eval_expr(right, context).await?
185+
}
186+
Kind::Div => {
187+
self.eval_expr(left, context).await? / self.eval_expr(right, context).await?
188+
}
189+
Kind::Rem => {
190+
self.eval_expr(left, context).await? % self.eval_expr(right, context).await?
191+
}
192+
Kind::Bx => {
193+
self.eval_expr(left, context).await? ^ self.eval_expr(right, context).await?
194+
}
195+
Kind::Bo => {
196+
self.eval_expr(left, context).await? | self.eval_expr(right, context).await?
197+
}
198+
Kind::Ba => {
199+
self.eval_expr(left, context).await? & self.eval_expr(right, context).await?
200+
}
201+
Kind::Sl => {
202+
self.eval_expr(left, context).await? << self.eval_expr(right, context).await?
203+
}
204+
Kind::Sr => {
205+
self.eval_expr(left, context).await? >> self.eval_expr(right, context).await?
206+
}
143207
Kind::Lo => match self.eval_expr(left, context).await? {
144208
Value::Boolean(false) | Value::Null => self.eval_expr(right, context).await,
145209
left => Ok(left),
@@ -184,7 +248,12 @@ impl Source {
184248
}
185249
}
186250

187-
async fn eval_call_expr(&self, name: &str, arguments: &[Expr], context: &mut Context) -> Result<Value, String> {
251+
async fn eval_call_expr(
252+
&self,
253+
name: &str,
254+
arguments: &[Expr],
255+
context: &mut Context,
256+
) -> Result<Value, String> {
188257
let arguments = self.eval_list(arguments, context).await?;
189258
match self.function(name) {
190259
Some((params, body)) => {
@@ -223,8 +292,14 @@ impl Source {
223292
for assert in exprs {
224293
if let Expr::Binary(token, left, right) = assert {
225294
let expr = format!("{left} {token} {right}");
226-
let left = self.eval_expr(left, &mut local).await.unwrap_or(Value::Null);
227-
let right = self.eval_expr(right, &mut local).await.unwrap_or(Value::Null);
295+
let left = self
296+
.eval_expr(left, &mut local)
297+
.await
298+
.unwrap_or(Value::Null);
299+
let right = self
300+
.eval_expr(right, &mut local)
301+
.await
302+
.unwrap_or(Value::Null);
228303
if let Some(result) = match token.kind {
229304
Kind::Lt => Some(left < right),
230305
Kind::Gt => Some(left > right),
@@ -345,14 +420,20 @@ mod tests {
345420
("5.0 / 2.0 * 2.0 + 1.0 - 0.5", Value::Float(5.5)),
346421
("5.0 * (0.2 + 1.0)", Value::Float(6.0)),
347422
("0.5 + 0.5 + 0.5 + 0.5 - 1.0", Value::Float(1.0)),
348-
("0.2 * 0.2 * 0.2 * 0.2 * 0.2", Value::Float(0.00032000000000000013)),
423+
(
424+
"0.2 * 0.2 * 0.2 * 0.2 * 0.2",
425+
Value::Float(0.00032000000000000013),
426+
),
349427
("0.5 * 2.2 + 1.1", Value::Float(2.2)),
350428
("0.5 + 0.2 * 10.0", Value::Float(2.5)),
351429
("0.5 * (2.0 + 10.0)", Value::Float(6.0)),
352430
("-0.5", Value::Float(-0.5)),
353431
("-1.0", Value::Float(-1.0)),
354432
("-5.0 + 10.0 + -5.0", Value::Float(0.0)),
355-
("(0.5 + 1.5 * 0.2 + 1.5 / 3.0) * 2.0 + -1.0", Value::Float(1.6)),
433+
(
434+
"(0.5 + 1.5 * 0.2 + 1.5 / 3.0) * 2.0 + -1.0",
435+
Value::Float(1.6),
436+
),
356437
];
357438
run_eval_tests(tests).await;
358439
}
@@ -395,9 +476,18 @@ mod tests {
395476
#[tokio::test]
396477
async fn test_string_literal() {
397478
let tests = vec![
398-
(r#""hello world""#, Value::String(String::from("hello world"))),
399-
(r#""hello" + " world""#, Value::String(String::from("hello world"))),
400-
(r#""hello"+" world"+"!""#, Value::String(String::from("hello world!"))),
479+
(
480+
r#""hello world""#,
481+
Value::String(String::from("hello world")),
482+
),
483+
(
484+
r#""hello" + " world""#,
485+
Value::String(String::from("hello world")),
486+
),
487+
(
488+
r#""hello"+" world"+"!""#,
489+
Value::String(String::from("hello world!")),
490+
),
401491
];
402492
run_eval_tests(tests).await;
403493
}
@@ -439,11 +529,19 @@ mod tests {
439529
("[]", Value::Array(vec![])),
440530
(
441531
"[1, 2, 3]",
442-
Value::Array(vec![Value::Integer(1), Value::Integer(2), Value::Integer(3)]),
532+
Value::Array(vec![
533+
Value::Integer(1),
534+
Value::Integer(2),
535+
Value::Integer(3),
536+
]),
443537
),
444538
(
445539
"[1 + 2, 3 - 4, 5 * 6]",
446-
Value::Array(vec![Value::Integer(3), Value::Integer(-1), Value::Integer(30)]),
540+
Value::Array(vec![
541+
Value::Integer(3),
542+
Value::Integer(-1),
543+
Value::Integer(30),
544+
]),
447545
),
448546
];
449547
run_eval_tests(tests).await;
@@ -499,10 +597,16 @@ mod tests {
499597
let tests = vec![
500598
("let one = 1; one", Value::Integer(1)),
501599
("let one = 1; let two = 2; one + two", Value::Integer(3)),
502-
("let one = 1; let two = one + one; one + two", Value::Integer(3)),
600+
(
601+
"let one = 1; let two = one + one; one + two",
602+
Value::Integer(3),
603+
),
503604
("let one = 1; one;", Value::Integer(1)),
504605
("let one = 1; let two = 2; one + two;", Value::Integer(3)),
505-
("let one = 1; let two = one + one; one + two;", Value::Integer(3)),
606+
(
607+
"let one = 1; let two = one + one; one + two;",
608+
Value::Integer(3),
609+
),
506610
("let one = 1;let one = 2;", Value::Integer(2)),
507611
("let one = 1;let one = 2;one", Value::Integer(2)),
508612
("let one = 1;let two = 2;let one = 3;one", Value::Integer(3)),
@@ -522,7 +626,10 @@ mod tests {
522626
("if (1 > 2) { 10 } else { 20 }", Value::Integer(20)),
523627
("if (1 > 2) { 10 }", Value::Null),
524628
("if (false) { 10 }", Value::Null),
525-
("if ((if (false) { 10 })) { 10 } else { 20 }", Value::Integer(20)),
629+
(
630+
"if ((if (false) { 10 })) { 10 } else { 20 }",
631+
Value::Integer(20),
632+
),
526633
("if (true) {} else { 10 }", Value::Null),
527634
("if (true) { 1; 2 } else { 3 }", Value::Integer(2)),
528635
];
@@ -548,8 +655,14 @@ mod tests {
548655
// ("fn identity(x) { return x; }; identity(5);", Value::Integer(5)),
549656
("fn double(x) { x * 2; }; double(5);", Value::Integer(10)),
550657
("fn add(x, y) { x + y; }; add(5, 5);", Value::Integer(10)),
551-
("fn add(x, y) { x + y; }; add(5 + 5, add(5, 5));", Value::Integer(20)),
552-
("fn one(x) { x + 1; } fn two(x) { one(one(x)); }; two(0);", Value::Integer(2)),
658+
(
659+
"fn add(x, y) { x + y; }; add(5 + 5, add(5, 5));",
660+
Value::Integer(20),
661+
),
662+
(
663+
"fn one(x) { x + 1; } fn two(x) { one(one(x)); }; two(0);",
664+
Value::Integer(2),
665+
),
553666
("fn x(x) { x; }; x(5)", Value::Integer(5)),
554667
("fn len(x) { x; }; len(10)", Value::Integer(10)),
555668
];

lib/src/http/header.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ pub struct Header {
1515

1616
impl Headers {
1717
pub fn insert(&mut self, name: String, value: String) {
18-
self.indices.entry(name.to_lowercase()).or_default().push(self.entries.len());
18+
self.indices
19+
.entry(name.to_lowercase())
20+
.or_default()
21+
.push(self.entries.len());
1922
self.entries.push(Header { name, value });
2023
}
2124

2225
pub fn replace(&mut self, name: &str, value: String) {
23-
if let Some(indices) = self.indices.get(name.to_lowercase().as_str()) {
24-
if let Some(index) = indices.first() {
25-
if let Some(header) = self.entries.get_mut(*index) {
26-
header.value = value;
27-
}
28-
}
26+
if let Some(indices) = self.indices.get(name.to_lowercase().as_str())
27+
&& let Some(index) = indices.first()
28+
&& let Some(header) = self.entries.get_mut(*index)
29+
{
30+
header.value = value;
2931
}
3032
}
3133

0 commit comments

Comments
 (0)