diff --git a/src/lib.rs b/src/lib.rs index cf3c3d8..c8c5c24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,34 +1,36 @@ use std::os::unix::net::UnixStream; use std::io::prelude::*; -pub struct I3msg { +pub struct I3msg<'payload> { magic_string: &'static str, payload_type: u32, + payload: &'payload str, buffer: Vec, } -impl I3msg { - pub fn new() -> I3msg { +impl<'payload> I3msg<'payload> { + pub fn new(payload: &str, payload_type:u32) -> I3msg { return I3msg { magic_string: "i3-ipc", - payload_type: 0, + payload_type: payload_type, + payload: payload, buffer: Vec::new(), } } - pub fn run(&mut self, command: &str, stream: &mut UnixStream) -> std::io::Result<()> { - self.packet(command, self.payload_type); - stream.write_all(self.buffer.as_slice()).unwrap(); + pub fn run_command(&mut self, stream: &mut UnixStream) -> std::io::Result<()> { + self.construct_packet(); + stream.write_all(&self.buffer.as_slice()).unwrap(); let mut response = String::new(); stream.read_to_string(&mut response)?; println!("{response}"); Ok(()) } - - fn packet(&mut self, payload:&str, pl_type:u32) { + + fn construct_packet(&mut self) { self.buffer.write(self.magic_string.as_bytes()).unwrap(); - self.buffer.write(payload.len().to_ne_bytes().split_last().unwrap().1).unwrap(); - self.buffer.write(&[*pl_type.to_ne_bytes().split_last().unwrap().0]).unwrap(); - self.buffer.write(payload.as_bytes()).unwrap(); + self.buffer.write(self.payload.len().to_ne_bytes().split_last().unwrap().1).unwrap(); + self.buffer.write(&[*self.payload_type.to_ne_bytes().split_last().unwrap().0]).unwrap(); + self.buffer.write(self.payload.as_bytes()).unwrap(); } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1423265..2b8c6b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,16 @@ use std::env; use sway_ipc; fn main() -> std::io::Result<()> { - let mut stream = UnixStream::connect(env!("SWAYSOCK"))?; - let mut i3 = sway_ipc::I3msg::new(); - i3.run("reload", &mut stream) + let _args: Vec = env::args().collect(); + + match UnixStream::connect(env!("SWAYSOCK")) { + Ok(mut sock) => { + let mut i3 = sway_ipc::I3msg::new("reload", 0); + i3.run_command(&mut sock)?; + }, + Err(e) => { + println!("Couldn't connect: {e:?}"); + } + }; + Ok(()) } \ No newline at end of file