Replace common functionality with rust implementation

This commit is contained in:
2025-09-03 17:06:13 +02:00
parent d0d162f3e9
commit d31d39473b
38 changed files with 2871 additions and 204 deletions

87
mgr/src/bin/client.rs Normal file
View File

@@ -0,0 +1,87 @@
#![expect(
clippy::print_stderr,
clippy::print_stdout,
reason = "output is fine for cli"
)]
use std::{
env,
io::{self, Read as _},
net,
os::unix::net::UnixStream,
process, str,
};
use thiserror::Error;
use mgr::{
Action,
cli::{self, CliCommand as _, ParseError},
wire::{client, socket},
};
#[derive(Debug, Error)]
enum Error {
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Socket(#[from] socket::Error),
#[error(transparent)]
Send(#[from] client::SendError),
#[error(transparent)]
CliParse(#[from] cli::ParseError),
#[error("response is not valid utf8: {0}")]
ResponseNonUtf8(#[from] str::Utf8Error),
}
enum MainResult {
Success,
Failure(Error),
}
impl process::Termination for MainResult {
fn report(self) -> process::ExitCode {
match self {
Self::Success => process::ExitCode::SUCCESS,
Self::Failure(e) => {
eprintln!("Error: {e}");
process::ExitCode::FAILURE
}
}
}
}
fn main() -> MainResult {
fn inner() -> Result<(), Error> {
let mut args = env::args().skip(1);
let socket = socket::get_socket_path()?;
let mut stream = UnixStream::connect(socket)?;
let action =
Action::parse_str(args.next().ok_or(ParseError::MissingAction)?.as_str(), args)?;
action.send(&mut stream)?;
stream.shutdown(net::Shutdown::Write)?;
let response = {
let mut buf = Vec::new();
stream.read_to_end(&mut buf)?;
let response = str::from_utf8(&buf)?.to_owned();
drop(stream);
response
};
if !response.is_empty() {
println!("{response}");
}
Ok(())
}
match inner() {
Ok(()) => MainResult::Success,
Err(e) => MainResult::Failure(e),
}
}

105
mgr/src/bin/main.rs Executable file
View File

@@ -0,0 +1,105 @@
#![expect(
clippy::print_stderr,
clippy::print_stdout,
reason = "output is fine for cli"
)]
use std::{env, process};
use thiserror::Error;
use tracing::Level;
use mgr::{
self, Action, Exec as _,
cli::{CliCommand as _, ParseError},
};
#[derive(Debug, Error)]
enum Error {
#[error(transparent)]
Power(#[from] mgr::power::Error),
#[error(transparent)]
Dmenu(#[from] mgr::dmenu::Error),
#[error(transparent)]
Server(#[from] mgr::wire::server::Error),
#[error(transparent)]
Presentation(#[from] mgr::present::Error),
#[error(transparent)]
Exec(#[from] mgr::ExecError),
#[error(transparent)]
ParseParse(#[from] ParseError),
#[error(transparent)]
Tracing(#[from] tracing::dispatcher::SetGlobalDefaultError),
}
enum MainResult {
Success,
Failure(Error),
}
impl process::Termination for MainResult {
fn report(self) -> process::ExitCode {
match self {
Self::Success => process::ExitCode::SUCCESS,
Self::Failure(e) => {
eprintln!("Error: {e}");
process::ExitCode::FAILURE
}
}
}
}
impl From<Error> for MainResult {
fn from(value: Error) -> Self {
Self::Failure(value)
}
}
fn init_tracing() -> Result<(), Error> {
tracing::subscriber::set_global_default(
tracing_subscriber::fmt()
.with_max_level(Level::DEBUG)
.event_format(
tracing_subscriber::fmt::format()
.with_ansi(false)
.with_target(false)
.compact(),
)
.finish(),
)?;
Ok(())
}
fn main() -> MainResult {
fn inner() -> Result<(), Error> {
init_tracing()?;
let mut args = env::args().skip(1);
match args.next().ok_or(ParseError::MissingAction)?.as_str() {
"serve" => {
mgr::wire::server::run()?;
Ok(())
}
"run" => {
let action = Action::parse_str(
args.next().ok_or(ParseError::MissingAction)?.as_str(),
args,
)?;
if let Some(output) = action.execute()? {
println!("{output}");
}
Ok(())
}
input => Err(ParseError::UnknownAction {
action: input.to_owned(),
}
.into()),
}
}
match inner() {
Ok(()) => MainResult::Success,
Err(e) => MainResult::Failure(e),
}
}

77
mgr/src/brightness.rs Normal file
View File

@@ -0,0 +1,77 @@
use thiserror::Error;
use super::{
Exec,
cli::{self, CliCommand},
cmd,
wire::{WireCommand, server},
};
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Cli(#[from] cli::ParseError),
#[error(transparent)]
Cmd(#[from] cmd::Error),
}
#[derive(Debug, Clone, Copy)]
pub enum Action {
Inc,
Dec,
}
impl WireCommand for Action {
fn parse_wire(mut input: impl Iterator<Item = u8>) -> Result<Self, server::ParseError> {
match input.next().ok_or(server::ParseError::Eof)? {
0x01 => Ok(Self::Inc),
0x02 => Ok(Self::Dec),
byte => Err(server::ParseError::Unknown(byte)),
}
}
fn to_wire(&self) -> Vec<u8> {
match *self {
Self::Inc => vec![0x01],
Self::Dec => vec![0x02],
}
}
}
impl CliCommand for Action {
type ExecErr = Error;
fn parse_str(input: &str, rest: impl Iterator<Item = String>) -> Result<Self, cli::ParseError>
where
Self: Sized,
{
let result = match input {
"inc" => Self::Inc,
"dec" => Self::Dec,
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
};
let rest = rest.collect::<Vec<String>>();
if rest.is_empty() {
Ok(result)
} else {
Err(cli::ParseError::UnexpectedInput { rest })
}
}
}
impl Exec for Action {
type ExecErr = Error;
fn execute(&self) -> Result<Option<String>, Self::ExecErr> {
match *self {
Self::Inc => cmd::command("brightnessctl", &["set", "8%+"])?,
Self::Dec => cmd::command("brightnessctl", &["set", "8%-"])?,
}
Ok(None)
}
}

19
mgr/src/cli.rs Normal file
View File

@@ -0,0 +1,19 @@
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ParseError {
#[error("no action given")]
MissingAction,
#[error("unknown action: {action}")]
UnknownAction { action: String },
#[error("unexpected input: {rest:?}")]
UnexpectedInput { rest: Vec<String> },
}
pub trait CliCommand {
type ExecErr: From<ParseError>;
fn parse_str(input: &str, rest: impl Iterator<Item = String>) -> Result<Self, ParseError>
where
Self: Sized;
}

183
mgr/src/cmd.rs Normal file
View File

@@ -0,0 +1,183 @@
use std::{io, panic, process, str, thread};
use thiserror::Error;
use tracing::{Level, event};
#[derive(Error, Debug)]
pub enum Error {
#[error("command \"{command}\" failed: {error}")]
CommandInvocation {
command: &'static str,
error: io::Error,
},
#[error("command \"{command}\" was terminated by signal")]
CommandTerminatedBySignal { command: &'static str },
#[error(
"command \"{command}\" failed [{code}]: {stderr}",
code = match *.code {
Some(code) => &.code.to_string(),
_ => "unknown exit code",
},
stderr = if .stderr.is_empty() {
"[stderr empty]"
} else {
.stderr
})]
CommandFailed {
command: &'static str,
code: Option<i32>,
stderr: String,
},
#[error("{command} produced non-utf8 output: {error}")]
CommandOutputNonUtf8 {
command: &'static str,
error: str::Utf8Error,
},
#[error("failed writing to stdin of command \"{command}\": {error}")]
StdinWriteFailed {
command: &'static str,
error: io::Error,
},
}
pub(crate) fn command(command: &'static str, args: &[&str]) -> Result<(), Error> {
let _: FinishedProcess = run_command_checked(command, args)?;
Ok(())
}
pub(crate) fn run_command(command: &'static str, args: &[&str]) -> Result<FinishedProcess, Error> {
event!(Level::DEBUG, "running {command} {args:?}");
let proc = process::Command::new(command)
.args(args)
.output()
.map_err(|error| Error::CommandInvocation { command, error })?;
Ok(FinishedProcess {
exit_code: proc
.status
.code()
.ok_or(Error::CommandTerminatedBySignal { command })?,
stdout: str::from_utf8(&proc.stdout)
.map_err(|error| Error::CommandOutputNonUtf8 { command, error })?
.to_owned(),
stderr: str::from_utf8(&proc.stderr)
.map_err(|error| Error::CommandOutputNonUtf8 { command, error })?
.to_owned(),
})
}
pub(crate) fn run_command_checked(
command: &'static str,
args: &[&str],
) -> Result<FinishedProcess, Error> {
let output = run_command(command, args)?;
if output.exit_code != 0_i32 {
event!(Level::DEBUG, "{command} {args:?} failed");
return Err(Error::CommandFailed {
command,
code: Some(output.exit_code),
stderr: output.stderr,
});
}
Ok(output)
}
pub(crate) fn command_output(command: &'static str, args: &[&str]) -> Result<String, Error> {
let output = run_command_checked(command, args)?;
Ok(output.stdout)
}
#[derive(Debug)]
pub(crate) struct FinishedProcess {
pub exit_code: i32,
pub stdout: String,
pub stderr: String,
}
pub(crate) fn command_output_with_stdin_write(
command: &'static str,
args: &[&str],
input: &[u8],
) -> Result<FinishedProcess, Error> {
use io::Write as _;
let process = process::Command::new(command)
.args(args)
.stdin(process::Stdio::piped())
.stdout(process::Stdio::piped())
.stderr(process::Stdio::null())
.spawn()
.map_err(|error| Error::CommandInvocation { command, error })?;
let mut stdin = process
.stdin
.as_ref()
.expect("stdin handle must be present");
stdin
.write_all(input)
.map_err(|error| Error::StdinWriteFailed { command, error })?;
let output = process
.wait_with_output()
.map_err(|error| Error::CommandInvocation { command, error })?;
let exit_code = output
.status
.code()
.ok_or(Error::CommandTerminatedBySignal { command })?;
let stdout = str::from_utf8(&output.stdout)
.map_err(|error| Error::CommandOutputNonUtf8 { command, error })?
.to_owned();
let stderr = str::from_utf8(&output.stderr)
.map_err(|error| Error::CommandOutputNonUtf8 { command, error })?
.to_owned();
Ok(FinishedProcess {
exit_code,
stdout,
stderr,
})
}
pub(crate) struct RunningProcess {
command: &'static str,
join_handle: thread::JoinHandle<Result<FinishedProcess, Error>>,
}
impl RunningProcess {
pub fn with<F: Fn() -> Result<(), E>, E: From<Error>>(
self,
f: F,
) -> Result<FinishedProcess, E> {
f()?;
event!(
Level::DEBUG,
"waiting for process {} to finish",
self.command
);
let ret = match self.join_handle.join() {
Ok(ret) => ret?,
Err(e) => panic::resume_unwind(e),
};
event!(Level::DEBUG, "process {} finished", self.command);
Ok(ret)
}
}
pub(crate) fn start_command(
command: &'static str,
args: &'static [&'static str],
) -> RunningProcess {
event!(Level::DEBUG, "starting {command} {args:?}");
let join_handle = thread::spawn(move || run_command_checked(command, args));
RunningProcess {
command,
join_handle,
}
}

29
mgr/src/dirs.rs Normal file
View File

@@ -0,0 +1,29 @@
use std::path::PathBuf;
use thiserror::Error;
use super::env;
const ENV_XDG_RUNTIME_DIR: &str = "XDG_RUNTIME_DIR";
const ENV_XDG_CACHE_DIR: &str = "XDG_CACHE_HOME";
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Env(#[from] env::Error),
}
pub(crate) fn xdg_runtime_dir() -> Result<Option<PathBuf>, Error> {
Ok(env::get(ENV_XDG_RUNTIME_DIR)?.map(PathBuf::from))
}
pub(crate) fn require_xdg_runtime_dir() -> Result<PathBuf, Error> {
Ok(PathBuf::from(env::require(ENV_XDG_RUNTIME_DIR)?))
}
pub(crate) fn xdg_cache_dir() -> Result<PathBuf, Error> {
Ok(match env::get(ENV_XDG_CACHE_DIR)? {
Some(value) => PathBuf::from(value),
None => PathBuf::from(env::require("HOME")?).join(".cache"),
})
}

66
mgr/src/dmenu.rs Normal file
View File

@@ -0,0 +1,66 @@
use std::{fmt::Write as _, num};
use thiserror::Error;
use tracing::{Level, event};
use super::cmd;
#[derive(Debug, Error)]
pub enum Error {
#[error("rofi did not return an integer: {error}")]
RofiNonIntOutput { error: num::ParseIntError },
#[error("rofi returned an invalid indexx: {index}")]
RofiInvalidIndex { index: usize },
#[error(transparent)]
Cmd(#[from] cmd::Error),
}
pub(crate) fn get_choice(actions: &[&'static str]) -> Result<Option<&'static str>, Error> {
const ROFI: &str = "rofi";
event!(Level::DEBUG, "starting rofi");
let process = cmd::command_output_with_stdin_write(
ROFI,
&[
"-dmenu",
"-p",
"action",
"-l",
&actions.len().to_string(),
"-no-custom",
"-sync",
"-format",
"i",
],
actions
.iter()
.enumerate()
.fold(String::new(), |mut output, (i, action)| {
writeln!(
output,
"({i}) {action}",
i = i.checked_add(1).expect("too many action")
)
.expect("writing to string cannot fail");
output
})
.as_bytes(),
)?;
if process.exit_code == 1 {
Ok(None)
} else {
let choice = process
.stdout
.trim()
.parse::<usize>()
.map_err(|error| Error::RofiNonIntOutput { error })?;
Ok(Some(
actions
.get(choice)
.ok_or(Error::RofiInvalidIndex { index: choice })?,
))
}
}

40
mgr/src/dunst.rs Normal file
View File

@@ -0,0 +1,40 @@
use thiserror::Error;
use super::cmd;
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Cmd(#[from] cmd::Error),
#[error("dunstctl is-paused returned unknown output: {output}")]
DunstctlIsPausedUnknownOutput { output: String },
}
#[derive(Clone, Copy)]
pub(crate) enum Status {
Paused,
Unpaused,
}
pub(crate) fn set_status(status: Status) -> Result<(), Error> {
Ok(cmd::command(
"dunstctl",
&[
"set-paused",
match status {
Status::Paused => "true",
Status::Unpaused => "false",
},
],
)?)
}
pub(crate) fn is_paused() -> Result<bool, Error> {
let output = cmd::command_output("dunstctl", &["is-paused"])?;
match output.trim() {
"true" => Ok(true),
"false" => Ok(false),
_ => Err(Error::DunstctlIsPausedUnknownOutput { output }),
}
}

28
mgr/src/env.rs Normal file
View File

@@ -0,0 +1,28 @@
use std::{env, ffi::OsString};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error(
"env variable \"{name}\" is not valid unicode: \"{value}\"",
value = value.to_string_lossy()
)]
EnvNotUnicode { name: &'static str, value: OsString },
#[error("env variable \"{name}\" not found")]
EnvNotFound { name: &'static str },
}
pub(crate) fn get(var: &'static str) -> Result<Option<String>, Error> {
match env::var(var) {
Ok(value) => Ok(Some(value)),
Err(e) => match e {
env::VarError::NotPresent => Ok(None),
env::VarError::NotUnicode(value) => Err(Error::EnvNotUnicode { name: var, value }),
},
}
}
pub(crate) fn require(var: &'static str) -> Result<String, Error> {
get(var)?.ok_or(Error::EnvNotFound { name: var })
}

190
mgr/src/lib.rs Normal file
View File

@@ -0,0 +1,190 @@
use thiserror::Error;
pub(crate) mod brightness;
pub mod cli;
pub(crate) mod cmd;
pub(crate) mod dirs;
pub mod dmenu;
pub(crate) mod dunst;
pub(crate) mod env;
pub mod power;
pub mod present;
pub(crate) mod pulseaudio;
pub(crate) mod redshift;
pub(crate) mod spotify;
pub(crate) mod systemd;
pub(crate) mod theme;
pub(crate) mod weather;
pub mod wire;
#[derive(Debug, Error)]
pub enum ExecError {
#[error(transparent)]
Power(#[from] power::Error),
#[error(transparent)]
Presentation(#[from] present::Error),
#[error(transparent)]
Pulseaudio(#[from] pulseaudio::Error),
#[error(transparent)]
Theme(#[from] theme::Error),
#[error(transparent)]
Spotify(#[from] spotify::Error),
#[error(transparent)]
Redshift(#[from] redshift::Error),
#[error(transparent)]
Weather(#[from] weather::Error),
#[error(transparent)]
Brightness(#[from] brightness::Error),
#[error(transparent)]
Parse(#[from] cli::ParseError),
}
#[derive(Debug)]
pub enum Action {
Power(power::Action),
Present(present::Action),
Pulseaudio(pulseaudio::Action),
Theme(theme::Action),
Spotify(spotify::Action),
Redshift(redshift::Action),
Weather(weather::Action),
Brightness(brightness::Action),
}
impl wire::WireCommand for Action {
fn parse_wire(mut input: impl Iterator<Item = u8>) -> Result<Self, wire::server::ParseError> {
match input.next().ok_or(wire::server::ParseError::Eof)? {
0x01 => Ok(Self::Power(power::Action::parse_wire(input)?)),
0x02 => Ok(Self::Present(present::Action::parse_wire(input)?)),
0x03 => Ok(Self::Pulseaudio(pulseaudio::Action::parse_wire(input)?)),
0x04 => Ok(Self::Theme(theme::Action::parse_wire(input)?)),
0x05 => Ok(Self::Spotify(spotify::Action::parse_wire(input)?)),
0x06 => Ok(Self::Redshift(redshift::Action::parse_wire(input)?)),
0x07 => Ok(Self::Weather(weather::Action::parse_wire(input)?)),
0x08 => Ok(Self::Brightness(brightness::Action::parse_wire(input)?)),
other => Err(wire::server::ParseError::Unknown(other)),
}
}
fn to_wire(&self) -> Vec<u8> {
match *self {
Self::Power(action) => {
let mut v = vec![0x01];
v.extend_from_slice(&action.to_wire());
v
}
Self::Present(action) => {
let mut v = vec![0x02];
v.extend_from_slice(&action.to_wire());
v
}
Self::Pulseaudio(action) => {
let mut v = vec![0x03];
v.extend_from_slice(&action.to_wire());
v
}
Self::Theme(action) => {
let mut v = vec![0x04];
v.extend_from_slice(&action.to_wire());
v
}
Self::Spotify(action) => {
let mut v = vec![0x05];
v.extend_from_slice(&action.to_wire());
v
}
Self::Redshift(action) => {
let mut v = vec![0x06];
v.extend_from_slice(&action.to_wire());
v
}
Self::Weather(action) => {
let mut v = vec![0x07];
v.extend_from_slice(&action.to_wire());
v
}
Self::Brightness(action) => {
let mut v = vec![0x08];
v.extend_from_slice(&action.to_wire());
v
}
}
}
}
impl cli::CliCommand for Action {
type ExecErr = ExecError;
fn parse_str(
input: &str,
mut rest: impl Iterator<Item = String>,
) -> Result<Self, cli::ParseError>
where
Self: Sized,
{
match input {
"power" => {
let choice = rest.next().ok_or(cli::ParseError::MissingAction)?;
Ok(Self::Power(power::Action::parse_str(&choice, rest)?))
}
"present" => {
let choice = rest.next().ok_or(cli::ParseError::MissingAction)?;
Ok(Self::Present(present::Action::parse_str(&choice, rest)?))
}
"pulseaudio" => {
let choice = rest.next().ok_or(cli::ParseError::MissingAction)?;
Ok(Self::Pulseaudio(pulseaudio::Action::parse_str(
&choice, rest,
)?))
}
"theme" => {
let choice = rest.next().ok_or(cli::ParseError::MissingAction)?;
Ok(Self::Theme(theme::Action::parse_str(&choice, rest)?))
}
"spotify" => {
let choice = rest.next().ok_or(cli::ParseError::MissingAction)?;
Ok(Self::Spotify(spotify::Action::parse_str(&choice, rest)?))
}
"redshift" => {
let choice = rest.next().ok_or(cli::ParseError::MissingAction)?;
Ok(Self::Redshift(redshift::Action::parse_str(&choice, rest)?))
}
"weather" => {
let choice = rest.next().ok_or(cli::ParseError::MissingAction)?;
Ok(Self::Weather(weather::Action::parse_str(&choice, rest)?))
}
"brightness" => {
let choice = rest.next().ok_or(cli::ParseError::MissingAction)?;
Ok(Self::Brightness(brightness::Action::parse_str(
&choice, rest,
)?))
}
s => Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
}),
}
}
}
pub trait Exec {
type ExecErr: Into<ExecError>;
fn execute(&self) -> Result<Option<String>, Self::ExecErr>;
}
impl Exec for Action {
type ExecErr = ExecError;
fn execute(&self) -> Result<Option<String>, Self::ExecErr> {
match *self {
Self::Power(action) => Ok(action.execute()?),
Self::Present(action) => Ok(action.execute()?),
Self::Pulseaudio(action) => Ok(action.execute()?),
Self::Theme(action) => Ok(action.execute()?),
Self::Spotify(action) => Ok(action.execute()?),
Self::Redshift(action) => Ok(action.execute()?),
Self::Weather(action) => Ok(action.execute()?),
Self::Brightness(action) => Ok(action.execute()?),
}
}
}

223
mgr/src/power.rs Normal file
View File

@@ -0,0 +1,223 @@
use thiserror::Error;
use tracing::{Level, event};
use super::{
Exec,
cli::{self, CliCommand},
cmd, dmenu, dunst, spotify,
wire::{WireCommand, server},
};
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Dunst(#[from] dunst::Error),
#[error("unknown action: {action}")]
UnknownAction { action: String },
#[error(transparent)]
Cmd(#[from] cmd::Error),
#[error(transparent)]
Dmenu(#[from] dmenu::Error),
#[error(transparent)]
Cli(#[from] cli::ParseError),
#[error(transparent)]
Spotify(#[from] spotify::Error),
}
#[derive(Debug, Clone, Copy)]
pub enum Action {
Menu,
Lock,
Suspend,
Hibernate,
Reboot,
Poweroff,
}
impl Action {
fn as_str(self) -> &'static str {
match self {
Self::Menu => "menu",
Self::Lock => "lock",
Self::Suspend => "suspend",
Self::Hibernate => "hibernate",
Self::Reboot => "reboot",
Self::Poweroff => "poweroff",
}
}
}
impl WireCommand for Action {
fn parse_wire(mut input: impl Iterator<Item = u8>) -> Result<Self, server::ParseError> {
match input.next().ok_or(server::ParseError::Eof)? {
0x01 => Ok(Self::Menu),
0x02 => Ok(Self::Lock),
0x03 => Ok(Self::Suspend),
0x04 => Ok(Self::Hibernate),
0x05 => Ok(Self::Reboot),
0x06 => Ok(Self::Poweroff),
byte => Err(server::ParseError::Unknown(byte)),
}
}
fn to_wire(&self) -> Vec<u8> {
match *self {
Self::Menu => vec![0x01],
Self::Lock => vec![0x02],
Self::Suspend => vec![0x03],
Self::Hibernate => vec![0x04],
Self::Reboot => vec![0x05],
Self::Poweroff => vec![0x06],
}
}
}
impl Exec for Action {
type ExecErr = Error;
fn execute(&self) -> Result<Option<String>, Self::ExecErr> {
match *self {
Self::Menu => menu()?,
Self::Lock => lock_and_screen_off()?,
Self::Suspend => lock_and_suspend()?,
Self::Hibernate => hibernate()?,
Self::Reboot => reboot()?,
Self::Poweroff => poweroff()?,
}
Ok(None)
}
}
impl CliCommand for Action {
type ExecErr = Error;
fn parse_str(input: &str, rest: impl Iterator<Item = String>) -> Result<Self, cli::ParseError>
where
Self: Sized,
{
let result = match input {
"menu" => Self::Menu,
"lock" => Self::Lock,
"suspend" => Self::Suspend,
"hibernate" => Self::Hibernate,
"reboot" => Self::Reboot,
"shutdown" => Self::Poweroff,
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
};
let rest = rest.collect::<Vec<String>>();
if rest.is_empty() {
Ok(result)
} else {
Err(cli::ParseError::UnexpectedInput { rest })
}
}
}
const MENU_ACTIONS: &[Action] = &[
Action::Lock,
Action::Suspend,
Action::Hibernate,
Action::Reboot,
Action::Poweroff,
];
fn menu() -> Result<(), Error> {
let choice = dmenu::get_choice(
&MENU_ACTIONS
.iter()
.map(|action| action.as_str())
.collect::<Vec<&str>>(),
)?;
if let Some(choice) = choice {
MENU_ACTIONS
.iter()
.find(|action| action.as_str() == choice)
.copied()
.expect("choice must be one of the valid values")
.execute()?;
} else {
event!(Level::DEBUG, "rofi was cancelled");
}
Ok(())
}
fn screen_off() -> Result<(), Error> {
Ok(cmd::command("xset", &["dpms", "force", "off"])?)
}
fn lock() -> Result<cmd::RunningProcess, Error> {
spotify::pause()?;
let lock_handle = cmd::start_command(
"i3lock",
&[
"--nofork",
"--show-failed-attempts",
"--ignore-empty-password",
"--color",
"000000",
],
);
Ok(lock_handle)
}
fn reset_screen() -> Result<(), Error> {
Ok(cmd::command(
"systemctl",
&["--user", "restart", "dpms.service"],
)?)
}
fn lock_and_screen_off() -> Result<(), Error> {
let dunst_paused = dunst::is_paused()?;
if dunst_paused {
dunst::set_status(dunst::Status::Paused)?;
}
lock()?.with(|| -> Result<(), Error> {
screen_off()?;
Ok(())
})?;
if dunst_paused {
dunst::set_status(dunst::Status::Unpaused)?;
}
reset_screen()?;
Ok(())
}
fn suspend() -> Result<(), Error> {
Ok(cmd::command("systemctl", &["suspend"])?)
}
fn hibernate() -> Result<(), Error> {
Ok(cmd::command("systemctl", &["hibernate"])?)
}
fn reboot() -> Result<(), Error> {
Ok(cmd::command("systemctl", &["reboot"])?)
}
fn poweroff() -> Result<(), Error> {
Ok(cmd::command("systemctl", &["poweroff"])?)
}
fn lock_and_suspend() -> Result<(), Error> {
lock()?.with(|| -> Result<(), Error> {
screen_off()?;
suspend()?;
Ok(())
})?;
Ok(())
}

154
mgr/src/present.rs Normal file
View File

@@ -0,0 +1,154 @@
use std::{fs, io, path::PathBuf};
use thiserror::Error;
use super::{
Exec,
cli::{self, CliCommand},
cmd, dirs, dunst, redshift, spotify,
wire::{WireCommand, server},
};
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Io(#[from] io::Error),
#[error("unknown action: {action}")]
UnknownAction { action: String },
#[error(transparent)]
Cli(#[from] cli::ParseError),
#[error(transparent)]
Dirs(#[from] dirs::Error),
#[error(transparent)]
Dunst(#[from] dunst::Error),
#[error(transparent)]
Cmd(#[from] cmd::Error),
#[error(transparent)]
Redshift(#[from] redshift::Error),
#[error(transparent)]
Spotify(#[from] spotify::Error),
}
#[derive(Debug, Clone, Copy)]
pub enum Action {
On,
Off,
Toggle,
Status,
}
impl WireCommand for Action {
fn parse_wire(mut input: impl Iterator<Item = u8>) -> Result<Self, server::ParseError> {
match input.next().ok_or(server::ParseError::Eof)? {
0x01 => Ok(Self::On),
0x02 => Ok(Self::Off),
0x03 => Ok(Self::Toggle),
0x04 => Ok(Self::Status),
byte => Err(server::ParseError::Unknown(byte)),
}
}
fn to_wire(&self) -> Vec<u8> {
match *self {
Self::On => vec![0x01],
Self::Off => vec![0x02],
Self::Toggle => vec![0x03],
Self::Status => vec![0x04],
}
}
}
fn status_file() -> Result<PathBuf, Error> {
Ok(dirs::require_xdg_runtime_dir()?.join("presentation-mode-on"))
}
#[derive(Debug, Clone, Copy)]
enum Status {
On,
Off,
}
fn status() -> Result<Status, Error> {
Ok(if status_file()?.exists() {
Status::On
} else {
Status::Off
})
}
fn on() -> Result<(), Error> {
drop(fs::File::create(status_file()?)?);
dunst::set_status(dunst::Status::Paused)?;
redshift::set(redshift::Status::Off)?;
spotify::set(spotify::Status::Off)?;
Ok(())
}
fn off() -> Result<(), Error> {
fs::remove_file(status_file()?)?;
dunst::set_status(dunst::Status::Unpaused)?;
redshift::set(redshift::Status::On)?;
spotify::set(spotify::Status::On)?;
Ok(())
}
fn toggle() -> Result<(), Error> {
match status()? {
Status::On => off()?,
Status::Off => on()?,
}
Ok(())
}
impl Exec for Action {
type ExecErr = Error;
fn execute(&self) -> Result<Option<String>, Self::ExecErr> {
match *self {
Self::On => {
on()?;
Ok(None)
}
Self::Off => {
off()?;
Ok(None)
}
Self::Toggle => {
toggle()?;
Ok(None)
}
Self::Status => Ok(match status()? {
Status::On => Some("on".to_owned()),
Status::Off => Some("off".to_owned()),
}),
}
}
}
impl CliCommand for Action {
type ExecErr = Error;
fn parse_str(input: &str, rest: impl Iterator<Item = String>) -> Result<Self, cli::ParseError>
where
Self: Sized,
{
let result = match input {
"on" => Self::On,
"off" => Self::Off,
"toggle" => Self::Toggle,
"status" => Self::Status,
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
};
let rest = rest.collect::<Vec<String>>();
if rest.is_empty() {
Ok(result)
} else {
Err(cli::ParseError::UnexpectedInput { rest })
}
}
}

112
mgr/src/pulseaudio.rs Normal file
View File

@@ -0,0 +1,112 @@
use thiserror::Error;
use super::{
Exec,
cli::{self, CliCommand},
cmd,
wire::{WireCommand, server},
};
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Cli(#[from] cli::ParseError),
#[error(transparent)]
Cmd(#[from] cmd::Error),
}
#[derive(Debug, Clone, Copy)]
pub enum Action {
InputToggle,
OutputToggle,
OutputInc,
OutputDec,
}
impl WireCommand for Action {
fn parse_wire(mut input: impl Iterator<Item = u8>) -> Result<Self, server::ParseError> {
match input.next().ok_or(server::ParseError::Eof)? {
0x01 => Ok(Self::InputToggle),
0x02 => Ok(Self::OutputToggle),
0x03 => Ok(Self::OutputInc),
0x04 => Ok(Self::OutputDec),
byte => Err(server::ParseError::Unknown(byte)),
}
}
fn to_wire(&self) -> Vec<u8> {
match *self {
Self::InputToggle => vec![0x01],
Self::OutputToggle => vec![0x02],
Self::OutputInc => vec![0x03],
Self::OutputDec => vec![0x04],
}
}
}
impl CliCommand for Action {
type ExecErr = Error;
fn parse_str(
input: &str,
mut rest: impl Iterator<Item = String>,
) -> Result<Self, cli::ParseError>
where
Self: Sized,
{
let result = match input {
"input" => match rest.next().ok_or(cli::ParseError::MissingAction)?.as_str() {
"toggle" => Self::InputToggle,
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
},
"output" => match rest.next().ok_or(cli::ParseError::MissingAction)?.as_str() {
"toggle" => Self::OutputToggle,
"inc" => Self::OutputInc,
"dec" => Self::OutputDec,
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
},
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
};
let rest = rest.collect::<Vec<String>>();
if rest.is_empty() {
Ok(result)
} else {
Err(cli::ParseError::UnexpectedInput { rest })
}
}
}
impl Exec for Action {
type ExecErr = Error;
fn execute(&self) -> Result<Option<String>, Self::ExecErr> {
match *self {
Self::InputToggle => {
cmd::command("pactl", &["set-source-mute", "@DEFAULT_SOURCE@", "toggle"])?;
}
Self::OutputToggle => {
cmd::command("pactl", &["set-sink-mute", "@DEFAULT_SINK@", "toggle"])?;
}
Self::OutputInc => {
cmd::command("pactl", &["set-sink-volume", "@DEFAULT_SINK@", "+5%"])?;
}
Self::OutputDec => {
cmd::command("pactl", &["set-sink-volume", "@DEFAULT_SINK@", "-5%"])?;
}
}
Ok(None)
}
}

116
mgr/src/redshift.rs Normal file
View File

@@ -0,0 +1,116 @@
use thiserror::Error;
use super::{
Exec,
cli::{self, CliCommand},
cmd, systemd,
wire::{WireCommand, server},
};
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Cmd(#[from] cmd::Error),
#[error(transparent)]
Cli(#[from] cli::ParseError),
#[error(transparent)]
Systemd(#[from] systemd::Error),
}
#[derive(Debug, Clone, Copy)]
pub enum Action {
Start,
Stop,
Status,
}
impl WireCommand for Action {
fn parse_wire(mut input: impl Iterator<Item = u8>) -> Result<Self, server::ParseError> {
match input.next().ok_or(server::ParseError::Eof)? {
0x01 => Ok(Self::Start),
0x02 => Ok(Self::Stop),
0x03 => Ok(Self::Status),
byte => Err(server::ParseError::Unknown(byte)),
}
}
fn to_wire(&self) -> Vec<u8> {
match *self {
Self::Start => vec![0x01],
Self::Stop => vec![0x02],
Self::Status => vec![0x03],
}
}
}
impl Exec for Action {
type ExecErr = Error;
fn execute(&self) -> Result<Option<String>, Self::ExecErr> {
match *self {
Self::Start => {
set(Status::On)?;
Ok(None)
}
Self::Stop => {
set(Status::Off)?;
Ok(None)
}
Self::Status => Ok(
if systemd::user::unit_status("redshift.service")?.is_active() {
Some("active".to_owned())
} else {
Some("inactive".to_owned())
},
),
}
}
}
impl CliCommand for Action {
type ExecErr = Error;
fn parse_str(input: &str, rest: impl Iterator<Item = String>) -> Result<Self, cli::ParseError>
where
Self: Sized,
{
let result = match input {
"start" => Self::Start,
"stop" => Self::Stop,
"status" => Self::Status,
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
};
let rest = rest.collect::<Vec<String>>();
if rest.is_empty() {
Ok(result)
} else {
Err(cli::ParseError::UnexpectedInput { rest })
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum Status {
On,
Off,
}
pub(crate) fn set(status: Status) -> Result<(), Error> {
Ok(cmd::command(
"systemctl",
&[
"--user",
"--no-block",
match status {
Status::On => "start",
Status::Off => "stop",
},
"redshift.service",
],
)?)
}

164
mgr/src/spotify.rs Normal file
View File

@@ -0,0 +1,164 @@
use thiserror::Error;
use super::{
Exec,
cli::{self, CliCommand},
cmd, systemd,
wire::{WireCommand, server},
};
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Cmd(#[from] cmd::Error),
#[error(transparent)]
Cli(#[from] cli::ParseError),
#[error(transparent)]
Systemd(#[from] systemd::Error),
}
#[derive(Debug, Clone, Copy)]
pub enum Action {
Start,
Stop,
Status,
Play,
Pause,
Toggle,
Previous,
Next,
}
impl WireCommand for Action {
fn parse_wire(mut input: impl Iterator<Item = u8>) -> Result<Self, server::ParseError> {
match input.next().ok_or(server::ParseError::Eof)? {
0x01 => Ok(Self::Start),
0x02 => Ok(Self::Stop),
0x03 => Ok(Self::Status),
0x04 => Ok(Self::Play),
0x05 => Ok(Self::Pause),
0x06 => Ok(Self::Toggle),
0x07 => Ok(Self::Previous),
0x08 => Ok(Self::Next),
byte => Err(server::ParseError::Unknown(byte)),
}
}
fn to_wire(&self) -> Vec<u8> {
match *self {
Self::Start => vec![0x01],
Self::Stop => vec![0x02],
Self::Status => vec![0x03],
Self::Play => vec![0x04],
Self::Pause => vec![0x05],
Self::Toggle => vec![0x06],
Self::Previous => vec![0x07],
Self::Next => vec![0x08],
}
}
}
impl Exec for Action {
type ExecErr = Error;
fn execute(&self) -> Result<Option<String>, Self::ExecErr> {
match *self {
Self::Start => {
set(Status::On)?;
Ok(None)
}
Self::Stop => {
set(Status::Off)?;
Ok(None)
}
Self::Status => Ok(
if systemd::user::unit_status("spotify.service")?.is_active() {
Some("active".to_owned())
} else {
Some("inactive".to_owned())
},
),
Self::Play => {
playerctl("play")?;
Ok(None)
}
Self::Pause => {
playerctl("pause")?;
Ok(None)
}
Self::Toggle => {
playerctl("play-pause")?;
Ok(None)
}
Self::Previous => {
playerctl("previous")?;
Ok(None)
}
Self::Next => {
playerctl("next")?;
Ok(None)
}
}
}
}
impl CliCommand for Action {
type ExecErr = Error;
fn parse_str(input: &str, rest: impl Iterator<Item = String>) -> Result<Self, cli::ParseError>
where
Self: Sized,
{
let result = match input {
"start" => Self::Start,
"stop" => Self::Stop,
"status" => Self::Status,
"play" => Self::Play,
"pause" => Self::Pause,
"toggle" => Self::Toggle,
"previous" => Self::Previous,
"next" => Self::Next,
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
};
let rest = rest.collect::<Vec<String>>();
if rest.is_empty() {
Ok(result)
} else {
Err(cli::ParseError::UnexpectedInput { rest })
}
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum Status {
On,
Off,
}
pub(crate) fn set(status: Status) -> Result<(), Error> {
Ok(cmd::command(
"systemctl",
&[
"--user",
"--no-block",
match status {
Status::On => "start",
Status::Off => "stop",
},
"spotify.service",
],
)?)
}
fn playerctl(cmd: &str) -> Result<(), Error> {
Ok(cmd::command("playerctl", &["-p", "spotify", cmd])?)
}
pub(crate) fn pause() -> Result<(), Error> {
playerctl("pause")
}

38
mgr/src/systemd.rs Normal file
View File

@@ -0,0 +1,38 @@
use thiserror::Error;
use super::cmd;
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Cmd(#[from] cmd::Error),
#[error("unknown status output: \"{output}\"")]
UnknownStatusOutput { output: String },
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum UnitStatus {
Active,
Inactive,
}
impl UnitStatus {
pub(crate) fn is_active(self) -> bool {
matches!(self, Self::Active)
}
}
pub(crate) mod user {
use super::{super::cmd, Error, UnitStatus};
pub(crate) fn unit_status(unit: &str) -> Result<UnitStatus, Error> {
let output = cmd::run_command("systemctl", &["--user", "is-active", unit])?;
match output.stdout.as_str().trim() {
"active" => Ok(UnitStatus::Active),
"inactive" => Ok(UnitStatus::Inactive),
other => Err(Error::UnknownStatusOutput {
output: other.to_owned(),
}),
}
}
}

101
mgr/src/theme.rs Normal file
View File

@@ -0,0 +1,101 @@
use thiserror::Error;
use super::{
Exec,
cli::{self, CliCommand},
cmd, systemd,
wire::{WireCommand, server},
};
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Cli(#[from] cli::ParseError),
#[error(transparent)]
Cmd(#[from] cmd::Error),
#[error(transparent)]
Systemd(#[from] systemd::Error),
}
#[derive(Debug, Clone, Copy)]
pub enum Action {
Dark = 0x01,
Light = 0x02,
Status = 0x03,
}
impl WireCommand for Action {
fn parse_wire(mut input: impl Iterator<Item = u8>) -> Result<Self, server::ParseError> {
match input.next().ok_or(server::ParseError::Eof)? {
0x01 => Ok(Self::Dark),
0x02 => Ok(Self::Light),
0x03 => Ok(Self::Status),
byte => Err(server::ParseError::Unknown(byte)),
}
}
fn to_wire(&self) -> Vec<u8> {
match *self {
Self::Dark => vec![0x01],
Self::Light => vec![0x02],
Self::Status => vec![0x03],
}
}
}
impl CliCommand for Action {
type ExecErr = Error;
fn parse_str(input: &str, rest: impl Iterator<Item = String>) -> Result<Self, cli::ParseError>
where
Self: Sized,
{
let result = match input {
"dark" => Self::Dark,
"light" => Self::Light,
"status" => Self::Status,
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
};
let rest = rest.collect::<Vec<String>>();
if rest.is_empty() {
Ok(result)
} else {
Err(cli::ParseError::UnexpectedInput { rest })
}
}
}
impl Exec for Action {
type ExecErr = Error;
fn execute(&self) -> Result<Option<String>, Self::ExecErr> {
match *self {
Self::Dark => {
cmd::command(
"systemctl",
&["--user", "--no-block", "start", "color-theme-dark.service"],
)?;
Ok(None)
}
Self::Light => {
cmd::command(
"systemctl",
&["--user", "--no-block", "start", "color-theme-light.service"],
)?;
Ok(None)
}
Self::Status => Ok(
if systemd::user::unit_status("color-theme-light.service")?.is_active() {
Some("light".to_owned())
} else {
Some("dark".to_owned())
},
),
}
}
}

198
mgr/src/weather.rs Normal file
View File

@@ -0,0 +1,198 @@
use std::{
fs, io,
ops::Sub as _,
path::{Path, PathBuf},
};
use thiserror::Error;
use time::format_description::well_known::Iso8601;
use tracing::{Level, event};
const CACHE_AGE: time::Duration = time::Duration::hours(1);
const CACHE_DELIMITER: char = '|';
use super::{
Exec,
cli::{self, CliCommand},
cmd, dirs, systemd,
wire::{WireCommand, server},
};
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
Cmd(#[from] cmd::Error),
#[error(transparent)]
Cli(#[from] cli::ParseError),
#[error(transparent)]
Systemd(#[from] systemd::Error),
#[error(transparent)]
Http(#[from] ureq::Error),
#[error(transparent)]
Dirs(#[from] dirs::Error),
#[error(transparent)]
Io(#[from] io::Error),
#[error("delimiter not found in cache file")]
CacheDelimitedNotFound,
#[error("invalid timestamp \"{input}\" in cache file: {error}")]
CacheTimestampParse {
input: String,
error: time::error::Parse,
},
#[error("cache timestamp ({cache_timestamp}) is from the future (now: {now})")]
CacheTimestampOverflow {
now: time::UtcDateTime,
cache_timestamp: time::UtcDateTime,
},
#[error("formatting cache timestamp failed: {error}")]
CacheTimestampFormat { error: time::error::Format },
}
#[derive(Debug, Clone, Copy)]
pub enum Action {
Get,
}
impl WireCommand for Action {
fn parse_wire(mut input: impl Iterator<Item = u8>) -> Result<Self, server::ParseError> {
match input.next().ok_or(server::ParseError::Eof)? {
0x01 => Ok(Self::Get),
byte => Err(server::ParseError::Unknown(byte)),
}
}
fn to_wire(&self) -> Vec<u8> {
match *self {
Self::Get => vec![0x01],
}
}
}
impl Exec for Action {
type ExecErr = Error;
fn execute(&self) -> Result<Option<String>, Self::ExecErr> {
match *self {
Self::Get => Ok(Some(get()?)),
}
}
}
impl CliCommand for Action {
type ExecErr = Error;
fn parse_str(input: &str, rest: impl Iterator<Item = String>) -> Result<Self, cli::ParseError>
where
Self: Sized,
{
let result = match input {
"get" => Self::Get,
s => {
return Err(cli::ParseError::UnknownAction {
action: s.to_owned(),
});
}
};
let rest = rest.collect::<Vec<String>>();
if rest.is_empty() {
Ok(result)
} else {
Err(cli::ParseError::UnexpectedInput { rest })
}
}
}
#[derive(Debug)]
struct Cache {
timestamp: time::UtcDateTime,
value: String,
}
fn cache_file() -> Result<PathBuf, Error> {
Ok(dirs::xdg_cache_dir()?.join("workstation-mgr.wttr.cache"))
}
fn store_cache(path: &Path, timestamp: &time::UtcDateTime, value: &str) -> Result<(), Error> {
event!(Level::DEBUG, "storing in cache: {timestamp} {value}");
Ok(fs::write(
path,
format!(
"{timestamp}{CACHE_DELIMITER}{value}",
timestamp = timestamp
.format(&Iso8601::DEFAULT)
.map_err(|error| Error::CacheTimestampFormat { error })?,
),
)?)
}
fn get_cache(path: &Path) -> Result<Option<Cache>, Error> {
match fs::read_to_string(path) {
Ok(content) => {
let (timestamp, value) = content
.split_once(CACHE_DELIMITER)
.ok_or(Error::CacheDelimitedNotFound)?;
let cache_timestamp =
time::UtcDateTime::parse(timestamp, &Iso8601::DEFAULT).map_err(|error| {
Error::CacheTimestampParse {
input: timestamp.to_owned(),
error,
}
})?;
Ok(Some(Cache {
timestamp: cache_timestamp,
value: value.to_owned(),
}))
}
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e.into()),
}
}
fn request() -> Result<String, Error> {
Ok(ureq::get("https://wttr.in/Ansbach?m&T&format=%c%t")
.call()?
.body_mut()
.read_to_string()?)
}
fn get_and_update_cache(cache_file: &Path, now: &time::UtcDateTime) -> Result<String, Error> {
event!(Level::DEBUG, "refreshing cache");
let value = request()?;
store_cache(cache_file, now, &value)?;
Ok(value)
}
fn get() -> Result<String, Error> {
let cache_file = cache_file()?;
event!(Level::DEBUG, "using cache file {cache_file:?}");
let cache = get_cache(&cache_file)?;
event!(Level::DEBUG, "read from cache: {cache:?}");
let now = time::UtcDateTime::now();
match cache {
Some(cache) => {
let cache_age = now.sub(cache.timestamp);
event!(Level::DEBUG, "cache age: {cache_age}");
if cache_age.is_negative() {
return Err(Error::CacheTimestampOverflow {
now,
cache_timestamp: cache.timestamp,
});
}
if cache_age <= CACHE_AGE {
event!(Level::DEBUG, "reusing cache");
Ok(cache.value)
} else {
get_and_update_cache(&cache_file, &now)
}
}
None => get_and_update_cache(&cache_file, &now),
}
}

20
mgr/src/wire/client.rs Normal file
View File

@@ -0,0 +1,20 @@
use std::{
io::{self, Write as _},
os::unix::net::UnixStream,
};
use thiserror::Error;
use super::{super::Action, WireCommand as _};
#[derive(Debug, Error)]
pub enum SendError {
#[error(transparent)]
Io(#[from] io::Error),
}
impl Action {
pub fn send(&self, stream: &mut UnixStream) -> Result<(), SendError> {
Ok(stream.write_all(&self.to_wire())?)
}
}

11
mgr/src/wire/mod.rs Normal file
View File

@@ -0,0 +1,11 @@
pub mod client;
pub mod server;
pub mod socket;
pub(crate) trait WireCommand {
fn parse_wire(input: impl Iterator<Item = u8>) -> Result<Self, server::ParseError>
where
Self: Sized;
fn to_wire(&self) -> Vec<u8>;
}

88
mgr/src/wire/server.rs Normal file
View File

@@ -0,0 +1,88 @@
use std::{
io::{self, Read, Write},
os::unix::net::{SocketAddr, UnixListener, UnixStream},
thread,
};
use thiserror::Error;
use tracing::{Level, event};
use super::{
super::{Action, Exec as _},
WireCommand, socket,
};
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Parse(#[from] ParseError),
#[error(transparent)]
Socket(#[from] socket::Error),
#[error(transparent)]
Exec(#[from] crate::ExecError),
}
#[derive(Debug, Error)]
pub enum ParseError {
#[error("received unexpected eof")]
Eof,
#[error("received unknown byte: {0:#X}")]
Unknown(u8),
#[error("received surplus input: {0:?}")]
Surplus(Vec<u8>),
}
fn handle_client(stream: &mut UnixStream) -> Result<(), Error> {
let input = {
let mut buf = Vec::new();
stream.read_to_end(&mut buf)?;
buf
};
event!(Level::DEBUG, "request data: {input:?}");
let action = Action::parse_wire(input.into_iter())?;
event!(Level::DEBUG, "parsed request: {action:?}");
if let Some(output) = action.execute()? {
stream.write_all(output.as_bytes())?;
}
Ok(())
}
pub fn run() -> Result<(), Error> {
event!(Level::DEBUG, "starting server");
let socket_path = socket::get_socket_path()?;
socket::try_remove_socket(&socket_path)?;
let socket_addr = SocketAddr::from_pathname(socket_path)?;
event!(Level::DEBUG, "socket address {socket_addr:?}");
let listener = UnixListener::bind_addr(&socket_addr)?;
for stream in listener.incoming() {
let mut stream = stream?;
thread::spawn(move || {
event!(Level::DEBUG, "received request");
let result = handle_client(&mut stream);
if let Err(e) = result {
let msg = e.to_string();
event!(Level::ERROR, "action failed: {msg}");
if let Err(e) = stream.write_all(msg.as_bytes()) {
event!(Level::ERROR, "sending \"{msg}\" failed: {e}");
}
}
event!(Level::DEBUG, "closing stream");
drop(stream);
});
}
unreachable!()
}

35
mgr/src/wire/socket.rs Normal file
View File

@@ -0,0 +1,35 @@
use std::{
fs, io,
path::{Path, PathBuf},
};
use thiserror::Error;
use super::super::dirs;
#[derive(Debug, Error)]
pub enum Error {
#[error("could not find a suitable socket path")]
NoSocketPathFound,
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
Dirs(#[from] dirs::Error),
}
pub fn get_socket_path() -> Result<PathBuf, Error> {
if let Some(mut dir) = dirs::xdg_runtime_dir()? {
dir.push("workstation-mgr.sock");
return Ok(dir);
}
Err(Error::NoSocketPathFound)
}
pub(crate) fn try_remove_socket(path: &Path) -> Result<(), Error> {
match fs::remove_file(path) {
Ok(()) => Ok(()),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(e.into()),
}
}