adds Key generic parameters, use custom Key type from template.rs

This commit is contained in:
2019-02-10 14:43:18 +01:00
parent 29b2f076bf
commit 2f3993bb9e
11 changed files with 241 additions and 279 deletions

View File

@@ -5,8 +5,9 @@
#[macro_use] extern crate serde_derive;
extern crate rocket_cors;
extern crate cookbook;
extern crate planner;
use std::path::Path;
use std::path::{Path, PathBuf};
use rocket::{
response::{NamedFile, status::NotFound},
http::Method,
@@ -15,8 +16,14 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Error};
#[get("/")]
fn index() -> Result<NamedFile, NotFound<String>> {
NamedFile::open(&Path::new("./html/index.html"))
.map_err(|_| NotFound(format!("Server error : index not found")))
files(PathBuf::from("index.html"))
}
#[get("/<file..>", rank=6)]
fn files(file: PathBuf) -> Result<NamedFile, NotFound<String>> {
let path = Path::new("vue/dist/").join(file);
NamedFile::open(&path)
.map_err(|_| NotFound(format!("Bad path: {:?}", path)))
}
mod api {
@@ -68,6 +75,27 @@ mod api {
pub fn delete_recipe(conn: CookbookDbConn, id: i32) -> Json<bool> {
Json( recipes::delete(&conn, id) )
}
#[get("/solver/one")]
pub fn one_solution(conn: CookbookDbConn) -> Json<String> {
use planner::{
template,
solver::{Domain, Problem}
};
let possible_values = recipes::load_all(&conn);
let domain = Domain::new(possible_values);
let mut problem = Problem::build();
for (var, dom, ini) in template::Template::generate_variables(&domain) {
problem = problem.add_variable(var, dom, ini);
}
let mut problem = problem
.add_constraint(|_| true)
.finish();
let results = problem.solve_all();
Json(format!("{:?}", results.first().unwrap()))
}
}
fn main() -> Result<(), Error> {
@@ -86,8 +114,8 @@ fn main() -> Result<(), Error> {
rocket::ignite()
.attach(api::CookbookDbConn::fairing())
.mount("/", routes![index])
.mount("/api", routes![api::recipes_list, api::delete_recipe])
.mount("/", routes![index, files])
.mount("/api", routes![api::recipes_list, api::delete_recipe, api::one_solution])
.attach(cors)
.launch();
Ok(())