Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use u64 for client_id instead of String & implment PartialEq for Activity types #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde_derive::Serialize;
///
/// Note that all methods return `Self`, and can be chained
/// for fluency
#[derive(Serialize, Clone)]
#[derive(Serialize, Clone, PartialEq)]
pub struct Activity<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
state: Option<&'a str>,
Expand Down Expand Up @@ -34,7 +34,7 @@ pub struct Activity<'a> {
///
/// Note that all methods return `Self`, and can be chained
/// for fluency
#[derive(Serialize, Clone)]
#[derive(Serialize, Clone, PartialEq)]
pub struct Timestamps {
#[serde(skip_serializing_if = "Option::is_none")]
start: Option<i64>,
Expand All @@ -47,7 +47,7 @@ pub struct Timestamps {
///
/// Note that all methods return `Self`, and can be chained
/// for fluency
#[derive(Serialize, Clone)]
#[derive(Serialize, Clone, PartialEq)]
pub struct Party<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<&'a str>,
Expand All @@ -61,7 +61,7 @@ pub struct Party<'a> {
///
/// Note that all methods return `Self`, and can be chained
/// for fluency
#[derive(Serialize, Clone)]
#[derive(Serialize, Clone, PartialEq)]
pub struct Assets<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
large_image: Option<&'a str>,
Expand All @@ -81,7 +81,7 @@ pub struct Assets<'a> {
///
/// Note that all methods return `Self`, and can be chained
/// for fluency
#[derive(Serialize, Clone)]
#[derive(Serialize, Clone, PartialEq)]
pub struct Secrets<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
join: Option<&'a str>,
Expand All @@ -97,7 +97,7 @@ pub struct Secrets<'a> {
/// attached to an `Activity`
///
/// An activity may have a maximum of 2 buttons
#[derive(Serialize, Clone)]
#[derive(Serialize, Clone, PartialEq)]
pub struct Button<'a> {
label: &'a str,
url: &'a str,
Expand Down
8 changes: 4 additions & 4 deletions src/discord_ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub trait DiscordIpc {
}

#[doc(hidden)]
fn get_client_id(&self) -> &String;
fn get_client_id(&self) -> String;

#[doc(hidden)]
fn connect_ipc(&mut self) -> Result<()>;
Expand Down Expand Up @@ -179,9 +179,9 @@ pub trait DiscordIpc {
}

/// Works the same as as [`set_activity`] but clears activity instead.
///
///
/// [`set_activity`]: #method.set_activity
///
///
/// # Errors
/// Returns an `Err` variant if sending the payload failed.
fn clear_activity(&mut self) -> Result<()> {
Expand All @@ -193,7 +193,7 @@ pub trait DiscordIpc {
},
"nonce": Uuid::new_v4().to_string()
});

self.send(data, 1)?;

Ok(())
Expand Down
12 changes: 6 additions & 6 deletions src/ipc_unix.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::discord_ipc::DiscordIpc;
use serde_json::json;
use std::os::unix::net::UnixStream;
use std::{
env::var,
error::Error,
io::{Read, Write},
net::Shutdown,
os::unix::net::UnixStream,
path::PathBuf,
};

Expand All @@ -27,7 +27,7 @@ type Result<T> = std::result::Result<T, Box<dyn Error>>;
/// underlying [`DiscordIpc`](trait@DiscordIpc) trait.
pub struct DiscordIpcClient {
/// Client ID of the IPC client.
pub client_id: String,
pub client_id: u64,
connected: bool,
socket: Option<UnixStream>,
}
Expand All @@ -39,9 +39,9 @@ impl DiscordIpcClient {
/// ```
/// let ipc_client = DiscordIpcClient::new("<some client id>")?;
/// ```
pub fn new(client_id: &str) -> Result<Self> {
pub fn new(client_id: u64) -> Result<Self> {
let client = Self {
client_id: client_id.to_string(),
client_id: client_id,
connected: false,
socket: None,
};
Expand Down Expand Up @@ -126,7 +126,7 @@ impl DiscordIpc for DiscordIpcClient {
Ok(())
}

fn get_client_id(&self) -> &String {
&self.client_id
fn get_client_id(&self) -> String {
self.client_id.to_string()
}
}
10 changes: 5 additions & 5 deletions src/ipc_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Result<T> = std::result::Result<T, Box<dyn Error>>;
/// underlying [`DiscordIpc`](trait@DiscordIpc) trait.
pub struct DiscordIpcClient {
/// Client ID of the IPC client.
pub client_id: String,
pub client_id: u64,
connected: bool,
socket: Option<File>,
}
Expand All @@ -28,9 +28,9 @@ impl DiscordIpcClient {
/// ```
/// let ipc_client = DiscordIpcClient::new("<some client id>")?;
/// ```
pub fn new(client_id: &str) -> Result<Self> {
pub fn new(client_id: u64) -> Result<Self> {
let client = Self {
client_id: client_id.to_string(),
client_id: client_id,
connected: false,
socket: None,
};
Expand Down Expand Up @@ -91,7 +91,7 @@ impl DiscordIpc for DiscordIpcClient {
Ok(())
}

fn get_client_id(&self) -> &String {
&self.client_id
fn get_client_id(&self) -> String {
self.client_id.to_string()
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ pub use ipc::DiscordIpcClient;
/// ```
/// let ipc_client = discord_ipc_client::new_client("<some client id>")?;
/// ```
pub fn new_client(client_id: &str) -> Result<impl DiscordIpc, Box<dyn std::error::Error>> {
pub fn new_client(client_id: u64) -> Result<impl DiscordIpc, Box<dyn std::error::Error>> {
ipc::DiscordIpcClient::new(client_id)
}
2 changes: 1 addition & 1 deletion tests/models_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::error::Error;

#[test]
fn test_models() -> Result<(), Box<dyn Error>> {
let mut client = DiscordIpcClient::new("771124766517755954")?;
let mut client = DiscordIpcClient::new(771124766517755954)?;
client.connect()?;

let activity = activity::Activity::new()
Expand Down
2 changes: 1 addition & 1 deletion tests/reconnect_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::error::Error;

#[test]
fn test_reconnect() -> Result<(), Box<dyn Error>> {
let mut client = DiscordIpcClient::new("771124766517755954")?;
let mut client = DiscordIpcClient::new(771124766517755954)?;
loop {
if client.connect().is_ok() {
break;
Expand Down
2 changes: 1 addition & 1 deletion tests/update_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::error::Error;

#[test]
fn test_updating() -> Result<(), Box<dyn Error>> {
let mut client = DiscordIpcClient::new("771124766517755954")?;
let mut client = DiscordIpcClient::new(771124766517755954)?;
client.connect()?;

client.set_activity(
Expand Down