From 6af5368a872b414ceadb7816427dbc4fbeac7e1c Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Sun, 4 Apr 2021 15:13:36 +0200 Subject: [PATCH] Error handler now returns a string instead (closes #10) Commit hook now properly returns status code on failed formatting --- .hooks/pre-commit | 5 ++++- src/catchers.rs | 6 ++++++ src/ivago/controller/pickup_times.rs | 14 ++++++++------ src/main.rs | 2 ++ 4 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 src/catchers.rs diff --git a/.hooks/pre-commit b/.hooks/pre-commit index a2dd254..825db47 100755 --- a/.hooks/pre-commit +++ b/.hooks/pre-commit @@ -1,7 +1,10 @@ #!/usr/bin/env bash # This hook lints the code, and if we're on develop or master, also forces the tests to pass. -cargo fmt -- --check +make lint &> /dev/null 2>&1 || { + >&2 echo "Format check failed, use 'make lint' for more information."; + exit 1; +} branch=`git rev-parse --abbrev-ref HEAD` diff --git a/src/catchers.rs b/src/catchers.rs new file mode 100644 index 0000000..7992450 --- /dev/null +++ b/src/catchers.rs @@ -0,0 +1,6 @@ +use rocket::Request; + +#[catch(404)] +pub fn not_found(_: &Request) -> String { + String::from("This route doesn't exist or doesn't use the specified parameters.") +} diff --git a/src/ivago/controller/pickup_times.rs b/src/ivago/controller/pickup_times.rs index 202f8fc..2434713 100644 --- a/src/ivago/controller/pickup_times.rs +++ b/src/ivago/controller/pickup_times.rs @@ -14,33 +14,34 @@ const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling"; const CAL_URL: &str = "https://www.ivago.be/nl/particulier/garbage/pick-up/pickups"; /// Represents a very simple Timezoneless date. Considering the timezone will -/// always be CEST (aka Belgium's timezone), this is good enough. +/// always be CEST (aka Belgium's timezone), this is good enough. I use this +/// instead of a NaiveDate to avoid E0117. pub struct BasicDate { year: u32, month: u8, day: u8, } +/// This allows us to use BasicDate as a query parameter in our routes. impl<'v> FromFormValue<'v> for BasicDate { type Error = &'v RawStr; fn from_form_value(form_value: &'v RawStr) -> Result { - // Beautiful how this exact example is in the docs match BasicDate::try_from(form_value.as_str()) { Err(_) => Err(form_value), - // Here, we can assume these parses will work, because the regex - // didn't fail Ok(date) => Ok(date), } } } +/// We need this when deserializing BasicDate. impl ToString for BasicDate { fn to_string(&self) -> String { format!("{}-{}-{}", self.year, self.month, self.day) } } +/// This is used to serialize BasicDate. impl TryFrom<&str> for BasicDate { type Error = (); @@ -49,8 +50,6 @@ impl TryFrom<&str> for BasicDate { match re.captures(s) { None => Err(()), - // Here, we can assume these parses will work, because the regex - // didn't fail Some(caps) => Ok(BasicDate { year: caps.get(1).unwrap().as_str().parse().unwrap(), month: caps.get(2).unwrap().as_str().parse().unwrap(), @@ -74,6 +73,9 @@ impl From for i64 { // Timezone of Brussels is UTC + 2 hours in the western hemisphere FixedOffset::west(7_200) .ymd(date.year as i32, date.month as u32, date.day as u32) + // For some reason, I couldn't get .timestamp() to work on a Date + // without a time component, even though the docs seemed to + // indicate this was possible .and_hms(0, 0, 0) .timestamp() } diff --git a/src/main.rs b/src/main.rs index 882db7c..49101f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate rocket; // Route modules +mod catchers; mod errors; mod hello; mod ivago; @@ -12,6 +13,7 @@ fn rocket() -> rocket::Rocket { rocket::ignite() .mount("/hello", hello::routes()) .mount("/ivago", ivago::routes()) + .register(catchers![catchers::not_found]) } fn main() {