Skip to main content

ara_com/
service.rs

1use crate::types::*;
2
3/// Marker trait for a service definition. Generated by cargo-arxml.
4pub trait ServiceDefinition: Send + Sync + 'static {
5    const SERVICE_ID: ServiceId;
6    const MAJOR_VERSION: MajorVersion;
7    const MINOR_VERSION: MinorVersion;
8
9    /// Human-readable service name (from ARXML short-name)
10    fn service_name() -> &'static str;
11}
12
13/// Handle for managing a service instance lifecycle
14#[derive(Debug)]
15pub struct ServiceHandle {
16    pub service_id: ServiceId,
17    pub instance_id: InstanceId,
18    state: ServiceState,
19}
20
21impl ServiceHandle {
22    /// Create a new `ServiceHandle` in the `Idle` state.
23    pub fn new(service_id: ServiceId, instance_id: InstanceId) -> Self {
24        Self {
25            service_id,
26            instance_id,
27            state: ServiceState::Idle,
28        }
29    }
30
31    /// Return the current state of this handle.
32    pub fn state(&self) -> &ServiceState {
33        &self.state
34    }
35
36    /// Transition to a new state.
37    pub fn set_state(&mut self, state: ServiceState) {
38        self.state = state;
39    }
40}
41
42#[derive(Debug, Clone, PartialEq, Eq)]
43pub enum ServiceState {
44    /// Not yet offered/requested
45    Idle,
46    /// Service is being offered (skeleton side)
47    Offered,
48    /// Service has been found (proxy side)
49    Available,
50    /// Service was available but is now gone
51    Unavailable,
52}
53
54/// Availability handler callback type
55pub type AvailabilityHandler = Box<dyn Fn(ServiceState) + Send + Sync>;
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_service_handle_state_transitions() {
63        let mut handle = ServiceHandle::new(ServiceId(0x1234), InstanceId(0x0001));
64        assert_eq!(handle.state(), &ServiceState::Idle);
65
66        handle.set_state(ServiceState::Offered);
67        assert_eq!(handle.state(), &ServiceState::Offered);
68
69        handle.set_state(ServiceState::Available);
70        assert_eq!(handle.state(), &ServiceState::Available);
71
72        handle.set_state(ServiceState::Unavailable);
73        assert_eq!(handle.state(), &ServiceState::Unavailable);
74    }
75}