From e3f134a9bf770664078e1b2ad85d57bf48a6b710 Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 2 Apr 2021 21:20:36 +0200 Subject: [PATCH] Added code formatting --- .hooks/pre-commit | 1 + Makefile | 4 +++ src/hello/mod.rs | 9 +++---- src/hello/tests.rs | 2 +- src/ivago/controller/mod.rs | 7 +++-- src/ivago/controller/pickup_times.rs | 12 +++------ src/ivago/controller/search.rs | 39 +++++++++++----------------- src/ivago/mod.rs | 14 +++++----- src/ivago/tests.rs | 1 + src/main.rs | 3 ++- 10 files changed, 40 insertions(+), 52 deletions(-) diff --git a/.hooks/pre-commit b/.hooks/pre-commit index d9dd92d..a2dd254 100755 --- a/.hooks/pre-commit +++ b/.hooks/pre-commit @@ -1,6 +1,7 @@ #!/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 branch=`git rev-parse --abbrev-ref HEAD` diff --git a/Makefile b/Makefile index 4017a77..2f9e53d 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,10 @@ test: @ cargo test .PHONY: test +format: + @ cargo fmt +.PHONY: format + # Documentation docs: diff --git a/src/hello/mod.rs b/src/hello/mod.rs index c15060b..608d234 100644 --- a/src/hello/mod.rs +++ b/src/hello/mod.rs @@ -1,11 +1,8 @@ -#[cfg(test)] mod tests; +#[cfg(test)] +mod tests; pub fn routes() -> Vec { - routes![ - world, - hello, - name_age - ] + routes![world, hello, name_age] } #[get("/world")] diff --git a/src/hello/tests.rs b/src/hello/tests.rs index d4f7096..e19264d 100644 --- a/src/hello/tests.rs +++ b/src/hello/tests.rs @@ -1,5 +1,5 @@ -use rocket::local::Client; use rocket::http::Status; +use rocket::local::Client; fn rocket() -> rocket::Rocket { rocket::ignite().mount("/", super::routes()) diff --git a/src/ivago/controller/mod.rs b/src/ivago/controller/mod.rs index e2555c5..6ff9dff 100644 --- a/src/ivago/controller/mod.rs +++ b/src/ivago/controller/mod.rs @@ -1,8 +1,7 @@ -mod search; mod pickup_times; -pub use search::{Street, search_streets}; +mod search; pub use pickup_times::{get_pickup_times, PickupTime}; - +pub use search::{search_streets, Street}; ///// Return the known pickup times for the given street and/or city ///// @@ -30,7 +29,7 @@ pub use pickup_times::{get_pickup_times, PickupTime}; // let params = [ // ("_format", "json"), // ("type", ""), - + // ] //r2 = s.get("https://www.ivago.be/nl/particulier/garbage/pick-up/pickups?", diff --git a/src/ivago/controller/pickup_times.rs b/src/ivago/controller/pickup_times.rs index 37d84c4..aa86a1d 100644 --- a/src/ivago/controller/pickup_times.rs +++ b/src/ivago/controller/pickup_times.rs @@ -1,27 +1,23 @@ -use std::error::Error; -use chrono::NaiveDate; use super::search::Street; - +use chrono::NaiveDate; +use std::error::Error; const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling"; - - /// Represents a pickup time instance. All fields are a direct map of the /// original API pub struct PickupTime { date: NaiveDate, label: String, classes: Vec, - url: String + url: String, } - pub fn get_pickup_times( street: Street, number: u64, start_date: NaiveDate, - end_date: NaiveDate + end_date: NaiveDate, ) -> Result, Box> { Ok(Vec::new()) } diff --git a/src/ivago/controller/search.rs b/src/ivago/controller/search.rs index d0282de..2df30b9 100644 --- a/src/ivago/controller/search.rs +++ b/src/ivago/controller/search.rs @@ -1,13 +1,11 @@ use reqwest::blocking as reqwest; +use serde::ser::{Serialize, SerializeStruct, Serializer}; use std::collections::HashMap; use std::convert::TryFrom; -use serde::ser::{Serialize, Serializer, SerializeStruct}; use std::error::Error; - /// Endpoint for the search feature -const SEARCH_URL: &str ="https://www.ivago.be/nl/particulier/autocomplete/garbage/streets"; - +const SEARCH_URL: &str = "https://www.ivago.be/nl/particulier/autocomplete/garbage/streets"; impl From for String { fn from(street: Street) -> String { @@ -15,44 +13,39 @@ impl From for String { } } - impl Serialize for Street { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - let mut s = serializer.serialize_struct("Street", 2)?; - s.serialize_field("name", &self.name)?; - s.serialize_field("city", &self.city)?; - s.end() - } + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut s = serializer.serialize_struct("Street", 2)?; + s.serialize_field("name", &self.name)?; + s.serialize_field("city", &self.city)?; + s.end() + } } - impl TryFrom for Street { type Error = (); fn try_from(value: String) -> Result { if let Some(index) = value.find('(') { Ok(Street { - name: (value[0 .. index - 1].trim()).to_string(), - city: (value[index + 1 .. value.len() - 1].trim()).to_string(), + name: (value[0..index - 1].trim()).to_string(), + city: (value[index + 1..value.len() - 1].trim()).to_string(), }) - - }else { + } else { Err(()) } } } - /// Represents a street pub struct Street { pub name: String, pub city: String, } - /// Searches the Ivago API for streets in the given city /// /// # Arguments @@ -62,9 +55,7 @@ pub struct Street { // TODO find out how to do this async pub fn search_streets(street_name: &String) -> Result, Box> { let client = reqwest::Client::new(); - let response = client.get(SEARCH_URL) - .query(&[("q", street_name)]) - .send()?; + let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?; let data: Vec> = response.json()?; let mut output: Vec = Vec::new(); diff --git a/src/ivago/mod.rs b/src/ivago/mod.rs index d24dc28..9cf77dc 100644 --- a/src/ivago/mod.rs +++ b/src/ivago/mod.rs @@ -1,13 +1,12 @@ -#[cfg(test)] mod tests; mod controller; +#[cfg(test)] +mod tests; -use rocket_contrib::json::Json; use chrono::NaiveDate; +use rocket_contrib::json::Json; pub fn routes() -> Vec { - routes![ - route_search_streets, - ] + routes![route_search_streets,] } // URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange @@ -26,7 +25,6 @@ pub fn route_get_pickup_times( street: controller::Street, number: u64, start_date: NaiveDate, - end_date: NaiveDate - ) -> Json> { - + end_date: NaiveDate, +) -> Json> { } diff --git a/src/ivago/tests.rs b/src/ivago/tests.rs index e69de29..8b13789 100644 --- a/src/ivago/tests.rs +++ b/src/ivago/tests.rs @@ -0,0 +1 @@ + diff --git a/src/main.rs b/src/main.rs index 43348ab..8a5ea07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![feature(proc_macro_hygiene, decl_macro)] -#[macro_use] extern crate rocket; +#[macro_use] +extern crate rocket; // Route modules mod hello;