Added documentation for DiscordClient

This commit is contained in:
DylanCa 2024-01-13 12:35:50 +01:00
parent d1762d8c7a
commit 8b9ee6fb3e

View file

@ -8,23 +8,37 @@ use std::os::unix::net::UnixStream;
use std::path::PathBuf; use std::path::PathBuf;
use crate::models::client::{commands::Commands, payload::OpCode, payload::Payload}; 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 { pub struct DiscordClient {
/// ID of Discord Application, see https://discord.com/developers for more info
pub id: String, pub id: String,
socket: Option<UnixStream>, socket: Option<UnixStream>,
} }
impl DiscordClient { impl DiscordClient {
/// Used to instantiate a new Discord Client.
pub fn new(id: &str) -> Self { pub fn new(id: &str) -> Self {
let mut client = Self { Self {
id: id.to_string(), id: id.to_string(),
socket: None, socket: None,
}; }
}
client /// Tries to enable a connection to the Discord Application.
.connect() pub fn connect(&mut self) -> Result<(), ErrorMsg> {
.expect("Could not connect to client. Is Discord running ?"); let path = self.fetch_process_pathbuf().join("discord-ipc-0");
client
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<dyn Error>> { pub fn send_payload(&mut self, payload: Payload) -> Result<(u32, Value), Box<dyn Error>> {
@ -44,21 +58,6 @@ impl DiscordClient {
self.socket.as_mut().unwrap() self.socket.as_mut().unwrap()
} }
fn connect(&mut self) -> Result<(), Box<dyn Error>> {
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 { fn fetch_process_pathbuf(&mut self) -> PathBuf {
let mut path = String::new(); let mut path = String::new();