30 lines
825 B
Rust
30 lines
825 B
Rust
use std::io::Write;
|
|
use std::os::unix::net::UnixStream;
|
|
use std::path::Path;
|
|
|
|
use magpie_types::MagpieServerMsg;
|
|
|
|
/// The name of the Magpie server socket.
|
|
pub const MAGPIE_SOCK: &str = "magpie.sock";
|
|
|
|
/// A client to a Magpie server.
|
|
pub struct MagpieClient {
|
|
socket: UnixStream,
|
|
}
|
|
|
|
impl MagpieClient {
|
|
pub fn new() -> std::io::Result<Self> {
|
|
let sock_dir = std::env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR not set");
|
|
let sock_dir = Path::new(&sock_dir);
|
|
let sock_path = sock_dir.join(MAGPIE_SOCK);
|
|
let socket = UnixStream::connect(sock_path)?;
|
|
Ok(Self { socket })
|
|
}
|
|
|
|
pub fn send_msg(&mut self, msg: &MagpieServerMsg) -> std::io::Result<()> {
|
|
let bytes = serde_json::to_vec(msg).unwrap();
|
|
self.socket.write_all(&bytes)?;
|
|
Ok(())
|
|
}
|
|
}
|