Feat(lib): File I/O,
This commit is contained in:
parent
b8fa6d8e18
commit
d401122b30
21
src/lib.rs
21
src/lib.rs
@ -1,6 +1,8 @@
|
|||||||
use clap::builder::{Arg, Command};
|
use clap::builder::{Arg, Command};
|
||||||
use clap::{crate_authors, crate_description, crate_name, crate_version, ArgAction, ArgMatches};
|
use clap::{crate_authors, crate_description, crate_name, crate_version, ArgAction, ArgMatches};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufRead, BufReader};
|
||||||
|
|
||||||
type MyResult<T> = Result<T, Box<dyn Error>>;
|
type MyResult<T> = Result<T, Box<dyn Error>>;
|
||||||
|
|
||||||
@ -34,13 +36,16 @@ pub fn get_args() -> MyResult<Config> {
|
|||||||
.arg(
|
.arg(
|
||||||
Arg::new("number")
|
Arg::new("number")
|
||||||
.short('n')
|
.short('n')
|
||||||
|
.long("number")
|
||||||
.help("Number lines")
|
.help("Number lines")
|
||||||
.num_args(0)
|
.num_args(0)
|
||||||
.action(ArgAction::SetTrue),
|
.action(ArgAction::SetTrue)
|
||||||
|
.conflicts_with("number_nonblank"),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::new("number_nonblank")
|
Arg::new("number_nonblank")
|
||||||
.short('b')
|
.short('b')
|
||||||
|
.long("number-nonblank")
|
||||||
.help("Number nonblank lines")
|
.help("Number nonblank lines")
|
||||||
.num_args(0)
|
.num_args(0)
|
||||||
.action(ArgAction::SetTrue),
|
.action(ArgAction::SetTrue),
|
||||||
@ -58,8 +63,20 @@ pub fn get_args() -> MyResult<Config> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn open(filename: &str) -> MyResult<Box<dyn BufRead>> {
|
||||||
|
match filename {
|
||||||
|
"-" => Ok(Box::new(BufReader::new(io::stdin()))),
|
||||||
|
_ => Ok(Box::new(BufReader::new(File::open(filename)?))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run(config: Config) -> MyResult<()> {
|
pub fn run(config: Config) -> MyResult<()> {
|
||||||
dbg!(config);
|
for filepath in config.files {
|
||||||
|
match open(&filepath) {
|
||||||
|
Err(err) => eprintln!("Failed to open {}: {}", filepath, err),
|
||||||
|
Ok(_) => println!("Opened {}", filepath),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user