rename admin Command to CommandInput

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-25 22:13:22 +00:00
parent 271959ee27
commit 68f42baf73
2 changed files with 14 additions and 14 deletions

View file

@ -11,7 +11,7 @@ use ruma::{
OwnedEventId, OwnedEventId,
}; };
use service::{ use service::{
admin::{Command, CommandOutput, CommandResult, HandlerResult}, admin::{CommandInput, CommandOutput, CommandResult, HandlerResult},
Services, Services,
}; };
@ -30,10 +30,10 @@ pub(super) fn complete(line: &str) -> String {
} }
#[must_use] #[must_use]
pub(super) fn handle(command: Command) -> HandlerResult { Box::pin(handle_command(command)) } pub(super) fn handle(command: CommandInput) -> HandlerResult { Box::pin(handle_command(command)) }
#[tracing::instrument(skip_all, name = "admin")] #[tracing::instrument(skip_all, name = "admin")]
async fn handle_command(command: Command) -> CommandResult { async fn handle_command(command: CommandInput) -> CommandResult {
AssertUnwindSafe(Box::pin(process_command(&command))) AssertUnwindSafe(Box::pin(process_command(&command)))
.catch_unwind() .catch_unwind()
.await .await
@ -41,7 +41,7 @@ async fn handle_command(command: Command) -> CommandResult {
.or_else(|error| handle_panic(&error, command)) .or_else(|error| handle_panic(&error, command))
} }
async fn process_command(command: &Command) -> CommandOutput { async fn process_command(command: &CommandInput) -> CommandOutput {
Handler { Handler {
services: service::services(), services: service::services(),
} }
@ -50,7 +50,7 @@ async fn process_command(command: &Command) -> CommandOutput {
.and_then(|content| reply(content, command.reply_id.clone())) .and_then(|content| reply(content, command.reply_id.clone()))
} }
fn handle_panic(error: &Error, command: Command) -> CommandResult { fn handle_panic(error: &Error, command: CommandInput) -> CommandResult {
let link = "Please submit a [bug report](https://github.com/girlbossceo/conduwuit/issues/new). 🥺"; let link = "Please submit a [bug report](https://github.com/girlbossceo/conduwuit/issues/new). 🥺";
let msg = format!("Panic occurred while processing command:\n```\n{error:#?}\n```\n{link}"); let msg = format!("Panic occurred while processing command:\n```\n{error:#?}\n```\n{link}");
let content = RoomMessageEventContent::notice_markdown(msg); let content = RoomMessageEventContent::notice_markdown(msg);

View file

@ -26,8 +26,8 @@ use crate::{globals, rooms, rooms::state::RoomMutexGuard, Dep};
pub struct Service { pub struct Service {
services: Services, services: Services,
sender: Sender<Command>, sender: Sender<CommandInput>,
receiver: Mutex<Receiver<Command>>, receiver: Mutex<Receiver<CommandInput>>,
pub handle: RwLock<Option<Handler>>, pub handle: RwLock<Option<Handler>>,
pub complete: StdRwLock<Option<Completer>>, pub complete: StdRwLock<Option<Completer>>,
#[cfg(feature = "console")] #[cfg(feature = "console")]
@ -44,13 +44,13 @@ struct Services {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Command { pub struct CommandInput {
pub command: String, pub command: String,
pub reply_id: Option<OwnedEventId>, pub reply_id: Option<OwnedEventId>,
} }
pub type Completer = fn(&str) -> String; pub type Completer = fn(&str) -> String;
pub type Handler = fn(Command) -> HandlerResult; pub type Handler = fn(CommandInput) -> HandlerResult;
pub type HandlerResult = Pin<Box<dyn Future<Output = CommandResult> + Send>>; pub type HandlerResult = Pin<Box<dyn Future<Output = CommandResult> + Send>>;
pub type CommandResult = Result<CommandOutput, Error>; pub type CommandResult = Result<CommandOutput, Error>;
pub type CommandOutput = Option<RoomMessageEventContent>; pub type CommandOutput = Option<RoomMessageEventContent>;
@ -129,7 +129,7 @@ impl Service {
} }
pub async fn command(&self, command: String, reply_id: Option<OwnedEventId>) { pub async fn command(&self, command: String, reply_id: Option<OwnedEventId>) {
self.send(Command { self.send(CommandInput {
command, command,
reply_id, reply_id,
}) })
@ -139,7 +139,7 @@ impl Service {
pub async fn command_in_place( pub async fn command_in_place(
&self, command: String, reply_id: Option<OwnedEventId>, &self, command: String, reply_id: Option<OwnedEventId>,
) -> Result<Option<RoomMessageEventContent>> { ) -> Result<Option<RoomMessageEventContent>> {
self.process_command(Command { self.process_command(CommandInput {
command, command,
reply_id, reply_id,
}) })
@ -153,7 +153,7 @@ impl Service {
.map(|complete| complete(command)) .map(|complete| complete(command))
} }
async fn send(&self, message: Command) { async fn send(&self, message: CommandInput) {
debug_assert!(!self.sender.is_closed(), "channel closed"); debug_assert!(!self.sender.is_closed(), "channel closed");
self.sender.send_async(message).await.expect("message sent"); self.sender.send_async(message).await.expect("message sent");
} }
@ -163,7 +163,7 @@ impl Service {
self.console.handle_signal(sig).await; self.console.handle_signal(sig).await;
} }
async fn handle_command(&self, command: Command) { async fn handle_command(&self, command: CommandInput) {
match self.process_command(command).await { match self.process_command(command).await {
Ok(Some(output)) => self.handle_response(output).await, Ok(Some(output)) => self.handle_response(output).await,
Ok(None) => debug!("Command successful with no response"), Ok(None) => debug!("Command successful with no response"),
@ -171,7 +171,7 @@ impl Service {
} }
} }
async fn process_command(&self, command: Command) -> CommandResult { async fn process_command(&self, command: CommandInput) -> CommandResult {
if let Some(handle) = self.handle.read().await.as_ref() { if let Some(handle) = self.handle.read().await.as_ref() {
handle(command).await handle(command).await
} else { } else {