Added code formatting

master
Jef Roosens 2021-04-02 21:20:36 +02:00
parent 68434ca75f
commit e3f134a9bf
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
10 changed files with 40 additions and 52 deletions

View File

@ -1,6 +1,7 @@
#!/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
branch=`git rev-parse --abbrev-ref HEAD` branch=`git rev-parse --abbrev-ref HEAD`

View File

@ -32,6 +32,10 @@ test:
@ cargo test @ cargo test
.PHONY: test .PHONY: test
format:
@ cargo fmt
.PHONY: format
# Documentation # Documentation
docs: docs:

View File

@ -1,11 +1,8 @@
#[cfg(test)] mod tests; #[cfg(test)]
mod tests;
pub fn routes() -> Vec<rocket::Route> { pub fn routes() -> Vec<rocket::Route> {
routes![ routes![world, hello, name_age]
world,
hello,
name_age
]
} }
#[get("/world")] #[get("/world")]

View File

@ -1,5 +1,5 @@
use rocket::local::Client;
use rocket::http::Status; use rocket::http::Status;
use rocket::local::Client;
fn rocket() -> rocket::Rocket { fn rocket() -> rocket::Rocket {
rocket::ignite().mount("/", super::routes()) rocket::ignite().mount("/", super::routes())

View File

@ -1,8 +1,7 @@
mod search;
mod pickup_times; mod pickup_times;
pub use search::{Street, search_streets}; mod search;
pub use pickup_times::{get_pickup_times, PickupTime}; 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 ///// 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 = [ // let params = [
// ("_format", "json"), // ("_format", "json"),
// ("type", ""), // ("type", ""),
// ] // ]
//r2 = s.get("https://www.ivago.be/nl/particulier/garbage/pick-up/pickups?", //r2 = s.get("https://www.ivago.be/nl/particulier/garbage/pick-up/pickups?",

View File

@ -1,27 +1,23 @@
use std::error::Error;
use chrono::NaiveDate;
use super::search::Street; use super::search::Street;
use chrono::NaiveDate;
use std::error::Error;
const BASE_URL: &str = "https://www.ivago.be/nl/particulier/afval/ophaling"; 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 /// Represents a pickup time instance. All fields are a direct map of the
/// original API /// original API
pub struct PickupTime { pub struct PickupTime {
date: NaiveDate, date: NaiveDate,
label: String, label: String,
classes: Vec<String>, classes: Vec<String>,
url: String url: String,
} }
pub fn get_pickup_times( pub fn get_pickup_times(
street: Street, street: Street,
number: u64, number: u64,
start_date: NaiveDate, start_date: NaiveDate,
end_date: NaiveDate end_date: NaiveDate,
) -> Result<Vec<PickupTime>, Box<dyn Error>> { ) -> Result<Vec<PickupTime>, Box<dyn Error>> {
Ok(Vec::new()) Ok(Vec::new())
} }

View File

@ -1,13 +1,11 @@
use reqwest::blocking as reqwest; use reqwest::blocking as reqwest;
use serde::ser::{Serialize, SerializeStruct, Serializer};
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryFrom; use std::convert::TryFrom;
use serde::ser::{Serialize, Serializer, SerializeStruct};
use std::error::Error; use std::error::Error;
/// Endpoint for the search feature /// 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<Street> for String { impl From<Street> for String {
fn from(street: Street) -> String { fn from(street: Street) -> String {
@ -15,44 +13,39 @@ impl From<Street> for String {
} }
} }
impl Serialize for Street { impl Serialize for Street {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where where
S: Serializer, S: Serializer,
{ {
let mut s = serializer.serialize_struct("Street", 2)?; let mut s = serializer.serialize_struct("Street", 2)?;
s.serialize_field("name", &self.name)?; s.serialize_field("name", &self.name)?;
s.serialize_field("city", &self.city)?; s.serialize_field("city", &self.city)?;
s.end() s.end()
} }
} }
impl TryFrom<String> for Street { impl TryFrom<String> for Street {
type Error = (); type Error = ();
fn try_from(value: String) -> Result<Self, Self::Error> { fn try_from(value: String) -> Result<Self, Self::Error> {
if let Some(index) = value.find('(') { if let Some(index) = value.find('(') {
Ok(Street { Ok(Street {
name: (value[0 .. index - 1].trim()).to_string(), name: (value[0..index - 1].trim()).to_string(),
city: (value[index + 1 .. value.len() - 1].trim()).to_string(), city: (value[index + 1..value.len() - 1].trim()).to_string(),
}) })
} else {
}else {
Err(()) Err(())
} }
} }
} }
/// Represents a street /// Represents a street
pub struct Street { pub struct Street {
pub name: String, pub name: String,
pub city: String, pub city: String,
} }
/// Searches the Ivago API for streets in the given city /// Searches the Ivago API for streets in the given city
/// ///
/// # Arguments /// # Arguments
@ -62,9 +55,7 @@ pub struct Street {
// TODO find out how to do this async // TODO find out how to do this async
pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error>> { pub fn search_streets(street_name: &String) -> Result<Vec<Street>, Box<dyn Error>> {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let response = client.get(SEARCH_URL) let response = client.get(SEARCH_URL).query(&[("q", street_name)]).send()?;
.query(&[("q", street_name)])
.send()?;
let data: Vec<HashMap<String, String>> = response.json()?; let data: Vec<HashMap<String, String>> = response.json()?;
let mut output: Vec<Street> = Vec::new(); let mut output: Vec<Street> = Vec::new();

View File

@ -1,13 +1,12 @@
#[cfg(test)] mod tests;
mod controller; mod controller;
#[cfg(test)]
mod tests;
use rocket_contrib::json::Json;
use chrono::NaiveDate; use chrono::NaiveDate;
use rocket_contrib::json::Json;
pub fn routes() -> Vec<rocket::Route> { pub fn routes() -> Vec<rocket::Route> {
routes![ routes![route_search_streets,]
route_search_streets,
]
} }
// URL: https://www.ivago.be/nl/particulier/autocomplete/garbage/streets?q=Lange // 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, street: controller::Street,
number: u64, number: u64,
start_date: NaiveDate, start_date: NaiveDate,
end_date: NaiveDate end_date: NaiveDate,
) -> Json<Vec<controller::PickupTime>> { ) -> Json<Vec<controller::PickupTime>> {
} }

View File

@ -0,0 +1 @@

View File

@ -1,6 +1,7 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
// Route modules // Route modules
mod hello; mod hello;