discord_ipc_rp/
lib.rs

1//! This library provides easy access to the Discord IPC.
2//!
3//! It provides implementations for both Unix and Windows
4//! operating systems, with both implementations using the
5//! same API. Thus, this crate can be used in a platform-agnostic
6//! manner.
7//!
8//! # Hello world
9//! ```
10//! use discord_ipc::{activity, DiscordIpc, DiscordIpcClient};
11//!
12//! fn main() -> Result<(), Box<dyn std::error::Error>> {
13//!     let mut client = DiscordIpcClient::new("<some client id>");
14//!     client.connect()?;
15//!
16//!     let payload = activity::Activity::new().state("Hello world!");
17//!     client.set_activity(payload)?;
18//! }
19//! ```
20#![deny(missing_docs)]
21
22mod discord_ipc;
23mod pack_unpack;
24pub use discord_ipc::*;
25pub mod activity;
26
27#[cfg(unix)]
28mod ipc_unix;
29#[cfg(unix)]
30use ipc_unix as ipc;
31
32#[cfg(windows)]
33mod ipc_windows;
34#[cfg(windows)]
35use ipc_windows as ipc;
36
37pub use ipc::DiscordIpcClient;
38use serde::ser::SerializeStruct;
39
40#[deprecated(since = "0.2.0", note = "use DiscordIpcClient::new() instead")]
41/// Creates a new client to connect to the Discord IPC. Functionally
42/// identical to [`DiscordIpcClient::new()`].
43///
44/// # Examples
45/// ```
46/// let ipc_client = discord_ipc_client::new_client("<some client id>");
47/// ```
48pub fn new_client(client_id: &str) -> impl DiscordIpc {
49    ipc::DiscordIpcClient::new(client_id)
50}
51
52/// The error type for this crate.
53#[derive(Debug, thiserror::Error)]
54pub enum Error {
55    /// Any IO errors.
56    #[error(transparent)]
57    IO(#[from] std::io::Error),
58    /// Any String FromUtf8 errors.
59    #[error(transparent)]
60    String(#[from] std::string::FromUtf8Error),
61    /// Errors from the serde_json crate.
62    #[error(transparent)]
63    Json(#[from] serde_json::Error),
64    /// The opcode was malformed while attempting to unpack.
65    #[error("malformed opcode")]
66    MalformedOpcode,
67    /// The header was malformed while attempting to unpack.
68    #[error("malformed header")]
69    MalformedHeader,
70    /// Could not connect to the Discord IPC socket.
71    #[error("could not connect to the Discord IPC socket: {0}")]
72    CouldNotConnect(std::io::Error),
73}
74
75/// The result type for this crate.
76pub type Result<T> = std::result::Result<T, Error>;
77
78/// An empty struct that represents the absence of a payload.
79pub(crate) struct Empty;
80impl serde::Serialize for Empty {
81    fn serialize<S: serde::Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
82        serializer.serialize_struct("Empty", 0).unwrap().end()
83    }
84}