Skip to content

Commit

Permalink
use async io
Browse files Browse the repository at this point in the history
  • Loading branch information
guuzaa committed Sep 9, 2024
1 parent 8386cf9 commit c110810
Show file tree
Hide file tree
Showing 6 changed files with 355 additions and 92 deletions.
229 changes: 229 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ edition = "2021"

[dependencies]
clap = { version = "4.3", features = ["derive"] }
tokio = { version = "1.38.0", features = ["full"] }
22 changes: 11 additions & 11 deletions src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs::File;
use std::io::{self, BufReader};
use clap::{Parser, CommandFactory}; // Add CommandFactory here
use clap::{CommandFactory, Parser};
use tokio::fs::File;
use tokio::io::{self, BufReader};

use crate::input_processor::{process_input, CountOptions};

Expand Down Expand Up @@ -59,23 +59,23 @@ impl Cli {
}
}

pub fn run() -> io::Result<()> {
pub async fn run() -> io::Result<()> {
let (cli, options) = Cli::parse_args();

let mut stdout = io::stdout();
let mut stdout = tokio::io::stdout();

if cli.files.is_empty() {
let stdin = io::stdin();
let mut handle = stdin.lock();
if let Err(err) = process_input(&mut handle, &mut stdout, &options) {
let stdin = tokio::io::stdin();
let mut reader = BufReader::new(stdin);
if let Err(err) = process_input(&mut reader, &mut stdout, &options).await {
eprintln!("Error processing stdin: {}", err);
}
} else {
for filename in cli.files {
match File::open(&filename) {
match File::open(&filename).await {
Ok(file) => {
let mut reader = BufReader::new(file);
if let Err(err) = process_input(&mut reader, &mut stdout, &options) {
if let Err(err) = process_input(&mut reader, &mut stdout, &options).await {
eprintln!("Error processing file '{}': {}", filename, err);
}
}
Expand All @@ -87,4 +87,4 @@ pub fn run() -> io::Result<()> {
}

Ok(())
}
}
Loading

0 comments on commit c110810

Please sign in to comment.