Replace gRPC Backend (#10)
**Rationale:** Having two separate servers and communication methods resulted in additional maintenance & the need to convert often between backend & frontend data types. By moving the backend communication off of gRPC and to just use websockets it both gives more control & allows for simplification of the implementation. #8 **Changes:** - Replaces gRPC backend. - New implementation automatically handles reconnect logic - Implements an api layer - Migrates examples to the api layer - Implements a proc macro to make command handling easier - Implements unit tests for the api layer (90+% coverage) - Implements integration tests for the proc macro (90+% coverage) Reviewed-on: #10 Co-authored-by: Sergey Savelyev <sergeysav.nn@gmail.com> Co-committed-by: Sergey Savelyev <sergeysav.nn@gmail.com>
This commit was merged in pull request #10.
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, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct CommandDefinition {
|
||||
pub name: String,
|
||||
pub parameters: Vec<CommandParameterDefinition>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct CommandHeader {
|
||||
pub timestamp: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Command {
|
||||
#[serde(flatten)]
|
||||
pub header: CommandHeader,
|
||||
pub parameters: HashMap<String, DataValue>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, 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>;
|
||||
}
|
||||
26
api-core/src/data_type.rs
Normal file
26
api-core/src/data_type.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use crate::data_value::DataValue;
|
||||
use derive_more::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Display)]
|
||||
pub enum DataType {
|
||||
Float32,
|
||||
Float64,
|
||||
Boolean,
|
||||
}
|
||||
|
||||
pub trait ToDataType: Into<DataValue> {
|
||||
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);
|
||||
20
api-core/src/data_value.rs
Normal file
20
api-core/src/data_value.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use crate::data_type::DataType;
|
||||
use derive_more::{From, TryInto};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, From, TryInto)]
|
||||
pub enum DataValue {
|
||||
Float32(f32),
|
||||
Float64(f64),
|
||||
Boolean(bool),
|
||||
}
|
||||
|
||||
impl DataValue {
|
||||
pub fn to_data_type(self) -> DataType {
|
||||
match self {
|
||||
DataValue::Float32(_) => DataType::Float32,
|
||||
DataValue::Float64(_) => DataType::Float64,
|
||||
DataValue::Boolean(_) => DataType::Boolean,
|
||||
}
|
||||
}
|
||||
}
|
||||
3
api-core/src/lib.rs
Normal file
3
api-core/src/lib.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod command;
|
||||
pub mod data_type;
|
||||
pub mod data_value;
|
||||
Reference in New Issue
Block a user