nice error message

This commit is contained in:
Paul Campbell 2023-03-22 07:35:35 +00:00
parent 9b6470bf06
commit a31d0f769a
2 changed files with 24 additions and 15 deletions

View file

@ -1,7 +1,6 @@
use clap::Parser; use clap::Parser;
use std::fmt::Debug; use std::fmt::{Debug, Formatter};
use std::fs; use std::fs;
use std::io;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@ -27,7 +26,17 @@ pub struct Cli {
ignore_extras: bool, ignore_extras: bool,
} }
pub fn skip<F>(cli: &Cli, out: F) -> io::Result<()> pub struct SkipError(String);
impl Debug for SkipError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::result::Result<(), std::fmt::Error> {
write!(f, "{}", self.0)
}
}
pub type Result<T> = core::result::Result<T, SkipError>;
pub fn skip<F>(cli: &Cli, out: F) -> Result<()>
where where
F: FnMut(String) -> (), F: FnMut(String) -> (),
{ {
@ -41,11 +50,11 @@ where
skip_file_lines(cli, out, file) skip_file_lines(cli, out, file)
} }
} }
None => todo!("reading from stdin"), None => Err(SkipError("Not yet implemented - reading from stdin".into())),
} }
} }
fn skip_file_lines<F>(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()> fn skip_file_lines<F>(cli: &Cli, mut out: F, file: &PathBuf) -> Result<()>
where where
F: FnMut(String) -> (), F: FnMut(String) -> (),
{ {
@ -60,7 +69,7 @@ where
Ok(()) Ok(())
} }
fn skip_file_lines_matching<F>(cli: &Cli, mut out: F, file: &PathBuf, line: &str) -> io::Result<()> fn skip_file_lines_matching<F>(cli: &Cli, mut out: F, file: &PathBuf, line: &str) -> Result<()>
where where
F: FnMut(String) -> (), F: FnMut(String) -> (),
{ {
@ -77,7 +86,7 @@ where
Ok(()) Ok(())
} }
fn skip_file_tokens<F>(cli: &Cli, mut out: F, file: &PathBuf, token: &str) -> io::Result<()> fn skip_file_tokens<F>(cli: &Cli, mut out: F, file: &PathBuf, token: &str) -> Result<()>
where where
F: FnMut(String) -> (), F: FnMut(String) -> (),
{ {
@ -105,7 +114,7 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn skip_one_line() -> io::Result<()> { fn skip_one_line() -> Result<()> {
//given //given
let cli = Cli { let cli = Cli {
lines: 1, lines: 1,
@ -125,7 +134,7 @@ mod tests {
} }
#[test] #[test]
fn skip_two_lines() -> io::Result<()> { fn skip_two_lines() -> Result<()> {
//given //given
let cli = Cli { let cli = Cli {
lines: 2, lines: 2,
@ -145,7 +154,7 @@ mod tests {
} }
#[test] #[test]
fn skip_two_matching_lines() -> io::Result<()> { fn skip_two_matching_lines() -> Result<()> {
//given //given
let cli = Cli { let cli = Cli {
lines: 2, lines: 2,
@ -165,7 +174,7 @@ mod tests {
} }
#[test] #[test]
fn skip_three_matching_tokens() -> io::Result<()> { fn skip_three_matching_tokens() -> Result<()> {
//given //given
let cli = Cli { let cli = Cli {
lines: 3, lines: 3,
@ -192,7 +201,7 @@ mod tests {
} }
#[test] #[test]
fn skip_three_matching_tokens_include_extras() -> io::Result<()> { fn skip_three_matching_tokens_include_extras() -> Result<()> {
//given //given
let cli = Cli { let cli = Cli {
lines: 4, lines: 4,
@ -224,7 +233,7 @@ mod tests {
} }
#[test] #[test]
fn skip_three_matching_tokens_ignore_extras() -> io::Result<()> { fn skip_three_matching_tokens_ignore_extras() -> Result<()> {
//given //given
let cli = Cli { let cli = Cli {
lines: 4, lines: 4,

View file

@ -1,8 +1,8 @@
use clap::Parser; use clap::Parser;
use skip::{skip, Cli}; use skip::{skip, Cli, Result};
use std::io::Write; use std::io::Write;
fn main() -> std::io::Result<()> { fn main() -> Result<()> {
let cli = Cli::parse(); let cli = Cli::parse();
let stdout = std::io::stdout(); let stdout = std::io::stdout();