use super::GoalClientLifecycle;
use crate::{ActionClient, CancelResponse, GoalUuid, MultiCancelResponse, RclrsError};
use rosidl_runtime_rs::Action;
use std::{
future::Future,
ops::{Deref, DerefMut},
pin::Pin,
sync::Arc,
task::{Context, Poll},
};
use tokio::sync::oneshot::Receiver;
pub struct CancellationClient<A: Action> {
client: ActionClient<A>,
goal_id: GoalUuid,
}
impl<A: Action> Clone for CancellationClient<A> {
fn clone(&self) -> Self {
Self {
client: Arc::clone(&self.client),
goal_id: self.goal_id,
}
}
}
impl<A: Action> CancellationClient<A> {
pub fn cancel(&self) -> CancelResponseClient<A> {
self.try_cancel().unwrap()
}
pub fn try_cancel(&self) -> Result<CancelResponseClient<A>, RclrsError> {
self.client
.board
.request_single_cancel(&self.client, self.goal_id)
}
pub(super) fn new(client: ActionClient<A>, goal_id: GoalUuid) -> Self {
Self { client, goal_id }
}
}
pub struct CancelResponseClient<A: Action> {
receiver: Receiver<CancelResponse>,
#[allow(unused)]
lifecycle: GoalClientLifecycle<A>,
}
impl<A: Action> Deref for CancelResponseClient<A> {
type Target = Receiver<CancelResponse>;
fn deref(&self) -> &Self::Target {
&self.receiver
}
}
impl<A: Action> DerefMut for CancelResponseClient<A> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.receiver
}
}
impl<A: Action> CancelResponseClient<A> {
pub(super) fn new(
receiver: Receiver<CancelResponse>,
lifecycle: GoalClientLifecycle<A>,
) -> Self {
Self {
receiver,
lifecycle,
}
}
}
impl<A: Action> Future for CancelResponseClient<A> {
type Output = CancelResponse;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Future::poll(Pin::new(&mut self.get_mut().receiver), cx)
.map(|result| result.unwrap())
}
}
pub struct MultiCancelResponseClient<A: Action> {
receiver: Receiver<MultiCancelResponse>,
#[allow(unused)]
lifecycle: GoalClientLifecycle<A>,
}
impl<A: Action> Deref for MultiCancelResponseClient<A> {
type Target = Receiver<MultiCancelResponse>;
fn deref(&self) -> &Self::Target {
&self.receiver
}
}
impl<A: Action> DerefMut for MultiCancelResponseClient<A> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.receiver
}
}
impl<A: Action> MultiCancelResponseClient<A> {
pub(super) fn new(
receiver: Receiver<MultiCancelResponse>,
lifecycle: GoalClientLifecycle<A>,
) -> Self {
Self {
receiver,
lifecycle,
}
}
}