Error handler now returns a string instead (closes #10)

Commit hook now properly returns status code on failed formatting
master
Jef Roosens 2021-04-04 15:13:36 +02:00
parent 64161ddcda
commit 6af5368a87
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
4 changed files with 20 additions and 7 deletions

View File

@ -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`

6
src/catchers.rs 100644
View File

@ -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.")
}

View File

@ -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<BasicDate, Self::Error> {
// 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<BasicDate> 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()
}

View File

@ -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() {