From 8b9ee6fb3eb1a92e6c4fa3b5dbf5d8a600ce1592 Mon Sep 17 00:00:00 2001 From: DylanCa Date: Sat, 13 Jan 2024 12:35:50 +0100 Subject: [PATCH] Added documentation for DiscordClient --- src/client/ipc.rs | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/client/ipc.rs b/src/client/ipc.rs index b6730a2..3ef2a26 100644 --- a/src/client/ipc.rs +++ b/src/client/ipc.rs @@ -8,23 +8,37 @@ use std::os::unix::net::UnixStream; use std::path::PathBuf; use crate::models::client::{commands::Commands, payload::OpCode, payload::Payload}; +use crate::models::error::Error as ErrorMsg; +use crate::models::error::Error::DiscordNotFound; +/// Client used to communicate with Discord through IPC. pub struct DiscordClient { + /// ID of Discord Application, see https://discord.com/developers for more info pub id: String, socket: Option, } impl DiscordClient { + /// Used to instantiate a new Discord Client. pub fn new(id: &str) -> Self { - let mut client = Self { + Self { id: id.to_string(), socket: None, - }; + } + } - client - .connect() - .expect("Could not connect to client. Is Discord running ?"); - client + /// Tries to enable a connection to the Discord Application. + pub fn connect(&mut self) -> Result<(), ErrorMsg> { + let path = self.fetch_process_pathbuf().join("discord-ipc-0"); + + match UnixStream::connect(&path) { + Ok(socket) => { + self.socket = Some(socket); + self.handshake().expect("Could not handshake."); + Ok(()) + } + Err(_) => Err(DiscordNotFound), + } } pub fn send_payload(&mut self, payload: Payload) -> Result<(u32, Value), Box> { @@ -44,21 +58,6 @@ impl DiscordClient { self.socket.as_mut().unwrap() } - fn connect(&mut self) -> Result<(), Box> { - let path = self.fetch_process_pathbuf().join("discord-ipc-0"); - - match UnixStream::connect(&path) { - Ok(socket) => { - self.socket = Some(socket); - - self.handshake().expect("Could not handshake."); - } - Err(_) => panic!("Could not connect to client. Is Discord running ?"), - } - - Ok(()) - } - fn fetch_process_pathbuf(&mut self) -> PathBuf { let mut path = String::new();