aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw/sock.rs
blob: 05597e683a635e680452bc573d81d13edf1a8130 (plain) (blame)
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
use std::io::{BufRead as _, Write as _};

pub struct Sock(std::os::unix::net::UnixStream);

impl Sock {
    pub fn connect() -> Self {
        Self(
            std::os::unix::net::UnixStream::connect(
                rbw::dirs::runtime_dir().join("socket"),
            )
            .unwrap(),
        )
    }

    pub fn send(&mut self, msg: &rbw::agent::Request) {
        let Self(sock) = self;
        sock.write_all(serde_json::to_string(msg).unwrap().as_bytes())
            .unwrap();
        sock.write_all(b"\n").unwrap();
    }

    pub fn recv(&mut self) -> rbw::agent::Response {
        let Self(sock) = self;
        let mut buf = std::io::BufReader::new(sock);
        let mut line = String::new();
        buf.read_line(&mut line).unwrap();
        serde_json::from_str(&line).unwrap()
    }
}