robotrt-middleware-core 0.1.0-beta.2

RobotRT modular robotics runtime and middleware components.
Documentation
use std::time::Duration;

use crate::{QosProfile, RouteRule};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ReplayDegradeStrategy {
    BlockPublisher,
    DropOldest,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct TopicReliabilityPolicy {
    pub max_retry: u8,
    pub retry_timeout: Duration,
    pub max_inflight_per_subscriber: usize,
    pub replay_window: Option<usize>,
    pub replay_degrade_strategy: ReplayDegradeStrategy,
}

impl Default for TopicReliabilityPolicy {
    fn default() -> Self {
        Self {
            max_retry: 3,
            retry_timeout: Duration::from_millis(200),
            max_inflight_per_subscriber: 16,
            replay_window: None,
            replay_degrade_strategy: ReplayDegradeStrategy::BlockPublisher,
        }
    }
}

impl TopicReliabilityPolicy {
    pub fn with_max_retry(mut self, max_retry: u8) -> Self {
        self.max_retry = max_retry;
        self
    }

    pub fn with_retry_timeout(mut self, retry_timeout: Duration) -> Self {
        self.retry_timeout = retry_timeout;
        self
    }

    pub fn with_max_inflight_per_subscriber(mut self, max_inflight: usize) -> Self {
        self.max_inflight_per_subscriber = max_inflight;
        self
    }

    pub fn with_replay_window(mut self, replay_window: Option<usize>) -> Self {
        self.replay_window = replay_window;
        self
    }

    pub fn with_replay_degrade_strategy(mut self, strategy: ReplayDegradeStrategy) -> Self {
        self.replay_degrade_strategy = strategy;
        self
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TopicQosOverride {
    pub topic: String,
    pub profile: QosProfile,
}

impl TopicQosOverride {
    pub fn new(topic: impl Into<String>, profile: QosProfile) -> Self {
        Self {
            topic: topic.into(),
            profile,
        }
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct MiddlewareRuntimeConfig {
    pub route_rules: Vec<RouteRule>,
    pub topic_qos_overrides: Vec<TopicQosOverride>,
    pub namespace_isolation: bool,
    pub topic_reliability_policy: TopicReliabilityPolicy,
}

impl MiddlewareRuntimeConfig {
    pub fn with_namespace_isolation(mut self, enabled: bool) -> Self {
        self.namespace_isolation = enabled;
        self
    }

    pub fn add_route_rule(mut self, rule: RouteRule) -> Self {
        self.route_rules.push(rule);
        self
    }

    pub fn add_topic_qos_override(mut self, topic: impl Into<String>, profile: QosProfile) -> Self {
        self.topic_qos_overrides
            .push(TopicQosOverride::new(topic, profile));
        self
    }

    pub fn with_topic_reliability_policy(mut self, policy: TopicReliabilityPolicy) -> Self {
        self.topic_reliability_policy = policy;
        self
    }
}