Skip to content

Commit

Permalink
Reorg the tree + fixed macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed May 13, 2024
1 parent f59fac3 commit e456706
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 81 deletions.
33 changes: 3 additions & 30 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
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"
28 changes: 28 additions & 0 deletions copper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "copper"
version = "0.2.0"
authors = ["Guillaume Binet <[email protected]>"]
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"



File renamed without changes.
39 changes: 27 additions & 12 deletions src/config.rs → copper/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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::<second>(60.into()));
camera.set_param::<Value>("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::<i32>("resolution-height").unwrap(), 1080);
}
}
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion copper_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 15 additions & 18 deletions copper_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
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]
pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {
let mut item_struct = parse_macro_input!(input as ItemStruct);

let mut config_file: Option<LitStr> = 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(())
Expand All @@ -40,27 +38,26 @@ 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
}
};

// 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");
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion copper_derive_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"
copper-derive = { path = "../copper_derive" }

[build-dependencies]
copper = { path = "../" }
copper = { path = "../copper" }
uom = "0.36.0"
4 changes: 2 additions & 2 deletions copper_derive_test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<second>(60.into()));
camera.set_param::<Value>("resolution-height", 1080.into());
camera.set_param::<Value>("resolution-width", 1920.into());
// camera.set_param::<Value>("resolution-width", 1920.into());
let mut isp = ConfigNode::new("copper-isp", "isppkg::Isp").set_base_period(Time::new::<second>(1.into()));
isp.set_param::<Value>("tone", 1.3.into());
// isp.set_param::<Value>("tone", 1.3.into());
let algo = ConfigNode::new("copper-algo", "algopkg::Algo").set_base_period(Time::new::<second>(5.into()));
let n1 = copperconfig.add_node(isp);
let n2 = copperconfig.add_node(camera);
Expand Down
1 change: 0 additions & 1 deletion copper_derive_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions examples/pluginload/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 3 additions & 14 deletions examples/pluginload/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::<second>(60.into()));
camera.set_param("resolution-height", 1080.into());
camera.set_param("resolution-width", 1920.into());
camera.set_param::<i32>("resolution-height", 1080);
camera.set_param::<i32>("resolution-width", 1920);
let mut isp = ConfigNode::new("copper-isp", "isppkg::Isp").set_base_period(Time::new::<second>(1.into()));
isp.set_param("tone", 1.3.into());
isp.set_param::<f64>("tone", 1.3);
let algo = ConfigNode::new("copper-algo", "algopkg::Algo").set_base_period(Time::new::<second>(5.into()));
let n1 = copperconfig.add_node(isp);
let n2 = copperconfig.add_node(camera);
Expand Down

0 comments on commit e456706

Please sign in to comment.