refactor of code
This commit is contained in:
parent
56c53312f5
commit
83fcda7484
26
src/lib.rs
26
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<u8>,
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
15
src/main.rs
15
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<String> = 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(())
|
||||
}
|
Loading…
Reference in New Issue
Block a user