Skip to content
This repository was archived by the owner on Dec 15, 2025. It is now read-only.

Commit eb82073

Browse files
committed
fixed formatting
1 parent 47344c7 commit eb82073

File tree

2 files changed

+164
-75
lines changed

2 files changed

+164
-75
lines changed

src/app/rpc.rs

Lines changed: 156 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::collections::HashMap;
1010
use std::str::FromStr;
1111
use tokio_stream::StreamExt;
1212

13-
use anyhow::Error as AnyError;
1413
use crate::zinit::errors::ZInitError;
14+
use anyhow::Error as AnyError;
1515

1616
use super::api::Api;
1717

@@ -45,16 +45,45 @@ fn code_name_from(code: i32) -> &'static str {
4545
}
4646

4747
// Map ZInit/anyhow error -> JSON-RPC ErrorObjectOwned with structured details.
48-
fn zinit_err_to_rpc(err: AnyError, action: &str, service: Option<&str>, default_code: i32) -> ErrorObjectOwned {
48+
fn zinit_err_to_rpc(
49+
err: AnyError,
50+
action: &str,
51+
service: Option<&str>,
52+
default_code: i32,
53+
) -> ErrorObjectOwned {
4954
// Choose code/name/message from domain errors when possible
5055
let (code, code_name, top_msg) = match err.downcast_ref::<ZInitError>() {
51-
Some(ZInitError::UnknownService { name }) => (SERVICE_NOT_FOUND, "ServiceNotFound", format!("service '{}' not found", name)),
52-
Some(ZInitError::ServiceAlreadyMonitored { name }) => (SERVICE_ALREADY_MONITORED, "ServiceAlreadyMonitored", format!("service '{}' already monitored", name)),
53-
Some(ZInitError::ServiceIsUp { name }) => (SERVICE_IS_UP, "ServiceIsUp", format!("service '{}' is up", name)),
54-
Some(ZInitError::ServiceIsDown { name }) => (SERVICE_IS_DOWN, "ServiceIsDown", format!("service '{}' is down", name)),
55-
Some(ZInitError::ShuttingDown) => (SHUTTING_DOWN, "ShuttingDown", "system is shutting down".to_string()),
56-
Some(ZInitError::InvalidStateTransition { message }) => (-32009, "InvalidStateTransition", message.clone()),
57-
Some(ZInitError::DependencyError { message }) => (-32010, "DependencyError", message.clone()),
56+
Some(ZInitError::UnknownService { name }) => (
57+
SERVICE_NOT_FOUND,
58+
"ServiceNotFound",
59+
format!("service '{}' not found", name),
60+
),
61+
Some(ZInitError::ServiceAlreadyMonitored { name }) => (
62+
SERVICE_ALREADY_MONITORED,
63+
"ServiceAlreadyMonitored",
64+
format!("service '{}' already monitored", name),
65+
),
66+
Some(ZInitError::ServiceIsUp { name }) => (
67+
SERVICE_IS_UP,
68+
"ServiceIsUp",
69+
format!("service '{}' is up", name),
70+
),
71+
Some(ZInitError::ServiceIsDown { name }) => (
72+
SERVICE_IS_DOWN,
73+
"ServiceIsDown",
74+
format!("service '{}' is down", name),
75+
),
76+
Some(ZInitError::ShuttingDown) => (
77+
SHUTTING_DOWN,
78+
"ShuttingDown",
79+
"system is shutting down".to_string(),
80+
),
81+
Some(ZInitError::InvalidStateTransition { message }) => {
82+
(-32009, "InvalidStateTransition", message.clone())
83+
}
84+
Some(ZInitError::DependencyError { message }) => {
85+
(-32010, "DependencyError", message.clone())
86+
}
5887
Some(ZInitError::ProcessError { message }) => (-32011, "ProcessError", message.clone()),
5988
None => (default_code, code_name_from(default_code), err.to_string()),
6089
};
@@ -199,30 +228,34 @@ pub trait ZinitServiceApi {
199228
#[async_trait]
200229
impl ZinitServiceApiServer for Api {
201230
async fn list(&self) -> RpcResult<HashMap<String, String>> {
202-
let services = self
203-
.zinit
204-
.list()
205-
.await
206-
.map_err(|e| zinit_err_to_rpc(e, "listing services", None, ErrorCode::InternalError.code()))?;
231+
let services = self.zinit.list().await.map_err(|e| {
232+
zinit_err_to_rpc(e, "listing services", None, ErrorCode::InternalError.code())
233+
})?;
207234

208235
let mut map: HashMap<String, String> = HashMap::new();
209236
for service in services {
210-
let state = self
211-
.zinit
212-
.status(&service)
213-
.await
214-
.map_err(|e| zinit_err_to_rpc(e, "getting status", Some(&service), ErrorCode::InternalError.code()))?;
237+
let state = self.zinit.status(&service).await.map_err(|e| {
238+
zinit_err_to_rpc(
239+
e,
240+
"getting status",
241+
Some(&service),
242+
ErrorCode::InternalError.code(),
243+
)
244+
})?;
215245
map.insert(service, format!("{:?}", state.state));
216246
}
217247
Ok(map)
218248
}
219249

220250
async fn status(&self, name: String) -> RpcResult<Status> {
221-
let status = self
222-
.zinit
223-
.status(&name)
224-
.await
225-
.map_err(|e| zinit_err_to_rpc(e, "getting status", Some(&name), ErrorCode::InternalError.code()))?;
251+
let status = self.zinit.status(&name).await.map_err(|e| {
252+
zinit_err_to_rpc(
253+
e,
254+
"getting status",
255+
Some(&name),
256+
ErrorCode::InternalError.code(),
257+
)
258+
})?;
226259

227260
let result = Status {
228261
name: name.clone(),
@@ -246,17 +279,25 @@ impl ZinitServiceApiServer for Api {
246279
}
247280

248281
async fn start(&self, name: String) -> RpcResult<()> {
249-
self.zinit
250-
.start(name.clone())
251-
.await
252-
.map_err(|e| zinit_err_to_rpc(e, "starting service", Some(&name), ErrorCode::InternalError.code()))
282+
self.zinit.start(name.clone()).await.map_err(|e| {
283+
zinit_err_to_rpc(
284+
e,
285+
"starting service",
286+
Some(&name),
287+
ErrorCode::InternalError.code(),
288+
)
289+
})
253290
}
254291

255292
async fn stop(&self, name: String) -> RpcResult<()> {
256-
self.zinit
257-
.stop(name.clone())
258-
.await
259-
.map_err(|e| zinit_err_to_rpc(e, "stopping service", Some(&name), ErrorCode::InternalError.code()))
293+
self.zinit.stop(name.clone()).await.map_err(|e| {
294+
zinit_err_to_rpc(
295+
e,
296+
"stopping service",
297+
Some(&name),
298+
ErrorCode::InternalError.code(),
299+
)
300+
})
260301
}
261302

262303
async fn monitor(&self, name: String) -> RpcResult<()> {
@@ -265,39 +306,53 @@ impl ZinitServiceApiServer for Api {
265306
return Err(invalid_service_name_error("monitoring service", &name));
266307
}
267308

268-
let (name_str, service) = config::load(format!("{}.yaml", name))
269-
.map_err(|e| {
270-
ErrorObjectOwned::owned(
271-
CONFIG_ERROR,
272-
format!("monitoring service: failed to load '{}.yaml'", name),
273-
Some(json!({
274-
"code_name": "ConfigError",
275-
"action": "loading service config",
276-
"service": name,
277-
"hint": "Verify the YAML file exists and is valid"
278-
})),
279-
)
280-
})?;
309+
let (name_str, service) = config::load(format!("{}.yaml", name)).map_err(|e| {
310+
ErrorObjectOwned::owned(
311+
CONFIG_ERROR,
312+
format!("monitoring service: failed to load '{}.yaml'", name),
313+
Some(json!({
314+
"code_name": "ConfigError",
315+
"action": "loading service config",
316+
"service": name,
317+
"hint": "Verify the YAML file exists and is valid"
318+
})),
319+
)
320+
})?;
281321

282322
self.zinit
283323
.monitor(name_str.clone(), service)
284324
.await
285-
.map_err(|e| zinit_err_to_rpc(e, "monitoring service", Some(&name_str), ErrorCode::InternalError.code()))
325+
.map_err(|e| {
326+
zinit_err_to_rpc(
327+
e,
328+
"monitoring service",
329+
Some(&name_str),
330+
ErrorCode::InternalError.code(),
331+
)
332+
})
286333
}
287334

288335
async fn forget(&self, name: String) -> RpcResult<()> {
289-
self.zinit
290-
.forget(name.clone())
291-
.await
292-
.map_err(|e| zinit_err_to_rpc(e, "forgetting service", Some(&name), ErrorCode::InternalError.code()))
336+
self.zinit.forget(name.clone()).await.map_err(|e| {
337+
zinit_err_to_rpc(
338+
e,
339+
"forgetting service",
340+
Some(&name),
341+
ErrorCode::InternalError.code(),
342+
)
343+
})
293344
}
294345

295346
async fn kill(&self, name: String, signal: String) -> RpcResult<()> {
296347
if let Ok(sig) = nix::sys::signal::Signal::from_str(&signal.to_uppercase()) {
297-
self.zinit
298-
.kill(name.clone(), sig)
299-
.await
300-
.map_err(|e| zinit_err_to_rpc(e, "sending signal", Some(&name), ErrorCode::InternalError.code()))
348+
self.zinit.kill(name.clone(), sig).await.map_err(|e| {
349+
zinit_err_to_rpc(
350+
e,
351+
"sending signal",
352+
Some(&name),
353+
ErrorCode::InternalError.code(),
354+
)
355+
})
301356
} else {
302357
Err(invalid_signal_error(&signal))
303358
}
@@ -345,11 +400,21 @@ impl ZinitServiceApiServer for Api {
345400
})?;
346401

347402
// Write the YAML content to the file
348-
let mut file = fs::File::create(&file_path)
349-
.map_err(|_| service_file_error("creating service file", &name, "failed to create service file"))?;
403+
let mut file = fs::File::create(&file_path).map_err(|_| {
404+
service_file_error(
405+
"creating service file",
406+
&name,
407+
"failed to create service file",
408+
)
409+
})?;
350410

351-
file.write_all(yaml_content.as_bytes())
352-
.map_err(|_| service_file_error("writing service file", &name, "failed to write service file"))?;
411+
file.write_all(yaml_content.as_bytes()).map_err(|_| {
412+
service_file_error(
413+
"writing service file",
414+
&name,
415+
"failed to write service file",
416+
)
417+
})?;
353418

354419
Ok(format!("Service '{}' created successfully", name))
355420
}
@@ -381,8 +446,13 @@ impl ZinitServiceApiServer for Api {
381446
}
382447

383448
// Delete the file
384-
fs::remove_file(&file_path)
385-
.map_err(|_| service_file_error("deleting service file", &name, "failed to delete service file"))?;
449+
fs::remove_file(&file_path).map_err(|_| {
450+
service_file_error(
451+
"deleting service file",
452+
&name,
453+
"failed to delete service file",
454+
)
455+
})?;
386456

387457
Ok(format!("Service '{}' deleted successfully", name))
388458
}
@@ -414,8 +484,9 @@ impl ZinitServiceApiServer for Api {
414484
}
415485

416486
// Read the file content
417-
let yaml_content = fs::read_to_string(&file_path)
418-
.map_err(|_| service_file_error("reading service file", &name, "failed to read service file"))?;
487+
let yaml_content = fs::read_to_string(&file_path).map_err(|_| {
488+
service_file_error("reading service file", &name, "failed to read service file")
489+
})?;
419490

420491
// Parse YAML to JSON
421492
let yaml_value: serde_yaml::Value = serde_yaml::from_str(&yaml_content).map_err(|_| {
@@ -448,11 +519,14 @@ impl ZinitServiceApiServer for Api {
448519
}
449520

450521
async fn stats(&self, name: String) -> RpcResult<Stats> {
451-
let stats = self
452-
.zinit
453-
.stats(&name)
454-
.await
455-
.map_err(|e| zinit_err_to_rpc(e, "collecting stats", Some(&name), ErrorCode::InternalError.code()))?;
522+
let stats = self.zinit.stats(&name).await.map_err(|e| {
523+
zinit_err_to_rpc(
524+
e,
525+
"collecting stats",
526+
Some(&name),
527+
ErrorCode::InternalError.code(),
528+
)
529+
})?;
456530

457531
let result = Stats {
458532
name: name.clone(),
@@ -504,25 +578,34 @@ impl ZinitSystemApiServer for Api {
504578
}
505579

506580
async fn reboot(&self) -> RpcResult<()> {
507-
self.zinit
508-
.reboot()
509-
.await
510-
.map_err(|e| zinit_err_to_rpc(e, "system reboot", None, ErrorCode::InternalError.code()))
581+
self.zinit.reboot().await.map_err(|e| {
582+
zinit_err_to_rpc(e, "system reboot", None, ErrorCode::InternalError.code())
583+
})
511584
}
512585

513586
async fn start_http_server(&self, address: String) -> RpcResult<String> {
514587
// Call the method from the API implementation
515588
match crate::app::api::Api::start_http_server(self, address).await {
516589
Ok(result) => Ok(result),
517-
Err(e) => Err(zinit_err_to_rpc(AnyError::from(e), "starting http server", None, ErrorCode::InternalError.code())),
590+
Err(e) => Err(zinit_err_to_rpc(
591+
AnyError::from(e),
592+
"starting http server",
593+
None,
594+
ErrorCode::InternalError.code(),
595+
)),
518596
}
519597
}
520598

521599
async fn stop_http_server(&self) -> RpcResult<()> {
522600
// Call the method from the API implementation
523601
match crate::app::api::Api::stop_http_server(self).await {
524602
Ok(_) => Ok(()),
525-
Err(e) => Err(zinit_err_to_rpc(AnyError::from(e), "stopping http server", None, ErrorCode::InternalError.code())),
603+
Err(e) => Err(zinit_err_to_rpc(
604+
AnyError::from(e),
605+
"stopping http server",
606+
None,
607+
ErrorCode::InternalError.code(),
608+
)),
526609
}
527610
}
528611
}

zinit-client/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,16 @@ impl From<RpcError> for ClientError {
8888

8989
let human = match (service, action, hint) {
9090
(Some(s), Some(a), Some(h)) => {
91-
format!("{}[{}]: {} while {} '{}'. Hint: {}. Details: {:?}", code_name, code, message, a, s, h, chain)
91+
format!(
92+
"{}[{}]: {} while {} '{}'. Hint: {}. Details: {:?}",
93+
code_name, code, message, a, s, h, chain
94+
)
9295
}
9396
(Some(s), Some(a), None) => {
94-
format!("{}[{}]: {} while {} '{}'. Details: {:?}", code_name, code, message, a, s, chain)
97+
format!(
98+
"{}[{}]: {} while {} '{}'. Details: {:?}",
99+
code_name, code, message, a, s, chain
100+
)
95101
}
96102
_ => {
97103
format!("{}[{}]: {}. Details: {:?}", code_name, code, message, chain)

0 commit comments

Comments
 (0)