1use crate::types::*;
2
3pub trait ServiceDefinition: Send + Sync + 'static {
5 const SERVICE_ID: ServiceId;
6 const MAJOR_VERSION: MajorVersion;
7 const MINOR_VERSION: MinorVersion;
8
9 fn service_name() -> &'static str;
11}
12
13#[derive(Debug)]
15pub struct ServiceHandle {
16 pub service_id: ServiceId,
17 pub instance_id: InstanceId,
18 state: ServiceState,
19}
20
21impl ServiceHandle {
22 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 pub fn state(&self) -> &ServiceState {
33 &self.state
34 }
35
36 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 Idle,
46 Offered,
48 Available,
50 Unavailable,
52}
53
54pub 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}