1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use super::{
LaunchdServiceManager, OpenRcServiceManager, RcdServiceManager, ScServiceManager,
ServiceInstallCtx, ServiceLevel, ServiceManager, ServiceManagerKind, ServiceStartCtx,
ServiceStopCtx, ServiceUninstallCtx, SystemdServiceManager,
};
use std::io;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TypedServiceManager {
Launchd(LaunchdServiceManager),
OpenRc(OpenRcServiceManager),
Rcd(RcdServiceManager),
Sc(ScServiceManager),
Systemd(SystemdServiceManager),
}
macro_rules! using {
($self:ident, $this:ident -> $expr:expr) => {{
match $self {
TypedServiceManager::Launchd($this) => $expr,
TypedServiceManager::OpenRc($this) => $expr,
TypedServiceManager::Rcd($this) => $expr,
TypedServiceManager::Sc($this) => $expr,
TypedServiceManager::Systemd($this) => $expr,
}
}};
}
impl ServiceManager for TypedServiceManager {
fn available(&self) -> io::Result<bool> {
using!(self, x -> x.available())
}
fn install(&self, ctx: ServiceInstallCtx) -> io::Result<()> {
using!(self, x -> x.install(ctx))
}
fn uninstall(&self, ctx: ServiceUninstallCtx) -> io::Result<()> {
using!(self, x -> x.uninstall(ctx))
}
fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
using!(self, x -> x.start(ctx))
}
fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> {
using!(self, x -> x.stop(ctx))
}
fn level(&self) -> ServiceLevel {
using!(self, x -> x.level())
}
fn set_level(&mut self, level: ServiceLevel) -> io::Result<()> {
using!(self, x -> x.set_level(level))
}
}
impl TypedServiceManager {
pub fn target_or_native(kind: impl Into<Option<ServiceManagerKind>>) -> io::Result<Self> {
match kind.into() {
Some(kind) => Ok(Self::target(kind)),
None => Self::native(),
}
}
pub fn target(kind: ServiceManagerKind) -> Self {
match kind {
ServiceManagerKind::Launchd => Self::Launchd(LaunchdServiceManager::default()),
ServiceManagerKind::OpenRc => Self::OpenRc(OpenRcServiceManager::default()),
ServiceManagerKind::Rcd => Self::Rcd(RcdServiceManager::default()),
ServiceManagerKind::Sc => Self::Sc(ScServiceManager::default()),
ServiceManagerKind::Systemd => Self::Systemd(SystemdServiceManager::default()),
}
}
pub fn native() -> io::Result<Self> {
Ok(Self::target(ServiceManagerKind::native()?))
}
pub fn into_box(self) -> Box<dyn ServiceManager> {
using!(self, x -> Box::new(x))
}
pub fn is_launchd(&self) -> bool {
matches!(self, Self::Launchd(_))
}
pub fn is_openrc(&self) -> bool {
matches!(self, Self::OpenRc(_))
}
pub fn is_rc_d(&self) -> bool {
matches!(self, Self::Rcd(_))
}
pub fn is_sc(&self) -> bool {
matches!(self, Self::Sc(_))
}
pub fn is_systemd(&self) -> bool {
matches!(self, Self::Systemd(_))
}
}
impl From<super::LaunchdServiceManager> for TypedServiceManager {
fn from(manager: super::LaunchdServiceManager) -> Self {
Self::Launchd(manager)
}
}
impl From<super::OpenRcServiceManager> for TypedServiceManager {
fn from(manager: super::OpenRcServiceManager) -> Self {
Self::OpenRc(manager)
}
}
impl From<super::RcdServiceManager> for TypedServiceManager {
fn from(manager: super::RcdServiceManager) -> Self {
Self::Rcd(manager)
}
}
impl From<super::ScServiceManager> for TypedServiceManager {
fn from(manager: super::ScServiceManager) -> Self {
Self::Sc(manager)
}
}
impl From<super::SystemdServiceManager> for TypedServiceManager {
fn from(manager: super::SystemdServiceManager) -> Self {
Self::Systemd(manager)
}
}