robotrt-buffer-core 0.1.0-beta.2

RobotRT modular robotics runtime and middleware components.
Documentation
use std::path::PathBuf;

use core_types::{ErrorCode, ErrorDomain, RtError};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ShmRateLimitConfig {
    pub max_bytes_per_sec: u64,
    pub burst_bytes: u64,
}

impl ShmRateLimitConfig {
    pub fn new(max_bytes_per_sec: u64, burst_bytes: u64) -> Self {
        Self {
            max_bytes_per_sec,
            burst_bytes,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ShmTransportConfig {
    pub namespace: String,
    pub directory: PathBuf,
    pub segment_payload_bytes: usize,
    pub segment_count: usize,
    pub rate_limit: Option<ShmRateLimitConfig>,
}

impl Default for ShmTransportConfig {
    fn default() -> Self {
        Self {
            namespace: "robotrt-default".to_string(),
            directory: std::env::temp_dir().join("robotrt-shm"),
            segment_payload_bytes: 8 * 1024 * 1024,
            segment_count: 8,
            rate_limit: None,
        }
    }
}

pub(super) fn validate_cfg(cfg: &ShmTransportConfig) -> Result<(), RtError> {
    if cfg.segment_payload_bytes == 0 {
        return Err(RtError::new(
            ErrorCode::InvalidState,
            ErrorDomain::Core,
            false,
            "segment_payload_bytes must be > 0",
        ));
    }
    if cfg.segment_count == 0 {
        return Err(RtError::new(
            ErrorCode::InvalidState,
            ErrorDomain::Core,
            false,
            "segment_count must be > 0",
        ));
    }
    if cfg.namespace.is_empty() {
        return Err(RtError::new(
            ErrorCode::InvalidState,
            ErrorDomain::Core,
            false,
            "namespace must not be empty",
        ));
    }

    if let Some(limit) = &cfg.rate_limit
        && (limit.max_bytes_per_sec == 0 || limit.burst_bytes == 0)
    {
        return Err(RtError::new(
            ErrorCode::InvalidState,
            ErrorDomain::Core,
            false,
            "rate limit config requires max_bytes_per_sec > 0 and burst_bytes > 0",
        ));
    }

    Ok(())
}