emulator: cut down on cfg directives

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