File: ipc.rs

package info (click to toggle)
rust-swtchr 0.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,244 kB
  • sloc: makefile: 4
file content (45 lines) | stat: -rw-r--r-- 946 bytes parent folder | download | duplicates (3)
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
use std::env;
use std::path::PathBuf;

use eyre::bail;

const SOCK_NAME: &str = "swtchrd.sock";

pub fn sock_path() -> PathBuf {
    match env::var("XDG_RUNTIME_DIR").unwrap_or_default().trim() {
        "" => {
            let uid = nix::unistd::getuid();
            PathBuf::from(format!("/run/user/{uid}/{SOCK_NAME}"))
        }
        path => [path, SOCK_NAME].iter().collect(),
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Command {
    Show,
}

impl Command {
    pub const BUF_LEN: usize = 16;

    pub fn msg(&self) -> &[u8] {
        use Command::*;

        match self {
            Show => b"show",
        }
    }

    pub fn from_msg(msg: &[u8]) -> eyre::Result<Self> {
        use Command::*;

        Ok(match msg {
            b"show" => Show,
            _ => bail!(
                "Unrecognized command received over swtchrd IPC socket: '{:?}'.",
                msg
            ),
        })
    }
}