Skip to content

Commit

Permalink
Add basic error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfarias committed Mar 16, 2021
1 parent 63c8665 commit 7e543a0
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 70 deletions.
56 changes: 56 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 @@ -13,5 +13,6 @@ log = "0.4"
pgn-reader = "0.17"
resvg = "0.14"
shakmaty = "0.18"
thiserror = "1.0"
tiny-skia = "0.5.0"
usvg = "0.14"
6 changes: 3 additions & 3 deletions src/bin/c2g.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use c2g::cli::Chess2Gif;
use c2g::{cli::Chess2Gif, error::C2GError};

fn main() {
fn main() -> Result<(), C2GError> {
env_logger::init();

let c2g = Chess2Gif::new();
c2g.run();
c2g.run()
}
40 changes: 30 additions & 10 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::io;
use clap::{App, Arg};
use pgn_reader::BufferedReader;

use crate::error::C2GError;
use crate::giffer::PGNGiffer;

pub struct Chess2Gif<'a> {
Expand All @@ -17,7 +18,7 @@ impl<'a> Chess2Gif<'a> {
Self::new_from(std::env::args_os().into_iter()).unwrap_or_else(|e| e.exit())
}

pub fn new_from<I, T>(args: I) -> Result<Self, clap::Error>
pub fn new_from<I, T>(args: I) -> Result<Self, C2GError>
where
I: Iterator<Item = T>,
T: Into<OsString> + Clone,
Expand Down Expand Up @@ -88,7 +89,11 @@ impl<'a> Chess2Gif<'a> {
let matches = app.get_matches_from_safe(args)?;

let size = u32::from_str_radix(matches.value_of("size").expect("Size must be defined"), 10)
.unwrap();
.expect("Size must be a positive number");

if size % 8 != 0 {
return Err(C2GError::NotDivisibleBy8);
}

let pgn = if matches.value_of("PGN").is_some() {
Some(matches.value_of("PGN").unwrap().to_owned())
Expand All @@ -107,27 +112,42 @@ impl<'a> Chess2Gif<'a> {

let dark: [u8; 4] = clap::values_t_or_exit!(matches, "dark", u8)
.try_into()
.unwrap();
.expect("Invalid dark color");
let light: [u8; 4] = clap::values_t_or_exit!(matches, "light", u8)
.try_into()
.unwrap();
.expect("Invalid light color");

Ok(Chess2Gif {
pgn: pgn,
giffer: PGNGiffer::new(pieces_path, font_path, size, output, 100, dark, light),
giffer: PGNGiffer::new(pieces_path, font_path, size, output, 100, dark, light)?,
})
}

pub fn run(mut self) {
pub fn run(mut self) -> Result<(), C2GError> {
log::info!("Reading PGN");
if let Some(pgn) = self.pgn {
let result = if let Some(pgn) = self.pgn {
let mut reader = BufferedReader::new_cursor(&pgn[..]);
reader.read_game(&mut self.giffer);
match reader.read_game(&mut self.giffer) {
Ok(result) => match result {
// result contains Option<Result<(), C2GError>>
Some(r) => Ok(r.unwrap()),
None => Ok(()),
},
Err(e) => Err(C2GError::ReadGame { source: e }),
}
} else {
let stdin = io::stdin();
let mut reader = BufferedReader::new(stdin);
reader.read_game(&mut self.giffer);
}
match reader.read_game(&mut self.giffer) {
Ok(result) => match result {
// result contains Option<Result<(), C2GError>>
Some(r) => Ok(r.unwrap()),
None => Ok(()),
},
Err(e) => Err(C2GError::ReadGame { source: e }),
}
};
log::info!("Done!");
result
}
}
Loading

0 comments on commit 7e543a0

Please sign in to comment.