use areev_core::decide::{questions_from_wire, DecideRequest, DecisionBackend};
use std::sync::Arc;
pub struct LoopDecider(pub Arc<dyn DecisionBackend>);
impl areev_loop::DecideBackend for LoopDecider {
fn decide(&self, request_json: &str) -> areev_loop::Result<String> {
let err = |m: String| areev_loop::Error::DecideBackend(m);
let v: serde_json::Value =
serde_json::from_str(request_json).map_err(|e| err(format!("request is not JSON: {e}")))?;
let questions = questions_from_wire(v.get("questions").unwrap_or(&serde_json::Value::Null))
.map_err(|e| err(e.to_string()))?;
let state = v.get("state").cloned().unwrap_or(serde_json::Value::Null);
let decision = self
.0
.decide(&DecideRequest::new(state, questions))
.map_err(|e| err(e.to_string()))?;
Ok(decision.to_json().to_string())
}
fn calibrated(&self) -> bool {
self.0.calibrated()
}
fn describe(&self) -> String {
self.0.describe()
}
}