working on rust side
This commit is contained in:
@@ -37,7 +37,7 @@ mod api {
|
||||
pub struct CookbookDbConn(diesel::SqliteConnection);
|
||||
|
||||
/// A serializable wrapper for [`cookbook::recipes::Recipe`]
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct RecipeObject {
|
||||
id: i32,
|
||||
title: String,
|
||||
@@ -76,13 +76,13 @@ mod api {
|
||||
Json( recipes::delete(&conn, id) )
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct TemplateItems {
|
||||
key: (String, String),
|
||||
value: Option<RecipeObject>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct TemplateObject {
|
||||
items: Vec<TemplateItems>
|
||||
}
|
||||
@@ -118,6 +118,65 @@ mod api {
|
||||
panic!("No solution at all !");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[post("/solver/complete", data="<partial>")]
|
||||
pub fn complete_problem(conn: CookbookDbConn, partial: Json<Vec<TemplateItems>>) -> Json<TemplateObject> {
|
||||
use planner::{
|
||||
template,
|
||||
solver::{Domain, Problem}
|
||||
};
|
||||
|
||||
println!("{:?}", partial);
|
||||
println!("Building 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) {
|
||||
// Let's hack for now
|
||||
// BUGGY because template does not generate every variables, needs refactoring
|
||||
// Find variable in partial
|
||||
let initial_id = partial.iter().find_map(|slot| {
|
||||
if slot.value.is_none() { return None; };
|
||||
//println!("{:?} vs {:?}", slot, var);
|
||||
if slot.key.0 == var.0
|
||||
&& slot.key.1 == format!("{:?}",var.1)
|
||||
{
|
||||
let id = slot.value.as_ref().unwrap().id;
|
||||
println!("found initial : recipe with id {}", id);
|
||||
return Some(id);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
});
|
||||
let ini = if let Some(id) = initial_id {
|
||||
let new_ini = domain.values.iter().find(|r| r.id == id);
|
||||
println!("Overrided {:?}", new_ini);
|
||||
new_ini
|
||||
} else {
|
||||
ini
|
||||
};
|
||||
// If found, override initial value
|
||||
problem = problem.add_variable(var, dom, ini);
|
||||
}
|
||||
let mut problem = problem
|
||||
.add_constraint(|_| true)
|
||||
.finish();
|
||||
if let Some(one_result) = problem.solve_one() {
|
||||
Json(TemplateObject {
|
||||
items: one_result
|
||||
.into_iter()
|
||||
.map(|(k,v)| {
|
||||
TemplateItems {
|
||||
key: (format!("{}", k.0), format!("{:?}", k.1)),
|
||||
value: v.map(|r| RecipeObject::from(&conn, r.clone())),
|
||||
}})
|
||||
.collect(),
|
||||
})
|
||||
} else {
|
||||
panic!("No solution at all !");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
@@ -137,7 +196,12 @@ fn main() -> Result<(), Error> {
|
||||
rocket::ignite()
|
||||
.attach(api::CookbookDbConn::fairing())
|
||||
.mount("/", routes![index, files])
|
||||
.mount("/api", routes![api::recipes_list, api::delete_recipe, api::one_solution])
|
||||
.mount("/api", routes![
|
||||
api::recipes_list,
|
||||
api::delete_recipe,
|
||||
api::one_solution,
|
||||
api::complete_problem,
|
||||
])
|
||||
.attach(cors)
|
||||
.launch();
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user