reflect
This commit is contained in:
+98
-153
@@ -10,92 +10,15 @@ use std::{
|
|||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use connector::{Connector, UdpConnector};
|
use connector::{Connector, UdpConnector};
|
||||||
|
use egui::Ui;
|
||||||
use log::*;
|
use log::*;
|
||||||
use msg::DsTlmSet;
|
use msg::{DsTlmSet, Msg, TargetMsg, TlmSetItem};
|
||||||
use reflect::*;
|
use reflect::*;
|
||||||
use rfe::macros::Reflect;
|
use rfe::macros::Reflect;
|
||||||
use rfe::*;
|
use rfe::*;
|
||||||
use serde::{Deserialize, Serialize};
|
extern crate alloc;
|
||||||
use simple_logger::SimpleLogger;
|
use simple_logger::SimpleLogger;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
||||||
struct TreeNode {
|
|
||||||
name: String,
|
|
||||||
children: Vec<TreeNode>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TreeNode {
|
|
||||||
pub fn new<T: ToString>(name: T, children: Vec<TreeNode>) -> Self {
|
|
||||||
Self {
|
|
||||||
name: name.to_string(),
|
|
||||||
children,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_child<T: ToString>(&mut self, name: T) -> &mut TreeNode {
|
|
||||||
self.children.push(TreeNode::new(name, Vec::new()));
|
|
||||||
let i = self.children.len() - 1;
|
|
||||||
return &mut self.children[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_add_path(&mut self, path: &str) -> &mut TreeNode {
|
|
||||||
if !path.starts_with(&format!("{}.", self.name)) {
|
|
||||||
return self;
|
|
||||||
}
|
|
||||||
let path = path.replace(&format!("{}.", self.name), "");
|
|
||||||
let names = path.split(".");
|
|
||||||
let mut working_tree = self;
|
|
||||||
|
|
||||||
for name in names {
|
|
||||||
let mut index = None;
|
|
||||||
for (i, child) in &mut working_tree.children.iter().enumerate() {
|
|
||||||
if child.name == name {
|
|
||||||
index = Some(i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if let Some(index) = index {
|
|
||||||
working_tree = &mut working_tree.children[index];
|
|
||||||
} else {
|
|
||||||
working_tree = working_tree.add_child(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
working_tree
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_last(&self) -> bool {
|
|
||||||
self.children.len() == 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_tree(msg_enum: Vec<String>) -> TreeNode {
|
|
||||||
let mut t = TreeNode::new("Msg", Vec::new());
|
|
||||||
for p in msg_enum {
|
|
||||||
let mut x = p
|
|
||||||
.split(" = ")
|
|
||||||
.map(|x| x.split("."))
|
|
||||||
.flatten()
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join(".");
|
|
||||||
|
|
||||||
if x.starts_with(".") {
|
|
||||||
x.remove(0);
|
|
||||||
} else {
|
|
||||||
x = format!("Msg.{}", x);
|
|
||||||
}
|
|
||||||
let s = x.rsplitn(2, ".").collect::<Vec<_>>();
|
|
||||||
|
|
||||||
if s.len() == 2 {
|
|
||||||
t.get_add_path(s[1]).add_child(s[0]);
|
|
||||||
} else {
|
|
||||||
t.add_child(s[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
t
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Reflect)]
|
#[derive(Debug, Default, Reflect)]
|
||||||
struct TestStruct {
|
struct TestStruct {
|
||||||
counter: i8,
|
counter: i8,
|
||||||
@@ -114,84 +37,106 @@ fn main() -> Result<()> {
|
|||||||
.init()
|
.init()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
info!(
|
spawn(|| {
|
||||||
"{:?}",
|
let mut udp = UdpConnector::new("127.0.0.1", 7011, "127.0.0.1", 7010).unwrap();
|
||||||
msg::Msg::DsCmd(msg::DsCmd::AddTlmSet(DsTlmSet::default()))
|
|
||||||
);
|
|
||||||
info!(
|
|
||||||
"json {}",
|
|
||||||
serde_json::to_string(&msg::Msg::DsCmd(msg::DsCmd::Noop)).unwrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut test = TestStruct::default();
|
let mut next_time = Instant::now() + Duration::from_millis(10);
|
||||||
info!("{:?}", test.fields());
|
loop {
|
||||||
reflect::path_set(&mut test, "counter", ReflectValue::I64(1));
|
sleep(next_time - Instant::now());
|
||||||
info!("{:?}", test.fields());
|
|
||||||
|
|
||||||
let mut test2 = EnumTest::None;
|
while let Some(msgs) = udp.recv() {
|
||||||
|
for msg in msgs {
|
||||||
|
info!("got msg {:?}", msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
next_time += Duration::from_millis(10);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// info!("{:?}", test2);
|
let options = eframe::NativeOptions {
|
||||||
|
viewport: egui::ViewportBuilder::default().with_inner_size([560.0, 480.0]),
|
||||||
// let testref = test2.as_variant(1).unwrap();
|
..Default::default()
|
||||||
|
};
|
||||||
// test2.as_variant(1).unwrap().fields()[0]
|
eframe::run_native(
|
||||||
// .1
|
"RFE Ground",
|
||||||
// .set_value(ReflectValue::I64(3));
|
options,
|
||||||
// info!("{:?}", test2);
|
Box::new(|_cc| Ok(Box::<MyApp>::default())),
|
||||||
|
)
|
||||||
// spawn(|| {
|
.or(Err(anyhow!("eframe failed")))?;
|
||||||
// let mut udp = UdpConnector::new("127.0.0.1", 7011, "127.0.0.1", 7010).unwrap();
|
|
||||||
|
|
||||||
// let mut next_time = Instant::now() + Duration::from_millis(10);
|
|
||||||
// loop {
|
|
||||||
// sleep(next_time - Instant::now());
|
|
||||||
|
|
||||||
// while let Some(msgs) = udp.recv() {
|
|
||||||
// for msg in msgs {
|
|
||||||
// info!("got msg {:?}", msg);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// next_time += Duration::from_millis(10);
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// let options = eframe::NativeOptions {
|
|
||||||
// viewport: egui::ViewportBuilder::default().with_inner_size([560.0, 480.0]),
|
|
||||||
// ..Default::default()
|
|
||||||
// };
|
|
||||||
// eframe::run_native(
|
|
||||||
// "RFE Ground",
|
|
||||||
// options,
|
|
||||||
// Box::new(|_cc| Ok(Box::<MyApp>::default())),
|
|
||||||
// )
|
|
||||||
// .or(Err(anyhow!("eframe failed")))?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// struct MyApp {
|
struct MyApp {
|
||||||
// command_path: String,
|
command: Msg,
|
||||||
// tree: TreeNode,
|
values: HashMap<String, String>,
|
||||||
// values: HashMap<String, String>,
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// impl Default for MyApp {
|
impl Default for MyApp {
|
||||||
// fn default() -> Self {
|
fn default() -> Self {
|
||||||
// let tree = to_tree(msg::Msg::None.enumerate_clean());
|
Self {
|
||||||
// Self {
|
command: Msg::None,
|
||||||
// command_path: "Msg".to_string(),
|
values: HashMap::new(),
|
||||||
// tree,
|
}
|
||||||
// values: HashMap::new(),
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
// }
|
fn build_command_ui_value_number(ui: &mut Ui, name: &str, cmd: &mut dyn Reflect) {
|
||||||
|
let mut r = cmd.get_value().str();
|
||||||
|
ui.horizontal(|ui| {
|
||||||
|
ui.label(name);
|
||||||
|
ui.text_edit_singleline(&mut r);
|
||||||
|
cmd.set_value(ReflectValue::Str(r));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_command_ui(ui: &mut Ui, name: &str, cmd: &mut dyn Reflect) {
|
||||||
|
match cmd.reflect_type() {
|
||||||
|
ReflectType::Value => match cmd.get_value() {
|
||||||
|
ReflectValue::None => {}
|
||||||
|
ReflectValue::U8(_) => build_command_ui_value_number(ui, name, cmd),
|
||||||
|
ReflectValue::U16(_) => build_command_ui_value_number(ui, name, cmd),
|
||||||
|
ReflectValue::U32(_) => build_command_ui_value_number(ui, name, cmd),
|
||||||
|
ReflectValue::U64(_) => build_command_ui_value_number(ui, name, cmd),
|
||||||
|
ReflectValue::I8(_) => build_command_ui_value_number(ui, name, cmd),
|
||||||
|
ReflectValue::I16(_) => build_command_ui_value_number(ui, name, cmd),
|
||||||
|
ReflectValue::I32(_) => build_command_ui_value_number(ui, name, cmd),
|
||||||
|
ReflectValue::I64(_) => build_command_ui_value_number(ui, name, cmd),
|
||||||
|
ReflectValue::Vec(_vec) => {}
|
||||||
|
ReflectValue::Str(_) => {}
|
||||||
|
ReflectValue::Bool(_) => {}
|
||||||
|
},
|
||||||
|
ReflectType::Enumeration => {
|
||||||
|
// variants
|
||||||
|
|
||||||
|
for (i, (name, _v)) in cmd.variants().into_iter().enumerate() {
|
||||||
|
if ui.button(name).clicked() {
|
||||||
|
cmd.convert_variant(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// values
|
||||||
|
if let Some((name, cmd)) = cmd.unwrap_variant() {
|
||||||
|
build_command_ui(ui, name, cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ReflectType::Structure => {
|
||||||
|
for (name, cmd) in cmd.fields().into_iter() {
|
||||||
|
build_command_ui(ui, name, cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eframe::App for MyApp {
|
||||||
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
ui.heading(format!("Command: {:?}", self.command));
|
||||||
|
if ui.button("X").clicked() {
|
||||||
|
self.command = Msg::None;
|
||||||
|
}
|
||||||
|
|
||||||
|
build_command_ui(ui, "Msg", &mut self.command);
|
||||||
|
|
||||||
// impl eframe::App for MyApp {
|
|
||||||
// fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
|
||||||
// egui::CentralPanel::default().show(ctx, |ui| {
|
|
||||||
// ui.heading(format!("Commands: {}", self.command_path));
|
|
||||||
// if ui.button("X").clicked() {
|
|
||||||
// self.command_path = "Msg".to_string();
|
|
||||||
// }
|
|
||||||
// if self
|
// if self
|
||||||
// .command_path
|
// .command_path
|
||||||
// .as_bytes()
|
// .as_bytes()
|
||||||
@@ -224,6 +169,6 @@ fn main() -> Result<()> {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// });
|
});
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|||||||
+46
-12
@@ -147,11 +147,17 @@ pub fn reflect_derive(input: TokenStream) -> TokenStream {
|
|||||||
rfe::reflect::ReflectValue::None
|
rfe::reflect::ReflectValue::None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> alloc::vec::Vec<(&str, alloc::boxed::Box<dyn rfe::reflect::Reflect>)> {
|
fn variants(&self) -> alloc::vec::Vec<(&'static str, alloc::boxed::Box<dyn rfe::reflect::Reflect>)> {
|
||||||
alloc::vec::Vec::new()
|
alloc::vec::Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn rfe::reflect::Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn rfe::reflect::Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<alloc::vec::Vec<&mut dyn rfe::reflect::Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -162,7 +168,7 @@ pub fn reflect_derive(input: TokenStream) -> TokenStream {
|
|||||||
|
|
||||||
Data::Enum(data_enum) => {
|
Data::Enum(data_enum) => {
|
||||||
let variants = data_enum.variants;
|
let variants = data_enum.variants;
|
||||||
let as_arms = variants.iter().enumerate().map(|(i, variant)| {
|
let convert_arms = variants.iter().enumerate().map(|(i, variant)| {
|
||||||
let variant_name = &variant.ident;
|
let variant_name = &variant.ident;
|
||||||
|
|
||||||
match &variant.fields {
|
match &variant.fields {
|
||||||
@@ -170,7 +176,6 @@ pub fn reflect_derive(input: TokenStream) -> TokenStream {
|
|||||||
quote! {
|
quote! {
|
||||||
if _i == #i {
|
if _i == #i {
|
||||||
*self = Self::#variant_name;
|
*self = Self::#variant_name;
|
||||||
return Some(self);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -178,11 +183,29 @@ pub fn reflect_derive(input: TokenStream) -> TokenStream {
|
|||||||
quote! {
|
quote! {
|
||||||
if _i == #i {
|
if _i == #i {
|
||||||
*self = Self::#variant_name(Default::default());
|
*self = Self::#variant_name(Default::default());
|
||||||
if let Self::#variant_name(r) = self {
|
|
||||||
return Some(r);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Fields::Named(_) => {
|
||||||
|
panic!("Named fields not supported by Reflect");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let unwrap_arms = variants.iter().enumerate().map(|(_i, variant)| {
|
||||||
|
let variant_name = &variant.ident;
|
||||||
|
let variant_name_s = variant_name.to_string();
|
||||||
|
|
||||||
|
match &variant.fields {
|
||||||
|
Fields::Unit => {
|
||||||
|
quote! {
|
||||||
|
Self::#variant_name => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Fields::Unnamed(_) => {
|
||||||
|
quote! {
|
||||||
|
Self::#variant_name(v) => Some((#variant_name_s, v)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Fields::Named(_) => {
|
Fields::Named(_) => {
|
||||||
panic!("Named fields not supported by Reflect");
|
panic!("Named fields not supported by Reflect");
|
||||||
@@ -198,16 +221,18 @@ pub fn reflect_derive(input: TokenStream) -> TokenStream {
|
|||||||
Fields::Unit => {
|
Fields::Unit => {
|
||||||
quote! {
|
quote! {
|
||||||
{
|
{
|
||||||
|
let name: &'static str = #variant_name_s;
|
||||||
let value: alloc::boxed::Box<dyn rfe::reflect::Reflect> = alloc::boxed::Box::new(Self::#variant_name);
|
let value: alloc::boxed::Box<dyn rfe::reflect::Reflect> = alloc::boxed::Box::new(Self::#variant_name);
|
||||||
variants.push((#variant_name_s, value));
|
variants.push((name, value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Fields::Unnamed(_) => {
|
Fields::Unnamed(_) => {
|
||||||
quote! {
|
quote! {
|
||||||
{
|
{
|
||||||
|
let name: &'static str = #variant_name_s;
|
||||||
let value: alloc::boxed::Box<dyn rfe::reflect::Reflect> = alloc::boxed::Box::new(Self::#variant_name(Default::default()));
|
let value: alloc::boxed::Box<dyn rfe::reflect::Reflect> = alloc::boxed::Box::new(Self::#variant_name(Default::default()));
|
||||||
variants.push((#variant_name_s, value));
|
variants.push((name, value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,15 +264,24 @@ pub fn reflect_derive(input: TokenStream) -> TokenStream {
|
|||||||
rfe::reflect::ReflectValue::None
|
rfe::reflect::ReflectValue::None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> alloc::vec::Vec<(&str, alloc::boxed::Box<dyn rfe::reflect::Reflect>)> {
|
fn variants(&self) -> alloc::vec::Vec<(&'static str, alloc::boxed::Box<dyn rfe::reflect::Reflect>)> {
|
||||||
let mut variants = alloc::vec::Vec::new();
|
let mut variants = alloc::vec::Vec::new();
|
||||||
#(#variant_arms)*
|
#(#variant_arms)*
|
||||||
variants
|
variants
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn rfe::reflect::Reflect> {
|
fn convert_variant(&mut self, _i: usize) {
|
||||||
#(#as_arms)*
|
#(#convert_arms)*
|
||||||
panic!("variant incorrect");
|
}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn rfe::reflect::Reflect)> {
|
||||||
|
match self {
|
||||||
|
#(#unwrap_arms)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<alloc::vec::Vec<&mut dyn rfe::reflect::Reflect>> {
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,6 +111,7 @@ pub type TlmSetId = u16;
|
|||||||
#[cfg_attr(feature = "reflect", derive(Reflect))]
|
#[cfg_attr(feature = "reflect", derive(Reflect))]
|
||||||
pub struct TlmSetItem {
|
pub struct TlmSetItem {
|
||||||
pub target: TargetMsg,
|
pub target: TargetMsg,
|
||||||
|
/// decimation 0 means sends every msg, decimation 1 means sends every other, etc
|
||||||
pub decimation: u16,
|
pub decimation: u16,
|
||||||
pub counter: u16,
|
pub counter: u16,
|
||||||
}
|
}
|
||||||
|
|||||||
+195
-37
@@ -1,10 +1,25 @@
|
|||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
use core::mem::zeroed;
|
||||||
use std::string::ToString;
|
use std::string::ToString;
|
||||||
|
|
||||||
use alloc::boxed::Box;
|
use alloc::boxed::Box;
|
||||||
use alloc::string::String;
|
use alloc::string::String;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
|
use hashbrown::HashMap;
|
||||||
|
|
||||||
|
pub trait Reflect: std::fmt::Debug {
|
||||||
|
fn reflect_type(&self) -> ReflectType;
|
||||||
|
fn type_name(&self) -> &str;
|
||||||
|
fn fields(&mut self) -> Vec<(&str, &mut dyn Reflect)>;
|
||||||
|
fn set_value(&mut self, value: ReflectValue);
|
||||||
|
fn get_value(&self) -> ReflectValue;
|
||||||
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)>;
|
||||||
|
fn convert_variant(&mut self, i: usize);
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)>;
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum ReflectType {
|
pub enum ReflectType {
|
||||||
Value,
|
Value,
|
||||||
Enumeration,
|
Enumeration,
|
||||||
@@ -39,7 +54,13 @@ impl ReflectValue {
|
|||||||
ReflectValue::I32(v) => *v as i64,
|
ReflectValue::I32(v) => *v as i64,
|
||||||
ReflectValue::I64(v) => *v as i64,
|
ReflectValue::I64(v) => *v as i64,
|
||||||
ReflectValue::Vec(_) => 0,
|
ReflectValue::Vec(_) => 0,
|
||||||
ReflectValue::Str(_) => 0,
|
ReflectValue::Str(s) => {
|
||||||
|
if let Ok(v) = s.parse() {
|
||||||
|
v
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
ReflectValue::Bool(b) => {
|
ReflectValue::Bool(b) => {
|
||||||
if *b {
|
if *b {
|
||||||
1
|
1
|
||||||
@@ -62,7 +83,13 @@ impl ReflectValue {
|
|||||||
ReflectValue::I32(v) => *v as u64,
|
ReflectValue::I32(v) => *v as u64,
|
||||||
ReflectValue::I64(v) => *v as u64,
|
ReflectValue::I64(v) => *v as u64,
|
||||||
ReflectValue::Vec(_) => 0,
|
ReflectValue::Vec(_) => 0,
|
||||||
ReflectValue::Str(_) => 0,
|
ReflectValue::Str(s) => {
|
||||||
|
if let Ok(v) = s.parse() {
|
||||||
|
v
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
ReflectValue::Bool(b) => {
|
ReflectValue::Bool(b) => {
|
||||||
if *b {
|
if *b {
|
||||||
1
|
1
|
||||||
@@ -76,7 +103,17 @@ impl ReflectValue {
|
|||||||
pub fn str(self) -> String {
|
pub fn str(self) -> String {
|
||||||
match self {
|
match self {
|
||||||
ReflectValue::Str(s) => s,
|
ReflectValue::Str(s) => s,
|
||||||
_ => "".to_string(),
|
ReflectValue::None => "".to_string(),
|
||||||
|
ReflectValue::U8(v) => v.to_string(),
|
||||||
|
ReflectValue::U16(v) => v.to_string(),
|
||||||
|
ReflectValue::U32(v) => v.to_string(),
|
||||||
|
ReflectValue::U64(v) => v.to_string(),
|
||||||
|
ReflectValue::I8(v) => v.to_string(),
|
||||||
|
ReflectValue::I16(v) => v.to_string(),
|
||||||
|
ReflectValue::I32(v) => v.to_string(),
|
||||||
|
ReflectValue::I64(v) => v.to_string(),
|
||||||
|
ReflectValue::Vec(_v) => "".to_string(),
|
||||||
|
ReflectValue::Bool(v) => v.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,18 +135,12 @@ impl ReflectValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn vec(self) -> Vec<ReflectValue> {
|
pub fn vec(self) -> Vec<ReflectValue> {
|
||||||
|
if let ReflectValue::Vec(v) = self {
|
||||||
|
v
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Reflect: std::fmt::Debug {
|
|
||||||
fn reflect_type(&self) -> ReflectType;
|
|
||||||
fn type_name(&self) -> &str;
|
|
||||||
fn fields(&mut self) -> Vec<(&str, &mut dyn Reflect)>;
|
|
||||||
fn set_value(&mut self, value: ReflectValue);
|
|
||||||
fn get_value(&self) -> ReflectValue;
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)>;
|
|
||||||
fn as_variant(&mut self, i: usize) -> Option<&mut dyn Reflect>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn path_set(reflect: &mut dyn Reflect, path: &str, value: ReflectValue) {
|
pub fn path_set(reflect: &mut dyn Reflect, path: &str, value: ReflectValue) {
|
||||||
@@ -148,6 +179,51 @@ pub fn path_get<'a>(reflect: &'a mut dyn Reflect, path: &str) -> Option<&'a mut
|
|||||||
Some(next)
|
Some(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn _flatten<'a>(reflect: &'a mut dyn Reflect) -> HashMap<String, &'a mut dyn Reflect> {
|
||||||
|
let mut list = HashMap::new();
|
||||||
|
|
||||||
|
match reflect.reflect_type() {
|
||||||
|
ReflectType::Value => match reflect.get_value() {
|
||||||
|
ReflectValue::Vec(_) => {
|
||||||
|
for (i, r) in reflect.as_vec().unwrap().into_iter().enumerate() {
|
||||||
|
let map = _flatten(r);
|
||||||
|
for (sn, v) in map {
|
||||||
|
list.insert(alloc::format!("[{i}].{}", sn), v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
list.insert("".to_string(), reflect);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ReflectType::Enumeration => {
|
||||||
|
if let Some((name, r)) = reflect.unwrap_variant() {
|
||||||
|
let map = _flatten(r);
|
||||||
|
for (sn, v) in map {
|
||||||
|
list.insert(alloc::format!("{}.{}", name, sn), v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ReflectType::Structure => {
|
||||||
|
for (name, r) in reflect.fields() {
|
||||||
|
let map = _flatten(r);
|
||||||
|
for (sn, v) in map {
|
||||||
|
list.insert(alloc::format!("{}.{}", name, sn), v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
list
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn flatten<'a>(reflect: &'a mut dyn Reflect) -> HashMap<String, &'a mut dyn Reflect> {
|
||||||
|
_flatten(reflect)
|
||||||
|
.into_iter()
|
||||||
|
.map(|(k, v)| (k[..k.len() - 1].to_string().replace(".[", "["), v))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
impl Reflect for u8 {
|
impl Reflect for u8 {
|
||||||
fn reflect_type(&self) -> ReflectType {
|
fn reflect_type(&self) -> ReflectType {
|
||||||
ReflectType::Value
|
ReflectType::Value
|
||||||
@@ -169,11 +245,17 @@ impl Reflect for u8 {
|
|||||||
ReflectValue::U8(*self)
|
ReflectValue::U8(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,11 +281,17 @@ impl Reflect for u16 {
|
|||||||
ReflectValue::U16(*self)
|
ReflectValue::U16(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -229,11 +317,17 @@ impl Reflect for u32 {
|
|||||||
ReflectValue::U32(*self)
|
ReflectValue::U32(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -259,11 +353,17 @@ impl Reflect for u64 {
|
|||||||
ReflectValue::U64(*self)
|
ReflectValue::U64(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -289,11 +389,17 @@ impl Reflect for i8 {
|
|||||||
ReflectValue::I8(*self)
|
ReflectValue::I8(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -319,11 +425,17 @@ impl Reflect for i16 {
|
|||||||
ReflectValue::I16(*self)
|
ReflectValue::I16(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -349,11 +461,17 @@ impl Reflect for i32 {
|
|||||||
ReflectValue::I32(*self)
|
ReflectValue::I32(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -379,11 +497,17 @@ impl Reflect for i64 {
|
|||||||
ReflectValue::I64(*self)
|
ReflectValue::I64(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -409,11 +533,17 @@ impl Reflect for String {
|
|||||||
ReflectValue::Str(self.to_string())
|
ReflectValue::Str(self.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -439,11 +569,17 @@ impl Reflect for bool {
|
|||||||
ReflectValue::Bool(*self)
|
ReflectValue::Bool(*self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_vec(&mut self) -> Option<Vec<&mut dyn Reflect>> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -462,18 +598,40 @@ impl<T: Reflect> Reflect for Vec<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn set_value(&mut self, value: ReflectValue) {
|
fn set_value(&mut self, value: ReflectValue) {
|
||||||
*self = value.vec();
|
if let ReflectValue::Vec(v) = value {
|
||||||
|
*self = v
|
||||||
|
.into_iter()
|
||||||
|
.map(|x| {
|
||||||
|
let mut t: T = unsafe { zeroed() };
|
||||||
|
t.set_value(x);
|
||||||
|
t
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_value(&self) -> ReflectValue {
|
fn get_value(&self) -> ReflectValue {
|
||||||
ReflectValue::Bool(*self)
|
ReflectValue::Vec(self.iter().map(|x| x.get_value()).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn variants(&self) -> Vec<(&str, Box<dyn Reflect>)> {
|
fn variants(&self) -> Vec<(&'static str, Box<dyn Reflect>)> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn as_variant(&mut self, _i: usize) -> Option<&mut dyn Reflect> {
|
fn convert_variant(&mut self, _i: usize) {}
|
||||||
|
|
||||||
|
fn unwrap_variant(&mut self) -> Option<(&str, &mut dyn Reflect)> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn as_vec<'a>(&'a mut self) -> Option<Vec<&'a mut dyn Reflect>> {
|
||||||
|
Some(
|
||||||
|
self.iter_mut()
|
||||||
|
.map(|x| {
|
||||||
|
let v: &mut dyn Reflect = x;
|
||||||
|
v
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ use std::{
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use bincode::decode_from_std_read;
|
use bincode::decode_from_std_read;
|
||||||
use log::*;
|
use log::*;
|
||||||
use rfe::to_csv::ToCsvClean;
|
|
||||||
use rfe::{msg::MsgPacket, BINCODE_CONFIG};
|
use rfe::{msg::MsgPacket, BINCODE_CONFIG};
|
||||||
use simple_logger::SimpleLogger;
|
use simple_logger::SimpleLogger;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user