git.delta.rocks / remowt / refs/commits / 751b8f1ca8aa

difftreelog

source

crates/remowt-endpoints/src/systemd.rs1.3 KiBsourcehistory
1use bifrostlink::declarative::endpoints;2use bifrostlink::Config;3use serde::{Deserialize, Serialize};4use zbus::proxy;5use zbus::zvariant::OwnedObjectPath;67pub struct Systemd;89#[derive(Serialize, Deserialize, Debug, thiserror::Error)]10pub enum Error {11	#[error("systemd request failed: {0}")]12	Failed(String),13}1415#[proxy(16	interface = "org.freedesktop.systemd1.Manager",17	default_service = "org.freedesktop.systemd1",18	default_path = "/org/freedesktop/systemd1"19)]20trait Manager {21	fn start_unit(&self, name: &str, mode: &str) -> zbus::Result<OwnedObjectPath>;22	fn stop_unit(&self, name: &str, mode: &str) -> zbus::Result<OwnedObjectPath>;23}2425async fn manager() -> Result<ManagerProxy<'static>, Error> {26	let conn = zbus::Connection::system()27		.await28		.map_err(|e| Error::Failed(e.to_string()))?;29	ManagerProxy::new(&conn)30		.await31		.map_err(|e| Error::Failed(e.to_string()))32}3334#[endpoints(ns = 5)]35impl Systemd {36	#[endpoints(id = 1)]37	async fn start(&self, unit: String) -> Result<(), Error> {38		manager()39			.await?40			.start_unit(&unit, "replace")41			.await42			.map_err(|e| Error::Failed(e.to_string()))?;43		Ok(())44	}45	#[endpoints(id = 2)]46	async fn stop(&self, unit: String) -> Result<(), Error> {47		manager()48			.await?49			.stop_unit(&unit, "replace")50			.await51			.map_err(|e| Error::Failed(e.to_string()))?;52		Ok(())53	}54}