use std::borrow::Cow;
use std::marker::PhantomData;
use rclrs::{PublisherOptions, QoSProfile, RclrsError};
pub struct PublisherBuilder<T>
where
T: rclrs::MessageIDL,
{
node: rclrs::Node,
topic: String,
qos: QoSProfile,
_phantom: PhantomData<T>,
}
impl<T> PublisherBuilder<T>
where
T: rclrs::MessageIDL,
{
pub(crate) fn new<'a>(node: rclrs::Node, topic: impl Into<Cow<'a, str>>) -> Self {
Self { node, topic: topic.into().into_owned(), qos: QoSProfile::topics_default(), _phantom: PhantomData }
}
pub fn topic<'a>(mut self, topic: impl Into<Cow<'a, str>>) -> Self {
self.topic = topic.into().into_owned();
self
}
pub fn qos(mut self, profile: QoSProfile) -> Self {
self.qos = profile;
self
}
pub fn with_options<'a>(mut self, options: PublisherOptions<'a>) -> Self {
self.topic = options.topic.to_string();
self.qos = options.qos;
self
}
pub fn reliable(mut self) -> Self {
self.qos = self.qos.reliable();
self
}
pub fn best_effort(mut self) -> Self {
self.qos = self.qos.best_effort();
self
}
pub fn transient_local(mut self) -> Self {
self.qos = self.qos.transient_local();
self
}
pub fn volatile(mut self) -> Self {
self.qos = self.qos.volatile();
self
}
pub fn history_keep_last(mut self, depth: u32) -> Self {
self.qos = self.qos.keep_last(depth);
self
}
pub fn avoid_ros_namespace_conventions(mut self, avoid: bool) -> Self {
self.qos.avoid_ros_namespace_conventions = avoid;
self
}
pub fn create(self) -> Result<rclrs::Publisher<T>, RclrsError> {
let mut options = PublisherOptions::new(&self.topic);
options.qos = self.qos;
self.node.create_publisher::<T>(options)
}
}