use crate::types::*;
pub trait ServiceDefinition: Send + Sync + 'static {
const SERVICE_ID: ServiceId;
const MAJOR_VERSION: MajorVersion;
const MINOR_VERSION: MinorVersion;
fn service_name() -> &'static str;
}
#[derive(Debug)]
pub struct ServiceHandle {
pub service_id: ServiceId,
pub instance_id: InstanceId,
state: ServiceState,
}
impl ServiceHandle {
pub fn new(service_id: ServiceId, instance_id: InstanceId) -> Self {
Self {
service_id,
instance_id,
state: ServiceState::Idle,
}
}
pub fn state(&self) -> &ServiceState {
&self.state
}
pub fn set_state(&mut self, state: ServiceState) {
self.state = state;
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ServiceState {
Idle,
Offered,
Available,
Unavailable,
}
pub type AvailabilityHandler = Box<dyn Fn(ServiceState) + Send + Sync>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_service_handle_state_transitions() {
let mut handle = ServiceHandle::new(ServiceId(0x1234), InstanceId(0x0001));
assert_eq!(handle.state(), &ServiceState::Idle);
handle.set_state(ServiceState::Offered);
assert_eq!(handle.state(), &ServiceState::Offered);
handle.set_state(ServiceState::Available);
assert_eq!(handle.state(), &ServiceState::Available);
handle.set_state(ServiceState::Unavailable);
assert_eq!(handle.state(), &ServiceState::Unavailable);
}
}