Skip to content

Commit d543275

Browse files
committed
refactor(builtins): migrate base64 from manual arg parsing to ArgParser
Replace manual `while i < ctx.args.len()` index management with ArgParser API in the base64 builtin. Uses flag_any, flag_value, positional, and is_flag for cleaner argument handling. Part of #880
1 parent 3b61c38 commit d543275

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

crates/bashkit/src/builtins/base64.rs

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use async_trait::async_trait;
44
use base64::Engine;
55

6+
use super::arg_parser::ArgParser;
67
use super::{Builtin, Context, read_text_file};
78
use crate::error::Result;
89
use crate::interpreter::ExecResult;
@@ -23,37 +24,27 @@ impl Builtin for Base64 {
2324
let mut wrap = 76usize;
2425
let mut file: Option<String> = None;
2526

26-
let mut p = super::arg_parser::ArgParser::new(ctx.args);
27-
while !p.is_done() {
28-
if p.flag_any(&["-d", "--decode"]) {
27+
let mut parser = ArgParser::new(ctx.args);
28+
while !parser.is_done() {
29+
if parser.flag_any(&["-d", "--decode"]) {
2930
decode = true;
30-
} else if let Some(val) = p.current().and_then(|s| s.strip_prefix("--wrap=")) {
31+
} else if let Some(val) = parser.current().and_then(|s| s.strip_prefix("--wrap=")) {
3132
wrap = val.parse().unwrap_or(76);
32-
p.advance();
33-
} else {
34-
match p.flag_value("-w", "base64") {
35-
Ok(Some(val)) => wrap = val.parse().unwrap_or(76),
36-
Err(_) => {
37-
return Ok(ExecResult::err(
38-
"base64: option requires an argument -- 'w'\n",
39-
1,
40-
));
41-
}
42-
Ok(None) => {
43-
if p.flag_any(&["-i", "--ignore-garbage"]) {
44-
// silently accept
45-
} else if let Some(flag) =
46-
p.current().filter(|s| s.starts_with('-') && s.len() > 1)
47-
{
48-
return Ok(ExecResult::err(
49-
format!("base64: invalid option -- '{}'\n", &flag[1..]),
50-
1,
51-
));
52-
} else if let Some(arg) = p.positional() {
53-
file = Some(arg.to_string());
54-
}
55-
}
33+
parser.advance();
34+
} else if let Some(val) = match parser.flag_value("-w", "base64") {
35+
Ok(v) => v,
36+
Err(e) => return Ok(ExecResult::err(format!("{e}\n"), 1)),
37+
} {
38+
wrap = val.parse().unwrap_or(76);
39+
} else if parser.flag_any(&["-i", "--ignore-garbage"]) {
40+
// silently accept
41+
} else if parser.is_flag() {
42+
if let Some(s) = parser.positional() {
43+
let msg = format!("base64: invalid option -- '{}'\n", &s[1..]);
44+
return Ok(ExecResult::err(msg, 1));
5645
}
46+
} else if let Some(arg) = parser.positional() {
47+
file = Some(arg.to_string());
5748
}
5849
}
5950

@@ -86,10 +77,7 @@ impl Builtin for Base64 {
8677
let output = String::from_utf8_lossy(&bytes).to_string();
8778
Ok(ExecResult::ok(output))
8879
}
89-
Err(e) => Ok(ExecResult::err(
90-
format!("base64: invalid input: {}\n", e),
91-
1,
92-
)),
80+
Err(e) => Ok(ExecResult::err(format!("base64: invalid input: {e}\n"), 1)),
9381
}
9482
} else {
9583
// Encode
@@ -107,7 +95,7 @@ impl Builtin for Base64 {
10795
wrapped.push('\n');
10896
wrapped
10997
} else {
110-
format!("{}\n", encoded)
98+
format!("{encoded}\n")
11199
};
112100
Ok(ExecResult::ok(output))
113101
}

0 commit comments

Comments
 (0)