Skip to content
Snippets Groups Projects
Commit 6ec53a05 authored by Venceslas's avatar Venceslas
Browse files

Add more convenient error messages

parent 9321087a
No related branches found
No related tags found
No related merge requests found
Pipeline #10884 passed
......@@ -24,3 +24,7 @@ chrono = "0.4"
color = { path="color", features=["serde"] }
toml_edit = "0.19"
# Logging applications
tracing = "0.1"
tracing-subscriber = "0.2.0"
\ No newline at end of file
......@@ -33,6 +33,13 @@ impl From<std::io::Error> for Error {
}
}
impl From<tera::Error> for Error {
#[inline]
fn from(e: tera::Error) -> Self {
Self::Other(Box::new(e))
}
}
impl From<toml_edit::TomlError> for Error {
#[inline]
fn from(e: toml_edit::TomlError) -> Self {
......
......@@ -4,7 +4,7 @@ mod cards;
mod shirt_info;
mod path_reader;
use std::collections::HashSet;
use std::{collections::HashSet, process::ExitCode};
use std::io::Write;
use std::path::PathBuf;
use clap::{Parser, Subcommand};
......@@ -139,17 +139,19 @@ fn into_hashset<'a>(from : &'a [String]) -> HashSet<&'a str> {
ret
}
fn generate_face_book(out: &mut Box<dyn std::io::Write>, data : &cards::Page, args : &GenArgs) -> std::io::Result<()> {
fn generate_face_book(out: &mut Box<dyn std::io::Write>, data : &cards::Page, args : &GenArgs) -> error::Result<()> {
let mut template_path = args.template_path.clone();
template_path.push("**/*.html");
let template = tera::Tera::new(template_path.as_os_str().to_str().unwrap()).unwrap();
let template = tera::Tera::new(template_path.as_os_str().to_str().unwrap())?;
let mut ctx = tera::Context::from_serialize(data).unwrap();
let mut ctx = tera::Context::from_serialize(data)?;
ctx.insert("show_credentials", &args.show_credentials);
let generated = template.render("index.html", &ctx).unwrap();
let generated = template.render("index.html", &ctx)?;
out.write_all(generated.as_bytes())
out.write_all(generated.as_bytes())?;
Ok(())
}
fn generate_shirt_info_table(out: &mut Box<dyn std::io::Write>, data : Vec<ShirtInfo>) {
......@@ -187,13 +189,13 @@ fn array_matrix_to_string(matrix : Vec<Vec<&str>>, total_size : usize) -> String
data
}
fn main() {
fn app() -> error::Result<()> {
let args : Args = Args::parse();
let data = format::load(args.input_file.as_path()).unwrap();
let data = format::load(args.input_file.as_path())?;
let mut out : Box<dyn std::io::Write> = if let Some(p) = &args.output_file {
Box::new(std::fs::File::create(p.as_path()).unwrap())
Box::new(std::fs::File::create(p.as_path())?)
} else {
Box::new(std::io::stdout())
};
......@@ -202,10 +204,10 @@ fn main() {
Commands::Gen(args) => {
let cards = data.generate_cards(into_hashset(args.select_clubs.as_slice()));
generate_face_book(&mut out, &cards, args).unwrap()
generate_face_book(&mut out, &cards, args)?;
},
Commands::GenShirtInfo => {
let mut shirt_info = data.generate_shirt_info().unwrap();
let mut shirt_info = data.generate_shirt_info()?;
shirt_info.sort();
......@@ -215,10 +217,28 @@ fn main() {
if let Some((lines, total_size)) = data.generate_club_list(args) {
let data = array_matrix_to_string(lines, total_size);
out.write_all(data.as_bytes()).unwrap();
out.write_all(data.as_bytes())?;
} else {}
}
}
Ok(())
}
fn main() -> ExitCode {
tracing::subscriber::set_global_default(
tracing_subscriber::FmtSubscriber::builder()
.with_max_level(tracing::Level::DEBUG)
.finish()
).unwrap();
if let Err(e) = app() {
tracing::error!("{}", e);
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
}
#[cfg(test)]
......
......@@ -69,13 +69,11 @@ fn check_extension(path : &Path, ext : &str) -> bool {
fn entry_task(
work : &mut Document,
entry : std::io::Result<DirEntry>,
entry : &DirEntry,
exclude : &HashSet<PathBuf>,
root : &Path,
depth : usize
) -> Result<()> {
let entry = entry?;
let raw_path = entry.path();
let path = match raw_path.strip_prefix(root) {
......@@ -130,7 +128,12 @@ fn read_multiple_files(
let mut work = Document::new();
for entry in std::fs::read_dir(from)? {
let _ = entry_task(&mut work, entry, exclude, root, depth);
match entry {
Ok(entry) => if let Err(e) = entry_task(&mut work, &entry, exclude, root, depth) {
tracing::warn!("`{}` at {}", e, entry.path().display());
},
Err(e) => tracing::warn!("`{}` at {}", e, from.display()),
}
}
Ok(Some(work))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment