Skip to content

Commit 134d73c

Browse files
refactor: streamline CLI utilities
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
1 parent d923dce commit 134d73c

File tree

7 files changed

+87
-86
lines changed

7 files changed

+87
-86
lines changed

src/cli/error_handler.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl ExitCode {
5151
}
5252

5353
/// Categories of errors for better organization
54-
#[derive(Debug, Clone, PartialEq)]
54+
#[derive(Debug, Clone, PartialEq, Eq)]
5555
pub enum ErrorCategory {
5656
/// User input related errors
5757
UserInput,
@@ -97,7 +97,7 @@ pub struct ErrorInfo {
9797

9898
impl ErrorInfo {
9999
/// Creates a new ErrorInfo with basic information
100-
pub fn new(category: ErrorCategory, exit_code: i32, message: String) -> Self {
100+
pub const fn new(category: ErrorCategory, exit_code: i32, message: String) -> Self {
101101
Self {
102102
category,
103103
exit_code,
@@ -129,13 +129,13 @@ impl ErrorInfo {
129129
}
130130

131131
/// Sets whether to show help information
132-
pub fn with_help(mut self, show_help: bool) -> Self {
132+
pub const fn with_help(mut self, show_help: bool) -> Self {
133133
self.show_help = show_help;
134134
self
135135
}
136136

137137
/// Sets whether to use stderr for output
138-
pub fn with_stderr(mut self, use_stderr: bool) -> Self {
138+
pub const fn with_stderr(mut self, use_stderr: bool) -> Self {
139139
self.use_stderr = use_stderr;
140140
self
141141
}
@@ -162,7 +162,7 @@ pub struct ErrorHandler {
162162

163163
impl ErrorHandler {
164164
/// Creates a new error handler with default settings
165-
pub fn new() -> Self {
165+
pub const fn new() -> Self {
166166
Self {
167167
use_korean: false, // Use English by default
168168
verbose: false,
@@ -171,7 +171,7 @@ impl ErrorHandler {
171171
}
172172

173173
/// Creates a new error handler with custom settings
174-
pub fn with_settings(use_korean: bool, verbose: bool, use_colors: bool) -> Self {
174+
pub const fn with_settings(use_korean: bool, verbose: bool, use_colors: bool) -> Self {
175175
Self {
176176
use_korean,
177177
verbose,
@@ -654,13 +654,13 @@ impl ErrorHandler {
654654
// Print help information if requested
655655
if error_info.show_help {
656656
let _ = writeln!(stderr);
657-
if self.use_korean {
658-
let _ = writeln!(stderr, "To see help, run the following command:");
659-
let _ = writeln!(stderr, " libdplyr --help");
657+
let help_intro = if self.use_korean {
658+
"To see help, run the following command:"
660659
} else {
661-
let _ = writeln!(stderr, "For help, run:");
662-
let _ = writeln!(stderr, " libdplyr --help");
663-
}
660+
"For help, run:"
661+
};
662+
let _ = writeln!(stderr, "{help_intro}");
663+
let _ = writeln!(stderr, " libdplyr --help");
664664
}
665665

666666
let _ = stderr.flush();

src/cli/json_output.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,23 +128,23 @@ pub struct JsonOutputFormatter {
128128

129129
impl JsonOutputFormatter {
130130
/// Creates a new JSON output formatter
131-
pub fn new() -> Self {
131+
pub const fn new() -> Self {
132132
Self {
133133
pretty_print: false,
134134
include_debug: false,
135135
}
136136
}
137137

138138
/// Creates a new JSON output formatter with pretty printing
139-
pub fn pretty() -> Self {
139+
pub const fn pretty() -> Self {
140140
Self {
141141
pretty_print: true,
142142
include_debug: false,
143143
}
144144
}
145145

146146
/// Creates a new JSON output formatter with debug information
147-
pub fn with_debug() -> Self {
147+
pub const fn with_debug() -> Self {
148148
Self {
149149
pretty_print: false,
150150
include_debug: true,
@@ -283,17 +283,15 @@ impl JsonOutputFormatter {
283283
output: &JsonOutput,
284284
estimated_size: usize,
285285
) -> JsonResult<String> {
286+
let mut buf = String::with_capacity(estimated_size);
286287
if self.pretty_print {
287-
let mut buf = String::with_capacity(estimated_size);
288288
let pretty_json = serde_json::to_string_pretty(output)?;
289289
buf.push_str(&pretty_json);
290-
Ok(buf)
291290
} else {
292-
let mut buf = String::with_capacity(estimated_size);
293291
let json = serde_json::to_string(output)?;
294292
buf.push_str(&json);
295-
Ok(buf)
296293
}
294+
Ok(buf)
297295
}
298296
}
299297

@@ -338,7 +336,7 @@ impl MetadataBuilder {
338336
}
339337

340338
/// Sets processing statistics
341-
pub fn with_stats(mut self, stats: ProcessingStats) -> Self {
339+
pub const fn with_stats(mut self, stats: ProcessingStats) -> Self {
342340
self.stats = stats;
343341
self
344342
}
@@ -373,7 +371,7 @@ impl MetadataBuilder {
373371
/// Helper functions for creating common metadata components
374372
impl ProcessingStats {
375373
/// Creates empty processing stats
376-
pub fn empty() -> Self {
374+
pub const fn empty() -> Self {
377375
Self {
378376
lex_time_us: 0,
379377
parse_time_us: 0,
@@ -387,7 +385,11 @@ impl ProcessingStats {
387385
}
388386

389387
/// Creates processing stats with timing information
390-
pub fn with_timing(lex_time_us: u64, parse_time_us: u64, generation_time_us: u64) -> Self {
388+
pub const fn with_timing(
389+
lex_time_us: u64,
390+
parse_time_us: u64,
391+
generation_time_us: u64,
392+
) -> Self {
391393
Self {
392394
lex_time_us,
393395
parse_time_us,

src/cli/output_formatter.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum FormatError {
1919
}
2020

2121
/// Output format types
22-
#[derive(Debug, Clone, PartialEq)]
22+
#[derive(Debug, Clone, PartialEq, Eq)]
2323
pub enum OutputFormat {
2424
/// Default format - basic processing
2525
Default,
@@ -36,11 +36,11 @@ pub enum OutputFormat {
3636
impl fmt::Display for OutputFormat {
3737
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3838
match self {
39-
OutputFormat::Default => write!(f, "default"),
40-
OutputFormat::Basic => write!(f, "basic"),
41-
OutputFormat::Pretty => write!(f, "pretty"),
42-
OutputFormat::Compact => write!(f, "compact"),
43-
OutputFormat::Json => write!(f, "json"),
39+
Self::Default => write!(f, "default"),
40+
Self::Basic => write!(f, "basic"),
41+
Self::Pretty => write!(f, "pretty"),
42+
Self::Compact => write!(f, "compact"),
43+
Self::Json => write!(f, "json"),
4444
}
4545
}
4646
}
@@ -94,7 +94,7 @@ impl OutputFormatter {
9494
}
9595

9696
/// Creates a new OutputFormatter with custom configuration
97-
pub fn with_config(config: FormatConfig) -> Self {
97+
pub const fn with_config(config: FormatConfig) -> Self {
9898
Self { config }
9999
}
100100

@@ -259,7 +259,7 @@ impl OutputFormatter {
259259
}
260260

261261
/// Gets the current format configuration
262-
pub fn config(&self) -> &FormatConfig {
262+
pub const fn config(&self) -> &FormatConfig {
263263
&self.config
264264
}
265265

@@ -269,7 +269,7 @@ impl OutputFormatter {
269269
}
270270

271271
/// Updates just the output format
272-
pub fn set_format(&mut self, format: OutputFormat) {
272+
pub const fn set_format(&mut self, format: OutputFormat) {
273273
self.config.format = format;
274274
}
275275
}

src/cli/pipeline.rs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct CliArgs {
3232
}
3333

3434
/// Supported SQL dialect types
35-
#[derive(Debug, Clone, PartialEq)]
35+
#[derive(Debug, Clone, PartialEq, Eq)]
3636
pub enum SqlDialectType {
3737
PostgreSql,
3838
MySql,
@@ -43,10 +43,10 @@ pub enum SqlDialectType {
4343
impl std::fmt::Display for SqlDialectType {
4444
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4545
match self {
46-
SqlDialectType::PostgreSql => write!(f, "postgresql"),
47-
SqlDialectType::MySql => write!(f, "mysql"),
48-
SqlDialectType::Sqlite => write!(f, "sqlite"),
49-
SqlDialectType::DuckDb => write!(f, "duckdb"),
46+
Self::PostgreSql => write!(f, "postgresql"),
47+
Self::MySql => write!(f, "mysql"),
48+
Self::Sqlite => write!(f, "sqlite"),
49+
Self::DuckDb => write!(f, "duckdb"),
5050
}
5151
}
5252
}
@@ -56,10 +56,10 @@ impl std::str::FromStr for SqlDialectType {
5656

5757
fn from_str(s: &str) -> Result<Self, Self::Err> {
5858
match s.to_lowercase().as_str() {
59-
"postgresql" | "postgres" | "pg" => Ok(SqlDialectType::PostgreSql),
60-
"mysql" => Ok(SqlDialectType::MySql),
61-
"sqlite" => Ok(SqlDialectType::Sqlite),
62-
"duckdb" | "duck" => Ok(SqlDialectType::DuckDb),
59+
"postgresql" | "postgres" | "pg" => Ok(Self::PostgreSql),
60+
"mysql" => Ok(Self::MySql),
61+
"sqlite" => Ok(Self::Sqlite),
62+
"duckdb" | "duck" => Ok(Self::DuckDb),
6363
_ => Err(format!("Unsupported SQL dialect: {s}")),
6464
}
6565
}
@@ -200,7 +200,7 @@ fn create_dialect(dialect_type: &SqlDialectType) -> Box<dyn SqlDialect> {
200200
}
201201

202202
/// CLI operation modes
203-
#[derive(Debug, Clone, PartialEq)]
203+
#[derive(Debug, Clone, PartialEq, Eq)]
204204
pub enum CliMode {
205205
/// File-based processing mode
206206
FileMode {
@@ -248,26 +248,28 @@ impl CliConfig {
248248

249249
/// Determine the CLI mode based on arguments
250250
fn determine_mode(args: &CliArgs) -> CliMode {
251-
if let Some(ref input_text) = args.input_text {
252-
CliMode::TextMode {
251+
args.input_text.as_ref().map_or_else(
252+
|| {
253+
args.input_file.as_ref().map_or(
254+
CliMode::StdinMode {
255+
validate_only: args.validate_only,
256+
streaming: false, // Future extension
257+
},
258+
|input_file| CliMode::FileMode {
259+
input_file: input_file.clone(),
260+
output_file: args.output_file.clone(),
261+
},
262+
)
263+
},
264+
|input_text| CliMode::TextMode {
253265
input_text: input_text.clone(),
254266
output_file: args.output_file.clone(),
255-
}
256-
} else if let Some(ref input_file) = args.input_file {
257-
CliMode::FileMode {
258-
input_file: input_file.clone(),
259-
output_file: args.output_file.clone(),
260-
}
261-
} else {
262-
CliMode::StdinMode {
263-
validate_only: args.validate_only,
264-
streaming: false, // Future extension
265-
}
266-
}
267+
},
268+
)
267269
}
268270

269271
/// Determine output format based on arguments
270-
fn determine_output_format(args: &CliArgs) -> OutputFormat {
272+
const fn determine_output_format(args: &CliArgs) -> OutputFormat {
271273
if args.json_output {
272274
OutputFormat::Json
273275
} else if args.compact {
@@ -630,24 +632,20 @@ impl ProcessingPipeline {
630632

631633
/// Check if processing should continue (signal handling)
632634
pub fn should_continue(&self) -> bool {
633-
if let Some(ref handler) = self.signal_handler {
634-
!handler.should_shutdown()
635-
} else {
636-
true
637-
}
635+
self.signal_handler
636+
.as_ref()
637+
.is_none_or(|handler| !handler.should_shutdown())
638638
}
639639

640640
/// Check if the output pipe was closed
641641
pub fn pipe_closed(&self) -> bool {
642-
if let Some(ref handler) = self.signal_handler {
643-
handler.pipe_closed()
644-
} else {
645-
false
646-
}
642+
self.signal_handler
643+
.as_ref()
644+
.is_some_and(|handler| handler.pipe_closed())
647645
}
648646

649647
/// Get configuration reference
650-
pub fn config(&self) -> &CliConfig {
648+
pub const fn config(&self) -> &CliConfig {
651649
&self.config
652650
}
653651
}

src/cli/signal_handler.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl Default for SignalHandler {
179179
}
180180

181181
/// Result of waiting for a signal
182-
#[derive(Debug, Clone, PartialEq)]
182+
#[derive(Debug, Clone, PartialEq, Eq)]
183183
pub enum SignalWaitResult {
184184
/// Shutdown signal received (SIGINT/SIGTERM)
185185
Shutdown,
@@ -239,7 +239,7 @@ pub mod utils {
239239
}
240240

241241
/// Check if we're running in a Unix-like environment
242-
pub fn is_unix_like() -> bool {
242+
pub const fn is_unix_like() -> bool {
243243
cfg!(unix)
244244
}
245245

@@ -316,7 +316,7 @@ impl SignalAwareProcessor {
316316
}
317317

318318
/// Get reference to the signal handler
319-
pub fn signal_handler(&self) -> &SignalHandler {
319+
pub const fn signal_handler(&self) -> &SignalHandler {
320320
&self.signal_handler
321321
}
322322
}

src/cli/stdin_reader.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl StdinReader {
7676
}
7777

7878
/// Creates a new StdinReader with custom configuration
79-
pub fn with_config(config: StdinConfig) -> Self {
79+
pub const fn with_config(config: StdinConfig) -> Self {
8080
Self {
8181
config,
8282
signal_handler: None,
@@ -320,12 +320,12 @@ impl StdinReader {
320320
}
321321

322322
/// Gets the current stdin configuration
323-
pub fn config(&self) -> &StdinConfig {
323+
pub const fn config(&self) -> &StdinConfig {
324324
&self.config
325325
}
326326

327327
/// Updates the stdin configuration
328-
pub fn set_config(&mut self, config: StdinConfig) {
328+
pub const fn set_config(&mut self, config: StdinConfig) {
329329
self.config = config;
330330
}
331331
}

0 commit comments

Comments
 (0)