Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature flag for deriving serde macros. The default is off. #4

Merged
merged 3 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ version = "0.1.0"
authors = ["Ryo Kawaguchi <ryo@kawagu.ch>"]
edition = "2018"

[features]
derive_serde = ["webrtc-audio-processing-sys/derive_serde"]

[dependencies]
webrtc-audio-processing-sys = { path = "webrtc-audio-processing-sys" }
5 changes: 4 additions & 1 deletion webrtc-audio-processing-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ version = "0.1.0"
authors = ["Ryo Kawaguchi <ryo@kawagu.ch>"]
edition = "2018"

[features]
derive_serde = ["serde"]

[build-dependencies]
bindgen = "0"
cc = "1.0.38"
failure = "0.1"
regex = "1"

[dependencies]
serde = { version = "1", features = ["derive"] }
serde = { version = "1", features = ["derive"], optional = true }
22 changes: 12 additions & 10 deletions webrtc-audio-processing-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ use std::{
};

// TODO: Consider fixing this with the upstream.
// https://github.com/rust-lang/rust-bindgen/issues/1089
// https://github.com/rust-lang/rust-bindgen/issues/1301
fn add_derives(binding_file: &Path) -> Result<(), Error> {
fn derive_serde(binding_file: &Path) -> Result<(), Error> {
let mut contents = String::new();
File::open(binding_file)?.read_to_string(&mut contents)?;

// Add PartialEq, Serialize and Deserialize to structs.
let contents = Regex::new(r"#\s*\[\s*derive\s*\((?P<d>[^)]+)\)\s*\]\s*pub struct")?
.replace_all(&contents, "#[derive($d, PartialEq, Serialize, Deserialize)]\n pub struct");
// Add Serialize and Deserialize to enums.
let contents = Regex::new(r"#\s*\[\s*derive\s*\((?P<d>[^)]+)\)\s*\]\s*pub enum")?
.replace_all(&contents, "#[derive($d, Serialize, Deserialize)]\n pub enum");
let new_contents = format!(
"use serde::{{Serialize, Deserialize}};\n{}",
Regex::new(r"#\s*\[\s*derive\s*\((?P<d>[^)]+)\)\s*\]\s*pub\s*(?P<s>struct|enum)")?
.replace_all(&contents, "#[derive($d, Serialize, Deserialize)] pub $s")
);

let new_binding_contents = format!("use serde::{{Serialize, Deserialize}};\n{}", contents);
File::create(&binding_file)?.write_all(new_binding_contents.as_bytes())?;
File::create(&binding_file)?.write_all(new_contents.as_bytes())?;

Ok(())
}
Expand Down Expand Up @@ -51,11 +50,14 @@ fn main() {
.rustified_enum(".*")
.derive_debug(true)
.derive_default(true)
.derive_partialeq(true)
.disable_name_namespacing()
.generate()
.expect("Unable to generate bindings")
.write_to_file(&binding_file)
.expect("Couldn't write bindings!");

add_derives(&binding_file).expect("Failed to modify derive macros");
if cfg!(feature = "derive_serde") {
derive_serde(&binding_file).expect("Failed to modify derive macros");
}
}