diff --git a/Cargo.toml b/Cargo.toml index 19510c226..4b6287509 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,30 +1,3 @@ -workspace = { members = [ "copper_derive", "copper_derive_test", "examples/pluginload", "plugins/camreader"] } - -[package] -name = "copper" -version = "0.2.0" -authors = ["Guillaume Binet "] -edition = "2021" - -[dependencies] -# old stuff -tempdir ="*" -nix = "*" -libc = "*" -spin = "0.9.8" -generic-array = "*" -arrayvec = "*" - -# new stuff -shm = "0.1.0" -lazy_static = "1.4.0" -zerocopy = "0.7.34" -zerocopy-derive = "0.7.34" -petgraph = { version = "0.6.5", features = ["serde", "serde-1", "serde_derive"] } -serde = { version = "1.0", features = ["derive"] } -serde_derive = "1.0" -uom = {version="0.36.0", features=["rational"]} -ron = "0.8.1" - - - +[workspace] +members = [ "copper", "copper_derive", "copper_derive_test", "examples/pluginload", "examples/camreader"] +resolver = "2" diff --git a/copper/Cargo.toml b/copper/Cargo.toml new file mode 100644 index 000000000..7b72a630a --- /dev/null +++ b/copper/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "copper" +version = "0.2.0" +authors = ["Guillaume Binet "] +edition = "2021" + +[dependencies] +# old stuff +tempdir ="*" +nix = "*" +libc = "*" +spin = "0.9.8" +generic-array = "*" +arrayvec = "*" + +# new stuff +shm = "0.1.0" +lazy_static = "1.4.0" +zerocopy = "0.7.34" +zerocopy-derive = "0.7.34" +petgraph = { version = "0.6.5", features = ["serde", "serde-1", "serde_derive"] } +serde = { version = "1.0", features = ["derive"] } +serde_derive = "1.0" +uom = {version="0.36.0", features=["rational"]} +ron = "0.8.1" + + + diff --git a/src/common/mod.rs b/copper/src/common/mod.rs similarity index 100% rename from src/common/mod.rs rename to copper/src/common/mod.rs diff --git a/src/config.rs b/copper/src/config.rs similarity index 85% rename from src/config.rs rename to copper/src/config.rs index c413d2583..94bfa534e 100644 --- a/src/config.rs +++ b/copper/src/config.rs @@ -141,26 +141,30 @@ impl CopperConfig { pub fn add_node(&mut self, node: ConfigNode) -> ConfigNodeId { self.graph.add_node(node).index() as ConfigNodeId } + + pub fn get_node(&self, node_id: ConfigNodeId) -> Option<&ConfigNode> { + self.graph.node_weight(node_id.into()) + } + pub fn connect(&mut self, source: ConfigNodeId, target: ConfigNodeId, msg_type: &str) { self.graph.add_edge(source.into(), target.into(), msg_type.to_string()); } - #[allow(dead_code)] - pub fn serialize(&self) -> String { - let ron = Options::default() + pub fn get_options() -> Options { + Options::default() .with_default_extension(Extensions::IMPLICIT_SOME) .with_default_extension(Extensions::UNWRAP_NEWTYPES) - .with_default_extension(Extensions::UNWRAP_VARIANT_NEWTYPES); - let pretty = ron::ser::PrettyConfig::default(); - let answer = ron.to_string_pretty(&self, pretty).unwrap(); - // RON doesn't put its own options in the serialization format, so we have to add it manually - format!("#![enable(implicit_some)]\n{}\n", answer) + .with_default_extension(Extensions::UNWRAP_VARIANT_NEWTYPES) + } + pub fn serialize(&self) -> String { + let ron = Self::get_options(); + let pretty = ron::ser::PrettyConfig::default(); + ron.to_string_pretty(&self, pretty).unwrap() } - #[allow(dead_code)] pub fn deserialize(ron: &str) -> Self { - ron::de::from_str(ron).expect("Syntax Error in config") + Self::get_options().from_str(ron).expect("Syntax Error in config") } pub fn render(&self, output: &mut dyn std::io::Write) { @@ -192,15 +196,26 @@ mod tests { } #[test] - fn test_serialize() { + fn test_plain_serialize() { let mut config = CopperConfig::new(); let n1 = config.add_node(ConfigNode::new("test1", "package::Plugin1")); let n2 = config.add_node(ConfigNode::new("test2", "package::Plugin2")); config.connect(n1, n2, "msgpkg::MsgType"); let serialized = config.serialize(); - println!("{}", serialized); let deserialized = CopperConfig::deserialize(&serialized); assert_eq!(config.graph.node_count(), deserialized.graph.node_count()); assert_eq!(config.graph.edge_count(), deserialized.graph.edge_count()); } + + #[test] + fn test_serialize_with_params() { + let mut config = CopperConfig::new(); + let mut camera = ConfigNode::new("copper-camera", "camerapkg::Camera").set_base_period(Time::new::(60.into())); + camera.set_param::("resolution-height", 1080.into()); + config.add_node(camera); + let serialized = config.serialize(); + println!("{}", serialized); + let deserialized = CopperConfig::deserialize(&serialized); + assert_eq!(deserialized.get_node(0).unwrap().get_param::("resolution-height").unwrap(), 1080); + } } diff --git a/src/cutask.rs b/copper/src/cutask.rs similarity index 100% rename from src/cutask.rs rename to copper/src/cutask.rs diff --git a/src/lib.rs b/copper/src/lib.rs similarity index 100% rename from src/lib.rs rename to copper/src/lib.rs diff --git a/copper_derive/Cargo.toml b/copper_derive/Cargo.toml index f7e6f1060..302da25c0 100644 --- a/copper_derive/Cargo.toml +++ b/copper_derive/Cargo.toml @@ -12,7 +12,7 @@ quote = "1.0.36" proc-macro2 = { version = "1.0.82" } walkdir = "2.5.0" syntect = "5.2.0" -copper = { path = ".." } +copper = { path = "../copper" } [build-dependencies] cargo_metadata = "0.18.1" diff --git a/copper_derive/src/lib.rs b/copper_derive/src/lib.rs index 365779f8f..f5f0621a5 100644 --- a/copper_derive/src/lib.rs +++ b/copper_derive/src/lib.rs @@ -1,20 +1,18 @@ extern crate proc_macro; -mod format; - -use std::path::PathBuf; -use syn::Fields::{Named, Unnamed, Unit}; -use syn::{parse_macro_input, parse_quote, ItemStruct, LitStr, Field}; -use syn::meta::parser; use proc_macro::TokenStream; +use std::path::PathBuf; use quote::quote; +use syn::{Field, ItemStruct, LitStr, parse_macro_input, parse_quote}; +use syn::Fields::{Named, Unit, Unnamed}; +use syn::meta::parser; use walkdir::WalkDir; -use format::{rustfmt_generated_code, highlight_rust_code}; - use copper::config::CopperConfig; +use format::{highlight_rust_code, rustfmt_generated_code}; +mod format; // Parses the CopperRuntime attribute like #[copper_runtime(config = "path")] #[proc_macro_attribute] @@ -22,7 +20,7 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream { let mut item_struct = parse_macro_input!(input as ItemStruct); let mut config_file: Option = None; - let attribute_config_parser = parser(|meta| { + let attribute_config_parser = parser(|meta| { if meta.path.is_ident("config") { config_file = Some(meta.value()?.parse()?); Ok(()) @@ -40,18 +38,18 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream { let deserialized = CopperConfig::deserialize(&config_content); let name = &item_struct.ident; - + let new_field: Field = parse_quote! { node_instances: (u32, i32) }; - + match &mut item_struct.fields { Named(fields_named) => { fields_named.named.push(new_field); - }, + } Unnamed(fields_unnamed) => { fields_unnamed.unnamed.push(new_field); - }, + } Unit => { // Handle unit structs if necessary } @@ -59,8 +57,7 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream { // Convert the modified struct back into a TokenStream let result = quote! { - #item_struct - + pub #item_struct impl #name { pub fn hello(&self) { println!("Hello from CopperRuntime"); @@ -90,16 +87,16 @@ fn caller_crate_root() -> PathBuf { .into_iter() .filter_entry(|e| !e.file_name().eq_ignore_ascii_case("target")) { - let Ok(entry) = entry else { continue }; + let Ok(entry) = entry else { continue; }; if !entry.file_type().is_file() { continue; } - let Some(file_name) = entry.path().file_name() else { continue }; + let Some(file_name) = entry.path().file_name() else { continue; }; if !file_name.eq_ignore_ascii_case("Cargo.toml") { continue; } let Ok(cargo_toml) = std::fs::read_to_string(entry.path()) else { - continue + continue; }; if cargo_toml .chars() diff --git a/copper_derive_test/Cargo.toml b/copper_derive_test/Cargo.toml index 34d6f1e5c..d9e9ba185 100644 --- a/copper_derive_test/Cargo.toml +++ b/copper_derive_test/Cargo.toml @@ -7,5 +7,5 @@ edition = "2021" copper-derive = { path = "../copper_derive" } [build-dependencies] -copper = { path = "../" } +copper = { path = "../copper" } uom = "0.36.0" diff --git a/copper_derive_test/build.rs b/copper_derive_test/build.rs index 3eb6e1c62..00c498ed9 100644 --- a/copper_derive_test/build.rs +++ b/copper_derive_test/build.rs @@ -13,9 +13,9 @@ fn main() { let mut copperconfig = CopperConfig::new(); let mut camera = ConfigNode::new("copper-camera", "camerapkg::Camera").set_base_period(Time::new::(60.into())); camera.set_param::("resolution-height", 1080.into()); - camera.set_param::("resolution-width", 1920.into()); + // camera.set_param::("resolution-width", 1920.into()); let mut isp = ConfigNode::new("copper-isp", "isppkg::Isp").set_base_period(Time::new::(1.into())); - isp.set_param::("tone", 1.3.into()); + // isp.set_param::("tone", 1.3.into()); let algo = ConfigNode::new("copper-algo", "algopkg::Algo").set_base_period(Time::new::(5.into())); let n1 = copperconfig.add_node(isp); let n2 = copperconfig.add_node(camera); diff --git a/copper_derive_test/src/main.rs b/copper_derive_test/src/main.rs index af582ab17..f90b9c9c9 100644 --- a/copper_derive_test/src/main.rs +++ b/copper_derive_test/src/main.rs @@ -4,7 +4,6 @@ use copper_derive::copper_runtime; struct MyRuntime {} fn main() { - println!("Hello, world!"); let runtime = MyRuntime{node_instances: (1,2)}; runtime.hello(); } diff --git a/plugins/camreader/Cargo.toml b/examples/camreader/Cargo.toml similarity index 100% rename from plugins/camreader/Cargo.toml rename to examples/camreader/Cargo.toml diff --git a/plugins/camreader/src/lib.rs b/examples/camreader/src/lib.rs similarity index 100% rename from plugins/camreader/src/lib.rs rename to examples/camreader/src/lib.rs diff --git a/examples/pluginload/Cargo.toml b/examples/pluginload/Cargo.toml index 3cd68ef2b..0df2e3aa1 100644 --- a/examples/pluginload/Cargo.toml +++ b/examples/pluginload/Cargo.toml @@ -5,8 +5,8 @@ edition = "2021" [dependencies] uom = {version="0.36.0", features=["rational"]} -copper = { path = "../.." } -camreader = { path = "../../plugins/camreader" } +copper = { path = "../../copper" } +camreader = { path = "../camreader" } [build-dependencies] cargo_metadata = "0.18.1" diff --git a/examples/pluginload/src/main.rs b/examples/pluginload/src/main.rs index a233649a8..79681ec80 100644 --- a/examples/pluginload/src/main.rs +++ b/examples/pluginload/src/main.rs @@ -1,27 +1,16 @@ -use copper::register_plugin; use copper::config::CopperConfig; use copper::config::ConfigNode; -use copper::config::Value; -use copper::core::Plugin; use uom::si::rational::Time; use uom::si::time::second; -#[derive(Default)] -struct MyPlugin; - -impl Plugin for MyPlugin { -} - -register_plugin!(MyPlugin); - fn main() { let mut copperconfig = CopperConfig::new(); let mut camera = ConfigNode::new("copper-camera", "camerapkg::Camera").set_base_period(Time::new::(60.into())); - camera.set_param("resolution-height", 1080.into()); - camera.set_param("resolution-width", 1920.into()); + camera.set_param::("resolution-height", 1080); + camera.set_param::("resolution-width", 1920); let mut isp = ConfigNode::new("copper-isp", "isppkg::Isp").set_base_period(Time::new::(1.into())); - isp.set_param("tone", 1.3.into()); + isp.set_param::("tone", 1.3); let algo = ConfigNode::new("copper-algo", "algopkg::Algo").set_base_period(Time::new::(5.into())); let n1 = copperconfig.add_node(isp); let n2 = copperconfig.add_node(camera);