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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<String>,
url: String
url: String,
}
pub fn get_pickup_times(
street: Street,
number: u64,
start_date: NaiveDate,
end_date: NaiveDate
end_date: NaiveDate,
) -> Result<Vec<PickupTime>, Box<dyn Error>> {
Ok(Vec::new())
}

View File

@ -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<Street> for String {
fn from(street: Street) -> String {
@ -15,44 +13,39 @@ impl From<Street> for String {
}
}
impl Serialize for Street {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<String> for Street {
type Error = ();
fn try_from(value: String) -> Result<Self, Self::Error> {
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<Vec<Street>, Box<dyn Error>> {
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<HashMap<String, String>> = response.json()?;
let mut output: Vec<Street> = Vec::new();

View File

@ -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<rocket::Route> {
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<Vec<controller::PickupTime>> {
end_date: NaiveDate,
) -> Json<Vec<controller::PickupTime>> {
}

View File

@ -0,0 +1 @@

View File

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