uses a proc-macro to automate command definitions

This commit is contained in:
2025-12-31 00:23:30 -05:00
parent 6fdbb868b7
commit 0e28b0416a
26 changed files with 757 additions and 177 deletions

View File

@@ -1,92 +1,12 @@
use anyhow::anyhow;
use api::client::command::Commanding;
use api::client::Client;
use api::data_type::DataType;
use api::messages::command::{
Command, CommandDefinition, CommandParameterDefinition, CommandResponse,
};
use api::macros::IntoCommandDefinition;
use api::messages::command::CommandHeader;
use log::info;
use std::error::Error;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
fn handle_command(command: Command) -> anyhow::Result<String> {
let timestamp = command.timestamp;
let a: f32 = (*command
.parameters
.get("a")
.ok_or(anyhow!("Parameter 'a' Missing"))?)
.try_into()?;
let b: f64 = (*command
.parameters
.get("b")
.ok_or(anyhow!("Parameter 'b' Missing"))?)
.try_into()?;
let c: bool = (*command
.parameters
.get("c")
.ok_or(anyhow!("Parameter 'c' Missing"))?)
.try_into()?;
info!("Command Received:\n timestamp: {timestamp}\n a: {a}\n b: {b}\n c: {c}");
Ok(format!(
"Successfully Received Command! timestamp: {timestamp} a: {a} b: {b} c: {c}"
))
}
struct CommandHandle {
cancellation_token: CancellationToken,
}
impl CommandHandle {
pub async fn register(
client: Arc<Client>,
command_definition: CommandDefinition,
mut callback: impl FnMut(Command) -> anyhow::Result<String> + Send + 'static,
) -> anyhow::Result<Self> {
let cancellation_token = CancellationToken::new();
let result = Self {
cancellation_token: cancellation_token.clone(),
};
tokio::spawn(async move {
while !cancellation_token.is_cancelled() {
// This would only fail if the sender closed while trying to insert data
// It would wait until space is made
let Ok(mut rx) = client
.register_callback_channel(command_definition.clone())
.await
else {
continue;
};
while let Some((cmd, responder)) = rx.recv().await {
let response = match callback(cmd) {
Ok(response) => CommandResponse {
success: true,
response,
},
Err(err) => CommandResponse {
success: false,
response: err.to_string(),
},
};
// This should only err if we had an error elsewhere
let _ = responder.send(response);
}
}
});
Ok(result)
}
}
impl Drop for CommandHandle {
fn drop(&mut self) {
self.cancellation_token.cancel();
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
@@ -102,28 +22,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
let client = Arc::new(Client::connect("ws://[::1]:8080/backend")?);
let handle = CommandHandle::register(
client,
CommandDefinition {
name: "simple_command/a".to_string(),
parameters: vec![
CommandParameterDefinition {
name: "a".to_string(),
data_type: DataType::Float32,
},
CommandParameterDefinition {
name: "b".to_string(),
data_type: DataType::Float64,
},
CommandParameterDefinition {
name: "c".to_string(),
data_type: DataType::Boolean,
},
],
},
handle_command,
)
.await?;
let handle =
Commanding::register_handler(client, "simple_command/a".to_string(), handle_command);
cancellation_token.cancelled().await;
@@ -131,3 +31,21 @@ async fn main() -> Result<(), Box<dyn Error>> {
Ok(())
}
#[derive(IntoCommandDefinition)]
struct SimpleCommandA {
a: f32,
b: f64,
c: bool,
}
fn handle_command(header: CommandHeader, command: SimpleCommandA) -> anyhow::Result<String> {
let timestamp = header.timestamp;
let SimpleCommandA { a, b, c } = command;
info!("Command Received:\n timestamp: {timestamp}\n a: {a}\n b: {b}\n c: {c}");
Ok(format!(
"Successfully Received Command! timestamp: {timestamp} a: {a} b: {b} c: {c}"
))
}