1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//! This library provides easy access to the Discord IPC.
//! 
//! It provides implementations for both Unix and Windows
//! operating systems, with both implementations using the
//! same API. Thus, this crate can be used in a platform-agnostic
//! manner.
//! 
//! # Hello world
//! ```
//! use discord_rich_presence::{new_client, DiscordIpc};
//! use serde_json::json;
//! 
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let client = new_client("<some client id>")?;
//!     client.connect()?;
//! 
//!     let payload = json!({
//!         "state": "Hello world!"
//!     });
//!     client.set_activity(payload)?;
//! }
//! ```

mod discord_ipc;
mod pack_unpack;
pub use discord_ipc::*;

#[cfg(unix)]
mod ipc_unix;
#[cfg(unix)]
use ipc_unix as ipc;

#[cfg(windows)]
mod ipc_windows;
#[cfg(windows)]
use ipc_windows as ipc;


/// Creates a new client to connect to the Discord IPC.
/// 
/// # Examples
/// ```
/// 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>> {
    let client = ipc::DiscordIpcClient {
        client_id: client_id.to_string(),
        connected: false,
        socket: None,
    };

    Ok(client)
}