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

47
api-core/src/command.rs Normal file
View File

@@ -0,0 +1,47 @@
use crate::data_type::DataType;
use crate::data_value::DataValue;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CommandParameterDefinition {
pub name: String,
pub data_type: DataType,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandDefinition {
pub name: String,
pub parameters: Vec<CommandParameterDefinition>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommandHeader {
pub timestamp: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Command {
#[serde(flatten)]
pub header: CommandHeader,
pub parameters: HashMap<String, DataValue>,
}
#[derive(Debug, Error)]
pub enum IntoCommandDefinitionError {
#[error("Parameter Missing: {0}")]
ParameterMissing(String),
#[error("Mismatched Type for {parameter}. {expected:?} expected")]
MismatchedType {
parameter: String,
expected: DataType,
},
}
pub trait IntoCommandDefinition: Sized {
fn create(name: String) -> CommandDefinition;
fn parse(command: Command) -> Result<Self, IntoCommandDefinitionError>;
}

24
api-core/src/data_type.rs Normal file
View File

@@ -0,0 +1,24 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DataType {
Float32,
Float64,
Boolean,
}
pub trait ToDataType {
const DATA_TYPE: DataType;
}
macro_rules! impl_to_data_type {
( $ty:ty, $value:expr ) => {
impl ToDataType for $ty {
const DATA_TYPE: DataType = $value;
}
};
}
impl_to_data_type!(f32, DataType::Float32);
impl_to_data_type!(f64, DataType::Float64);
impl_to_data_type!(bool, DataType::Boolean);

View File

@@ -0,0 +1,9 @@
use derive_more::TryInto;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize, Deserialize, TryInto)]
pub enum DataValue {
Float32(f32),
Float64(f64),
Boolean(bool),
}

3
api-core/src/lib.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod command;
pub mod data_type;
pub mod data_value;