use derive_deftly::Deftly;
use futures::FutureExt as _;
use std::sync::Arc;
use tor_rpcbase::{self as rpc, SingleIdResponse, templates::*};
#[derive(Debug, serde::Deserialize, Deftly)]
#[derive_deftly(DynMethod)]
#[deftly(rpc(method_name = "rpc:release", bypass_method_dispatch))]
struct RpcRelease {}
impl rpc::RpcMethod for RpcRelease {
type Output = rpc::Nil;
type Update = rpc::NoUpdates;
}
impl rpc::DynMethod for RpcRelease {
fn invoke_without_dispatch(
&self,
ctx: Arc<dyn rpc::Context>,
obj_id: &rpc::ObjectId,
) -> Result<tor_rpcbase::dispatch::RpcResultFuture, tor_rpcbase::InvokeError> {
let result = match ctx.release(obj_id) {
Ok(()) => Ok(Box::new(rpc::NIL) as _),
Err(e) => Err(rpc::RpcError::from(e)),
};
Ok(futures::future::ready(result).boxed())
}
}
#[derive(Debug, serde::Deserialize, Deftly)]
#[derive_deftly(DynMethod)]
#[deftly(rpc(method_name = "rpc:clone_id", bypass_method_dispatch))]
struct RpcCloneId {
#[serde(default)]
weak: bool,
#[serde(default)]
release: bool,
}
impl rpc::RpcMethod for RpcCloneId {
type Output = rpc::SingleIdResponse;
type Update = rpc::NoUpdates;
}
impl rpc::DynMethod for RpcCloneId {
fn invoke_without_dispatch(
&self,
ctx: Arc<dyn rpc::Context>,
obj_id: &rpc::ObjectId,
) -> Result<tor_rpcbase::dispatch::RpcResultFuture, tor_rpcbase::InvokeError> {
let result = match ctx.lookup_object(obj_id) {
Ok(obj) => {
let new_id = if self.weak {
ctx.register_weak(&obj)
} else {
ctx.register_owned(obj)
};
if self.release {
let _ignore = ctx.release(obj_id);
}
Ok(Box::new(SingleIdResponse::from(new_id)) as _)
}
Err(e) => Err(rpc::RpcError::from(e)),
};
Ok(futures::future::ready(result).boxed())
}
}