From 607d3d0bff95d4a670c04e071ddee9dd8590f708 Mon Sep 17 00:00:00 2001 From: Kozlov Maxim Date: Fri, 4 Aug 2023 10:37:40 +0600 Subject: [PATCH] restructured add_in description --- src/lib.rs | 4 +- src/{my_add_in.rs => my_add_in/func.rs} | 128 ++---------------------- src/my_add_in/mod.rs | 90 +++++++++++++++++ src/my_add_in/props.rs | 37 +++++++ 4 files changed, 139 insertions(+), 120 deletions(-) rename src/{my_add_in.rs => my_add_in/func.rs} (54%) create mode 100644 src/my_add_in/mod.rs create mode 100644 src/my_add_in/props.rs diff --git a/src/lib.rs b/src/lib.rs index 946cc63..aea9e5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,8 +4,8 @@ mod ffi; mod my_add_in; use add_in::AddIn; -use my_add_in::AddInDescription; +use my_add_in::MyAddInDescription; pub fn init_my_add_in() -> impl AddIn { - AddInDescription::new() + MyAddInDescription::new() } diff --git a/src/my_add_in.rs b/src/my_add_in/func.rs similarity index 54% rename from src/my_add_in.rs rename to src/my_add_in/func.rs index 3c10452..055859d 100644 --- a/src/my_add_in.rs +++ b/src/my_add_in/func.rs @@ -1,8 +1,6 @@ -use crate::add_in::{ - AddIn, ComponentFuncDescription, ComponentPropDescription, -}; +use super::MyAddInDescription; +use crate::add_in::ComponentFuncDescription; use crate::ffi::{ - connection::Connection, types::ParamValue, utils::{from_os_string, os_string}, }; @@ -14,45 +12,18 @@ use log4rs::{ encode::pattern::PatternEncoder, Config, }; -use std::{path::PathBuf, sync::Arc, thread, time::Duration}; -use utf16_lit::utf16_null; +use std::{path::PathBuf, thread, time::Duration}; pub struct FunctionListElement { - description: ComponentFuncDescription, - callback: - fn(&mut AddInDescription, &[ParamValue]) -> Result>, + pub description: ComponentFuncDescription, + pub callback: fn( + &mut MyAddInDescription, + &[ParamValue], + ) -> Result>, } -pub struct PropListElement { - description: ComponentPropDescription, - get_callback: Option Option>, - set_callback: Option bool>, -} - -pub struct AddInDescription { - name: &'static [u16], - connection: Arc>, - log_handle: Option, - - functions: Vec, - props: Vec, - some_prop_container: i32, -} - -impl AddInDescription { - pub fn new() -> Self { - Self { - name: &utf16_null!("MyAddIn"), - connection: Arc::new(None), - log_handle: None, - functions: Self::generate_func_list(), - props: Self::generate_prop_list(), - - some_prop_container: 0, - } - } - - fn generate_func_list() -> Vec { +impl MyAddInDescription { + pub fn generate_func_list() -> Vec { vec![ FunctionListElement { description: ComponentFuncDescription::new::<0>( @@ -89,32 +60,6 @@ impl AddInDescription { ] } - fn generate_prop_list() -> Vec { - vec![PropListElement { - description: ComponentPropDescription { - names: &["prop"], - readable: true, - writable: true, - }, - get_callback: Some(Self::get_prop), - set_callback: Some(Self::set_prop), - }] - } - - fn get_prop(&self) -> Option { - Some(ParamValue::I32(self.some_prop_container)) - } - - fn set_prop(&mut self, value: &ParamValue) -> bool { - match value { - ParamValue::I32(val) => { - self.some_prop_container = *val; - true - } - _ => false, - } - } - fn iterate( &mut self, _params: &[ParamValue], @@ -188,56 +133,3 @@ impl AddInDescription { Ok(None) } } - -impl AddIn for AddInDescription { - fn init(&mut self, interface: &'static Connection) -> bool { - interface.set_event_buffer_depth(10); - self.connection = Arc::new(Some(interface)); - self.some_prop_container = 100; - true - } - - fn add_in_name(&self) -> &'static [u16] { - self.name - } - - fn call_function( - &mut self, - name: &str, - params: &[ParamValue], - ) -> Result> { - let func = self - .functions - .iter() - .find(|el| el.description.names.iter().any(|n| n == &name)); - - let Some(func) = func.map(|el| el.callback) else { return Err(eyre!("No function with such name")) }; - func(self, params) - } - - fn get_parameter(&self, name: &str) -> Option { - let prop = self - .props - .iter() - .find(|el| el.description.names.iter().any(|n| n == &name)); - let Some(Some(get)) = prop.map(|el| el.get_callback) else { return None }; - get(self) - } - - fn set_parameter(&mut self, name: &str, value: &ParamValue) -> bool { - let prop = self - .props - .iter() - .find(|el| el.description.names.iter().any(|n| n == &name)); - let Some(Some(set)) = prop.map(|el| el.set_callback) else { return false }; - set(self, value) - } - - fn list_functions(&self) -> Vec<&ComponentFuncDescription> { - self.functions.iter().map(|el| &el.description).collect() - } - - fn list_parameters(&self) -> Vec<&ComponentPropDescription> { - self.props.iter().map(|el| &el.description).collect() - } -} diff --git a/src/my_add_in/mod.rs b/src/my_add_in/mod.rs new file mode 100644 index 0000000..05dd117 --- /dev/null +++ b/src/my_add_in/mod.rs @@ -0,0 +1,90 @@ +mod func; +mod props; + +use crate::add_in::{ + AddIn, ComponentFuncDescription, ComponentPropDescription, +}; +use crate::ffi::{connection::Connection, types::ParamValue}; +use color_eyre::eyre::{eyre, Result}; +use std::sync::Arc; +use utf16_lit::utf16_null; + +use self::func::FunctionListElement; +use self::props::PropListElement; + +pub struct MyAddInDescription { + name: &'static [u16], + connection: Arc>, + log_handle: Option, + + functions: Vec, + props: Vec, + some_prop_container: i32, +} + +impl MyAddInDescription { + pub fn new() -> Self { + Self { + name: &utf16_null!("MyAddIn"), + connection: Arc::new(None), + log_handle: None, + functions: Self::generate_func_list(), + props: Self::generate_prop_list(), + + some_prop_container: 0, + } + } +} + +impl AddIn for MyAddInDescription { + fn init(&mut self, interface: &'static Connection) -> bool { + interface.set_event_buffer_depth(10); + self.connection = Arc::new(Some(interface)); + self.some_prop_container = 100; + true + } + + fn add_in_name(&self) -> &'static [u16] { + self.name + } + + fn call_function( + &mut self, + name: &str, + params: &[ParamValue], + ) -> Result> { + let func = self + .functions + .iter() + .find(|el| el.description.names.iter().any(|n| n == &name)); + + let Some(func) = func.map(|el| el.callback) else { return Err(eyre!("No function with such name")) }; + func(self, params) + } + + fn get_parameter(&self, name: &str) -> Option { + let prop = self + .props + .iter() + .find(|el| el.description.names.iter().any(|n| n == &name)); + let Some(Some(get)) = prop.map(|el| el.get_callback) else { return None }; + get(self) + } + + fn set_parameter(&mut self, name: &str, value: &ParamValue) -> bool { + let prop = self + .props + .iter() + .find(|el| el.description.names.iter().any(|n| n == &name)); + let Some(Some(set)) = prop.map(|el| el.set_callback) else { return false }; + set(self, value) + } + + fn list_functions(&self) -> Vec<&ComponentFuncDescription> { + self.functions.iter().map(|el| &el.description).collect() + } + + fn list_parameters(&self) -> Vec<&ComponentPropDescription> { + self.props.iter().map(|el| &el.description).collect() + } +} diff --git a/src/my_add_in/props.rs b/src/my_add_in/props.rs new file mode 100644 index 0000000..e88fc3d --- /dev/null +++ b/src/my_add_in/props.rs @@ -0,0 +1,37 @@ +use crate::{add_in::ComponentPropDescription, ffi::types::ParamValue}; + +use super::MyAddInDescription; + +pub struct PropListElement { + pub description: ComponentPropDescription, + pub get_callback: Option Option>, + pub set_callback: Option bool>, +} + +impl MyAddInDescription { + pub fn generate_prop_list() -> Vec { + vec![PropListElement { + description: ComponentPropDescription { + names: &["prop"], + readable: true, + writable: true, + }, + get_callback: Some(Self::get_prop), + set_callback: Some(Self::set_prop), + }] + } + + fn get_prop(&self) -> Option { + Some(ParamValue::I32(self.some_prop_container)) + } + + fn set_prop(&mut self, value: &ParamValue) -> bool { + match value { + ParamValue::I32(val) => { + self.some_prop_container = *val; + true + } + _ => false, + } + } +}