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;
+27
View File
@@ -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<DiscordError> for RpcClientError {
fn from(err: DiscordError) -> Self {
Self::Client(err)
@@ -52,6 +57,7 @@ impl From<DiscordError> 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<Message>,
@@ -76,6 +85,7 @@ pub struct RpcClient {
thread_handle: Option<Arc<std::thread::JoinHandle<()>>>,
}
#[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<Message>,
rpc_reciever: Receiver<Message>,
) -> 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 {
Ok(Some(RpcClient::new(rpc_sender, rpc_reciever)?))
} else {
+1 -4
View File
@@ -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<Command>,
state_tx: &Sender<State>,
mut processor: Processor,
#[cfg(feature = "discord-rpc")] rpc_client: Option<&Arc<RpcClient>>,
rpc_client: Option<&Arc<RpcClient>>,
) {
println!("INFO: Starting emulator.");
+1 -12
View File
@@ -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<dyn std::error::Error>> {
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<dyn std::error::Error>> {
fn setup_emulator(
cmd_receiver: Receiver<Command>,
state_sender: Sender<State>,
#[cfg(feature = "discord-rpc")] rpc_client: Option<Arc<RpcClient>>,
rpc_client: Option<Arc<RpcClient>>,
) {
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);
});
}