Skip to content

Commit

Permalink
Changed some errors to warnigns
Browse files Browse the repository at this point in the history
  • Loading branch information
Winterwolf365 committed Oct 16, 2023
1 parent 4e0002f commit 70f38d6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "skills"
version = "0.2.0"
version = "0.2.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion data/Oefenimport.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ LA = Taal waarin het boek is geschreven (bijv. Nl, Dui, Eng, Spa, Dan, Rus, Fra
037, 20190414, Marta Kowalska, Fan, Wyprawa w Poszukiwaniu Zaginionej Krainy, 145, 4.8, Pol
038, 19980625, Claudia Meier, Hist, Geheimnisse der Alten Stadt, 168, 4.6, Dui
039, 20120310, Petra Novak, Mys, Tajemství Ukradeného Diamantu, 131, 3.9, Cze
040, 20150703, Sofia Alexandrescu, FicMisterul Cheii Disparute, 159, 3.7, Rom
040, 20150703, Sofia Alexandrescu, Mys, FicMisterul Cheii Disparute, 159, 3.7, Rom
54 changes: 35 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ fn main() {
// in the data file there is first some information
// than an # sign and than the actual data
let file: Vec<&str> = file.split('#').collect();

if file.len() < 2 {
println!(
"{} expected an '#' in the file to seperate the info from data but found none",
"error:".red().bold(),
);

std::process::exit(0);
}

let info = file[0];
let data = file[1];

Expand Down Expand Up @@ -130,43 +140,46 @@ fn main() {
// the line varaiable should contain 8 values
if line.len() < 8 {
println!(
"{} expected 8 variables sperated by one comma or at least two spaces but found {} variables \n{} {}",
"error:".red().bold(),
"{} expected 8 variables sperated by one comma or at least two spaces but found {} variables (skipping line) \n{} {} \n",
"warning:".yellow().bold(),
line.len(),
format!("{} |", index).cyan(),
data
);

std::process::exit(0);
continue;
}

let publication_date = NaiveDate::parse_from_str(
parse_number::<u32>(line[1], 2, index, data)
parse_number::<u32>(line[1], 2, index, data).unwrap_or(00000000)
.to_string()
.as_str(),
"%Y%m%d",
)
.unwrap_or_else(|_| {
println!(
"{} \"{}\"(variable no. 2) is not 8 numbers so it is not able to be parsed to an date \n{} {}",
"error:".red().bold(),
"{} \"{}\"(variable no. 2) is not 8 numbers so it is not able to be parsed to an date (replacing it with 2000-01-01) \n{} {} \n",
"warning:".yellow().bold(),
line[1],
format!("{} |", index).cyan(),
data,
);

std::process::exit(0);
NaiveDate::parse_from_str(
"20000101",
"%Y%m%d",
).unwrap()
});

books.push(Book {
id: parse_number(line[0], 1, index, data),
id: parse_number(line[0], 1, index, data).unwrap_or(0),
publication_date,
author: line[2],
genre: line[3],
title: line[4],
total_pages: parse_number(line[5], 6, index, data),
total_pages: parse_number(line[5], 6, index, data).unwrap_or(0),
// the valid range of ratings is 0.0 - 5.0
rating: parse_number::<f64>(line[6], 7, index, data),
rating: parse_number::<f64>(line[6], 7, index, data).unwrap_or(0.0),
language: line[7],
});
}
Expand All @@ -178,38 +191,41 @@ fn main() {
if let Some(write) = &args.write {
fs::write(write, &books).unwrap_or_else(|error| {
println!(
"{} unable to write to: \"{}\" \n{}",
"error:".red().bold(),
"{} unable to write to: \"{}\" (not writing) \n{} \n",
"warning:".yellow().bold(),
write,
error,
);

std::process::exit(0);
});
}

println!("{}", &books);
}

fn parse_number<T: std::str::FromStr>(number: &str, variable: u32, index: usize, line: &str) -> T {
fn parse_number<T: std::str::FromStr>(
number: &str,
variable: u32,
index: usize,
line: &str,
) -> Result<T, String> {
let filterd_number: String = number
.chars()
.filter(|c| c.is_ascii_digit() || c == &'.')
.collect();

if let Ok(filterd_number) = filterd_number.parse::<T>() {
filterd_number
Ok(filterd_number)
} else {
println!(
"{} \"{}\"(variable no. {}) is not of type {} \n{} {}",
"error:".red().bold(),
"{} \"{}\"(variable no. {}) is not of type {} (replacing it with 0) \n{} {} \n",
"warning:".yellow().bold(),
number,
variable,
std::any::type_name::<T>(),
format!("{} |", index).cyan(),
line,
);

std::process::exit(0);
Err("not an number".to_string())
}
}

0 comments on commit 70f38d6

Please sign in to comment.