simple-someip 0.10.0

A lightweight SOME/IP serialization and communication library
Documentation
//! State tracking structures for E2E profiles.

/// State for E2E Profile 4 protection/checking.
#[derive(Debug, Clone)]
pub struct Profile4State {
    /// Counter for protection (incremented on each protect call).
    pub(crate) protect_counter: u16,
    /// Last received counter for checking.
    pub(crate) last_counter: Option<u16>,
}

impl Profile4State {
    /// Create a new Profile 4 state with initial counter value of 0.
    #[must_use]
    pub fn new() -> Self {
        Self {
            protect_counter: 0,
            last_counter: None,
        }
    }

    /// Create a new Profile 4 state with a specific initial counter.
    #[must_use]
    pub fn with_initial_counter(counter: u16) -> Self {
        Self {
            protect_counter: counter,
            last_counter: None,
        }
    }

    /// Returns the current protection counter value.
    #[must_use]
    pub const fn protect_counter(&self) -> u16 {
        self.protect_counter
    }

    /// Returns the last received counter value, or `None` if no message
    /// has been checked yet.
    #[must_use]
    pub const fn last_counter(&self) -> Option<u16> {
        self.last_counter
    }

    /// Reset the state to initial values.
    pub fn reset(&mut self) {
        self.protect_counter = 0;
        self.last_counter = None;
    }
}

impl Default for Profile4State {
    fn default() -> Self {
        Self::new()
    }
}

/// State for E2E Profile 5 protection/checking.
#[derive(Debug, Clone)]
pub struct Profile5State {
    /// Counter for protection (incremented on each protect call).
    pub(crate) protect_counter: u8,
    /// Last received counter for checking.
    pub(crate) last_counter: Option<u8>,
}

impl Profile5State {
    /// Create a new Profile 5 state with initial counter value of 0.
    #[must_use]
    pub fn new() -> Self {
        Self {
            protect_counter: 0,
            last_counter: None,
        }
    }

    /// Create a new Profile 5 state with a specific initial counter.
    #[must_use]
    pub fn with_initial_counter(counter: u8) -> Self {
        Self {
            protect_counter: counter,
            last_counter: None,
        }
    }

    /// Returns the current protection counter value.
    #[must_use]
    pub const fn protect_counter(&self) -> u8 {
        self.protect_counter
    }

    /// Returns the last received counter value, or `None` if no message
    /// has been checked yet.
    #[must_use]
    pub const fn last_counter(&self) -> Option<u8> {
        self.last_counter
    }

    /// Reset the state to initial values.
    pub fn reset(&mut self) {
        self.protect_counter = 0;
        self.last_counter = None;
    }
}

impl Default for Profile5State {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn profile4_reset_clears_state() {
        let mut state = Profile4State::with_initial_counter(42);
        state.last_counter = Some(10);
        state.reset();
        assert_eq!(state.protect_counter, 0);
        assert!(state.last_counter.is_none());
    }

    #[test]
    fn profile4_default_matches_new() {
        let from_new = Profile4State::new();
        let from_default = Profile4State::default();
        assert_eq!(from_new.protect_counter, from_default.protect_counter);
        assert_eq!(from_new.last_counter, from_default.last_counter);
    }

    #[test]
    fn profile5_reset_clears_state() {
        let mut state = Profile5State::with_initial_counter(42);
        state.last_counter = Some(10);
        state.reset();
        assert_eq!(state.protect_counter, 0);
        assert!(state.last_counter.is_none());
    }

    #[test]
    fn profile5_default_matches_new() {
        let from_new = Profile5State::new();
        let from_default = Profile5State::default();
        assert_eq!(from_new.protect_counter, from_default.protect_counter);
        assert_eq!(from_new.last_counter, from_default.last_counter);
    }

    #[test]
    fn profile4_with_initial_counter_sets_counter() {
        let state = Profile4State::with_initial_counter(42);
        assert_eq!(state.protect_counter(), 42);
        assert_eq!(state.last_counter(), None);
    }

    #[test]
    fn profile4_accessors() {
        let mut state = Profile4State::new();
        assert_eq!(state.protect_counter(), 0);
        assert_eq!(state.last_counter(), None);
        state.protect_counter = 5;
        state.last_counter = Some(3);
        assert_eq!(state.protect_counter(), 5);
        assert_eq!(state.last_counter(), Some(3));
    }

    #[test]
    fn profile5_with_initial_counter_sets_counter() {
        let state = Profile5State::with_initial_counter(42);
        assert_eq!(state.protect_counter(), 42);
        assert_eq!(state.last_counter(), None);
    }

    #[test]
    fn profile5_accessors() {
        let mut state = Profile5State::new();
        assert_eq!(state.protect_counter(), 0);
        assert_eq!(state.last_counter(), None);
        state.protect_counter = 5;
        state.last_counter = Some(3);
        assert_eq!(state.protect_counter(), 5);
        assert_eq!(state.last_counter(), Some(3));
    }
}