use super::{Pump, RpcError};
use serde_json::{Value, json};
impl Pump {
pub(super) async fn configure(&mut self, id: &Value, change: &Value) -> anyhow::Result<()> {
if self
.opening
.as_ref()
.is_some_and(super::sessions::Opening::changes_session)
{
return self.refuse(id, "session_busy").await;
}
let Some(object) = change.as_object() else {
return self.refuse(id, "invalid_config").await;
};
if object.is_empty() {
self.say(json!({"kind":"config_options","options":self.session.config}))
.await?;
return self.ack(id).await;
}
if self.configuring.is_some() {
return self.refuse(id, "config_busy").await;
}
if object.len() != 2
|| !change["option"].is_string()
|| !(change["value"].is_string() || change["value"].is_boolean())
{
return self.refuse(id, "invalid_config").await;
}
let call = self.call_id()?;
self.output.request(call,"session/set_config_option",json!({"sessionId":self.session.id,"configId":change["option"],"value":change["value"]}))?;
self.configuring = Some((call, id.clone()));
self.ack(id).await
}
pub(super) async fn configured(
&mut self,
result: Result<Value, RpcError>,
) -> anyhow::Result<()> {
let (_, id) = self
.configuring
.take()
.ok_or_else(|| anyhow::anyhow!("config_request_absent"))?;
match result {
Ok(answer)=>{
anyhow::ensure!(answer["configOptions"].is_array(),"acp_config_response_invalid");
self.session.config=answer["configOptions"].clone();
self.say(json!({"kind":"config_options","request_id":id,"options":self.session.config})).await
}
Err(error)=>self.say(json!({"kind":"config_failed","request_id":id,"code":error.code,"message":error.message})).await,
}
}
}