uses a proc-macro to automate command definitions
This commit is contained in:
47
api-core/src/command.rs
Normal file
47
api-core/src/command.rs
Normal 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>;
|
||||
}
|
||||
Reference in New Issue
Block a user