Skip to content

Commit

Permalink
WIP for the derive part, types generated.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed May 20, 2024
1 parent 62f8d15 commit 1ae37ed
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
25 changes: 21 additions & 4 deletions copper/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::collections::HashMap;
use std::iter::Map;
use std::path::Iter;

use petgraph::dot::Config as PetConfig;
use petgraph::dot::Dot;
use petgraph::stable_graph::StableDiGraph;
use petgraph::graph::NodeIndex;
use petgraph::stable_graph::{NodeIndices, StableDiGraph};
use ron::extensions::Extensions;
use ron::Options;
use ron::value::Value as RonValue;
use ron::Options;
use serde::{Deserialize, Serialize};
use uom::si::rational::Time;
use uom::si::time::nanosecond;
Expand Down Expand Up @@ -98,6 +101,10 @@ impl ConfigNode {
self
}

pub fn get_type_name(&self) -> &str {
self.type_name.as_ref().unwrap()
}

#[allow(dead_code)]
pub fn base_period(&self) -> Option<Time> {
self.base_period_ns
Expand Down Expand Up @@ -133,12 +140,15 @@ pub struct CopperConfig {
pub graph: StableDiGraph<ConfigNode, String, ConfigNodeId>,
}

impl CopperConfig {
pub fn new() -> Self {
impl Default for CopperConfig {
fn default() -> Self {
CopperConfig {
graph: StableDiGraph::new(),
}
}
}

impl CopperConfig {
pub fn add_node(&mut self, node: ConfigNode) -> ConfigNodeId {
self.graph.add_node(node).index() as ConfigNodeId
}
Expand All @@ -147,6 +157,13 @@ impl CopperConfig {
self.graph.node_weight(node_id.into())
}

pub fn get_all_nodes(&self) -> Vec<&ConfigNode> {
self.graph
.node_indices()
.map(|node| &self.graph[node])
.collect()
}

pub fn connect(&mut self, source: ConfigNodeId, target: ConfigNodeId, msg_type: &str) {
self.graph
.add_edge(source.into(), target.into(), msg_type.to_string());
Expand Down
43 changes: 37 additions & 6 deletions copper_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ extern crate proc_macro;
use copper::config::CopperConfig;
use format::{highlight_rust_code, rustfmt_generated_code};
use proc_macro::TokenStream;
use proc_macro2::Ident;
use quote::quote;
use syn::meta::parser;
use syn::punctuated::Punctuated;
use syn::Fields::{Named, Unit, Unnamed};
use syn::{parse_macro_input, parse_quote, Field, ItemStruct, LitStr};
use syn::{
parse_macro_input, parse_quote, parse_str, Field, FieldMutability, ItemStruct, LitStr, Type,
TypeTuple,
};

mod format;
mod utils;
Expand Down Expand Up @@ -34,15 +39,41 @@ pub fn copper_runtime(args: TokenStream, input: TokenStream) -> TokenStream {
config_full_path.push(config_file);
let config_content = std::fs::read_to_string(&config_full_path)
.unwrap_or_else(|_| panic!("Failed to read configuration file: {:?}", &config_full_path));
println!("Config content:\n {}", config_content);
let deserialized = CopperConfig::deserialize(&config_content);

let name = &item_struct.ident;
let copper_config = CopperConfig::deserialize(&config_content);
let all_node_configs = copper_config.get_all_nodes();

// Collect all the type names used by our configs.
let all_configs_type_names = all_node_configs
.iter()
.map(|node_config| node_config.get_type_name());

let new_field: Field = parse_quote! {
node_instances: (u32, i32)
// Transform them as Rust types
let all_configs_types: Vec<Type> = all_configs_type_names
.map(|name| {
println!("Found type: {}", name);
parse_str(name).unwrap()
})
.collect();

// Build the tuple of all those types
let tuple_types = TypeTuple {
paren_token: Default::default(),
elems: Punctuated::from_iter(all_configs_types),
};

// add that to a new field
let new_field = Field {
attrs: Vec::new(),
vis: syn::Visibility::Inherited,
mutability: FieldMutability::None,
ident: Some(Ident::new("node_instances", proc_macro2::Span::call_site())),
colon_token: Some(syn::Token![:](proc_macro2::Span::call_site())),
ty: Type::Tuple(tuple_types),
};

let name = &item_struct.ident;

match &mut item_struct.fields {
Named(fields_named) => {
fields_named.named.push(new_field);
Expand Down
2 changes: 2 additions & 0 deletions copper_derive_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ edition = "2021"

[dependencies]
copper-derive = { path = "../copper_derive" }
simplelogger = { path = "../examples/simplelogger" }
v4lsrc = { path = "../examples/v4lsrc" }

[build-dependencies]
copper = { path = "../copper" }
Expand Down
23 changes: 10 additions & 13 deletions copper_derive_test/copperconfig.ron
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@
graph: (
nodes: [
(
instance_name: "copper-isp",
type_name: "isppkg::Isp",
instance_name: "camera",
type_name: "v4lsrc::Video4LinuxSource",
base_period_ns: 1000000000,
instance_config: {
"device": "/dev/video0",
},
),
(
instance_name: "camera",
type_name: "camerapkg::Camera",
instance_name: "logger",
type_name: "simplelogger::SimpleLogger",
base_period_ns: 60000000000,
instance_config: {
"resolution-height": 1080,
"path": "/tmp/stuff.cu",
},
),
(
instance_name: "copper-algo",
type_name: "algopkg::Algo",
base_period_ns: 5000000000,
),
],
node_holes: [],
edge_property: directed,
edges: [
(1, 0, "imgmsgpkg::Image"),
(0, 2, "imgmsgpkg::Image"),
(0, 1, "v4lsrc::ImageMsg"),
],
),
)
)

0 comments on commit 1ae37ed

Please sign in to comment.