diff --git a/emulator/src/emulator/misc/mod.rs b/emulator/src/emulator/misc/mod.rs index 7689ddb..06a3fd0 100644 --- a/emulator/src/emulator/misc/mod.rs +++ b/emulator/src/emulator/misc/mod.rs @@ -1,2 +1 @@ -#[cfg(feature = "discord-rpc")] pub mod rpc; diff --git a/emulator/src/emulator/misc/rpc.rs b/emulator/src/emulator/misc/rpc.rs index c548874..183d773 100644 --- a/emulator/src/emulator/misc/rpc.rs +++ b/emulator/src/emulator/misc/rpc.rs @@ -25,15 +25,18 @@ use std::{ time::Duration, }; +#[cfg(feature = "discord-rpc")] use discord_presence::{Client, DiscordError, models::ActivityTimestamps}; use crate::emulator::config::Config; #[derive(Debug)] +#[cfg(feature = "discord-rpc")] pub enum RpcClientError { Client(DiscordError), } +#[cfg(feature = "discord-rpc")] impl std::fmt::Display for RpcClientError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -42,8 +45,10 @@ impl std::fmt::Display for RpcClientError { } } +#[cfg(feature = "discord-rpc")] impl std::error::Error for RpcClientError {} +#[cfg(feature = "discord-rpc")] impl From for RpcClientError { fn from(err: DiscordError) -> Self { Self::Client(err) @@ -52,6 +57,7 @@ impl From for RpcClientError { /// The type of activity the user is currently doing. #[derive(Debug, Clone)] +#[cfg(feature = "discord-rpc")] pub enum Activity { Idle, EditingFile(PathBuf), @@ -59,6 +65,7 @@ pub enum Activity { /// Messages to send over the wire. #[derive(Debug)] +#[cfg(feature = "discord-rpc")] pub enum Message { /// Sent when we want to update the [`Context`]. Update(Activity), @@ -66,9 +73,11 @@ pub enum Message { Stop, } +#[cfg(feature = "discord-rpc")] unsafe impl Send for Message {} #[derive(Debug, Clone)] +#[cfg(feature = "discord-rpc")] pub struct RpcClient { /// Sends updates to [`Context`] (our state). sender: Sender, @@ -76,6 +85,7 @@ pub struct RpcClient { thread_handle: Option>>, } +#[cfg(feature = "discord-rpc")] impl RpcClient { #[expect(clippy::unreadable_literal)] /// Sets up the [`RpcClient`]. @@ -169,6 +179,7 @@ impl RpcClient { } // Possibly unneeded but good practice. +#[cfg(feature = "discord-rpc")] impl Drop for RpcClient { fn drop(&mut self) { self.stop(); @@ -181,6 +192,18 @@ impl Drop for RpcClient { } } +/// Stub for when the feature is disabled. +#[cfg(not(feature = "discord-rpc"))] +pub struct RpcClient {} + +/// Stub for when the feature is disabled. +#[cfg(not(feature = "discord-rpc"))] +pub enum Message {} + +/// Stub for when the feature is disabled. +#[cfg(not(feature = "discord-rpc"))] +pub enum Activity {} + /// Gets the discord [`RpcClient`] or returns None if this has been disabled in the config /// options. #[cfg(feature = "config")] @@ -189,6 +212,10 @@ pub fn get_rpc_client_or_none( rpc_sender: Sender, rpc_reciever: Receiver, ) -> Result, Box> { + #[cfg(not(feature = "discord-rpc"))] + return Ok(None); + + #[cfg(feature = "discord-rpc")] if config.misc.use_discord_rpc { Ok(Some(RpcClient::new(rpc_sender, rpc_reciever)?)) } else { diff --git a/emulator/src/emulator/system/emulator.rs b/emulator/src/emulator/system/emulator.rs index 6619b8d..46638d2 100644 --- a/emulator/src/emulator/system/emulator.rs +++ b/emulator/src/emulator/system/emulator.rs @@ -1,4 +1,3 @@ -#[cfg(feature = "discord-rpc")] use std::sync::Arc; use std::{ sync::mpsc::{self, Receiver, Sender}, @@ -6,9 +5,7 @@ use std::{ time::Duration, }; -#[cfg(feature = "discord-rpc")] use crate::emulator::misc::rpc::{Activity, RpcClient}; - use crate::emulator::system::{ model::{Command, PersistentState, Running, State}, processor::Processor, @@ -21,7 +18,7 @@ pub fn run_emulator( cmd_rx: &Receiver, state_tx: &Sender, mut processor: Processor, - #[cfg(feature = "discord-rpc")] rpc_client: Option<&Arc>, + rpc_client: Option<&Arc>, ) { println!("INFO: Starting emulator."); diff --git a/emulator/src/main.rs b/emulator/src/main.rs index fef27a5..b51bf1f 100644 --- a/emulator/src/main.rs +++ b/emulator/src/main.rs @@ -1,4 +1,3 @@ -#[cfg(feature = "discord-rpc")] use std::sync::Arc; use std::{ path::Path, @@ -6,7 +5,6 @@ use std::{ thread, }; -#[cfg(feature = "discord-rpc")] use dsa_rs::emulator::misc::rpc::{RpcClient, get_rpc_client_or_none}; use dsa_rs::emulator::{ @@ -31,17 +29,12 @@ fn main() -> Result<(), Box> { let config = Config::load(Path::new(".dsa.emulator.toml"))?; // Setup RPC if enabled. - #[cfg(feature = "discord-rpc")] let (rpc_sender, rpc_reciever) = std::sync::mpsc::channel(); - #[cfg(feature = "discord-rpc")] let rpc_client = get_rpc_client_or_none(&config, rpc_sender, rpc_reciever)?.map(Arc::new); - #[cfg(feature = "discord-rpc")] setup_emulator(cmd_receiver, state_sender, rpc_client); - #[cfg(not(feature = "discord-rpc"))] - setup_emulator(cmd_receiver, state_sender); let ui = setup_ui(cmd_sender, state_reciever); @@ -66,17 +59,13 @@ fn main() -> Result<(), Box> { fn setup_emulator( cmd_receiver: Receiver, state_sender: Sender, - #[cfg(feature = "discord-rpc")] rpc_client: Option>, + rpc_client: Option>, ) { let main_store = MainStore::new(); let processor = Processor::new(Box::new(main_store), vec![]); thread::spawn(move || { - #[cfg(feature = "discord-rpc")] run_emulator(&cmd_receiver, &state_sender, processor, rpc_client.as_ref()); - - #[cfg(not(feature = "discord-rpc"))] - run_emulator(&cmd_receiver, &state_sender, processor); }); }