Error handler now returns a string instead (closes #10)
Commit hook now properly returns status code on failed formattingmaster
parent
64161ddcda
commit
6af5368a87
|
@ -1,7 +1,10 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
# This hook lints the code, and if we're on develop or master, also forces the tests to pass.
|
# 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`
|
branch=`git rev-parse --abbrev-ref HEAD`
|
||||||
|
|
||||||
|
|
|
@ -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.")
|
||||||
|
}
|
|
@ -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";
|
const CAL_URL: &str = "https://www.ivago.be/nl/particulier/garbage/pick-up/pickups";
|
||||||
|
|
||||||
/// Represents a very simple Timezoneless date. Considering the timezone will
|
/// 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 {
|
pub struct BasicDate {
|
||||||
year: u32,
|
year: u32,
|
||||||
month: u8,
|
month: u8,
|
||||||
day: u8,
|
day: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This allows us to use BasicDate as a query parameter in our routes.
|
||||||
impl<'v> FromFormValue<'v> for BasicDate {
|
impl<'v> FromFormValue<'v> for BasicDate {
|
||||||
type Error = &'v RawStr;
|
type Error = &'v RawStr;
|
||||||
|
|
||||||
fn from_form_value(form_value: &'v RawStr) -> Result<BasicDate, Self::Error> {
|
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()) {
|
match BasicDate::try_from(form_value.as_str()) {
|
||||||
Err(_) => Err(form_value),
|
Err(_) => Err(form_value),
|
||||||
// Here, we can assume these parses will work, because the regex
|
|
||||||
// didn't fail
|
|
||||||
Ok(date) => Ok(date),
|
Ok(date) => Ok(date),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// We need this when deserializing BasicDate.
|
||||||
impl ToString for BasicDate {
|
impl ToString for BasicDate {
|
||||||
fn to_string(&self) -> String {
|
fn to_string(&self) -> String {
|
||||||
format!("{}-{}-{}", self.year, self.month, self.day)
|
format!("{}-{}-{}", self.year, self.month, self.day)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This is used to serialize BasicDate.
|
||||||
impl TryFrom<&str> for BasicDate {
|
impl TryFrom<&str> for BasicDate {
|
||||||
type Error = ();
|
type Error = ();
|
||||||
|
|
||||||
|
@ -49,8 +50,6 @@ impl TryFrom<&str> for BasicDate {
|
||||||
|
|
||||||
match re.captures(s) {
|
match re.captures(s) {
|
||||||
None => Err(()),
|
None => Err(()),
|
||||||
// Here, we can assume these parses will work, because the regex
|
|
||||||
// didn't fail
|
|
||||||
Some(caps) => Ok(BasicDate {
|
Some(caps) => Ok(BasicDate {
|
||||||
year: caps.get(1).unwrap().as_str().parse().unwrap(),
|
year: caps.get(1).unwrap().as_str().parse().unwrap(),
|
||||||
month: caps.get(2).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
|
// Timezone of Brussels is UTC + 2 hours in the western hemisphere
|
||||||
FixedOffset::west(7_200)
|
FixedOffset::west(7_200)
|
||||||
.ymd(date.year as i32, date.month as u32, date.day as u32)
|
.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)
|
.and_hms(0, 0, 0)
|
||||||
.timestamp()
|
.timestamp()
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
// Route modules
|
// Route modules
|
||||||
|
mod catchers;
|
||||||
mod errors;
|
mod errors;
|
||||||
mod hello;
|
mod hello;
|
||||||
mod ivago;
|
mod ivago;
|
||||||
|
@ -12,6 +13,7 @@ fn rocket() -> rocket::Rocket {
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.mount("/hello", hello::routes())
|
.mount("/hello", hello::routes())
|
||||||
.mount("/ivago", ivago::routes())
|
.mount("/ivago", ivago::routes())
|
||||||
|
.register(catchers![catchers::not_found])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in New Issue