From 3f73941b4aa69896e597067b8deab1a4b89f57d0 Mon Sep 17 00:00:00 2001 From: Ryo Kawaguchi Date: Thu, 5 Dec 2019 12:37:47 +0900 Subject: [PATCH 1/3] Add a Cargo feature to control derivation of serde's Serialize and Deserialize macro. --- Cargo.toml | 3 +++ webrtc-audio-processing-sys/Cargo.toml | 5 ++++- webrtc-audio-processing-sys/build.rs | 14 +++++++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b0936e9..5804ba5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,8 @@ version = "0.1.0" authors = ["Ryo Kawaguchi "] edition = "2018" +[features] +derive_serde = ["webrtc-audio-processing-sys/derive_serde"] + [dependencies] webrtc-audio-processing-sys = { path = "webrtc-audio-processing-sys" } diff --git a/webrtc-audio-processing-sys/Cargo.toml b/webrtc-audio-processing-sys/Cargo.toml index 6d596e3..da42dd9 100644 --- a/webrtc-audio-processing-sys/Cargo.toml +++ b/webrtc-audio-processing-sys/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["Ryo Kawaguchi "] edition = "2018" +[features] +derive_serde = ["serde"] + [build-dependencies] bindgen = "0" cc = "1.0.38" @@ -11,4 +14,4 @@ failure = "0.1" regex = "1" [dependencies] -serde = { version = "1", features = ["derive"] } +serde = { version = "1", features = ["derive"], optional = true } diff --git a/webrtc-audio-processing-sys/build.rs b/webrtc-audio-processing-sys/build.rs index 9759cda..648aac4 100644 --- a/webrtc-audio-processing-sys/build.rs +++ b/webrtc-audio-processing-sys/build.rs @@ -13,12 +13,16 @@ fn add_derives(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. + // Add PartialEq to structs. + // Used for checking partial equality of `Config` struct. let contents = Regex::new(r"#\s*\[\s*derive\s*\((?P[^)]+)\)\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[^)]+)\)\s*\]\s*pub enum")? - .replace_all(&contents, "#[derive($d, Serialize, Deserialize)]\n pub enum"); + .replace_all(&contents, "#[derive($d, PartialEq)]\n pub struct"); + + #[cfg(feature = "derive_serde")] { + // Add Serialize and Deserialize derive to enums and structs. + let contents = Regex::new(r"#\s*\[\s*derive\s*\((?P[^)]+)\)\s*\]\s*pub struct|enum")? + .replace_all(&contents, "#[derive($d, Serialize, Deserialize)]\n pub struct"); + } let new_binding_contents = format!("use serde::{{Serialize, Deserialize}};\n{}", contents); File::create(&binding_file)?.write_all(new_binding_contents.as_bytes())?; From 6002765abe00f59640943af9bd24a06f37002149 Mon Sep 17 00:00:00 2001 From: Ryo Kawaguchi Date: Thu, 5 Dec 2019 16:13:56 +0900 Subject: [PATCH 2/3] Fix regexp. --- webrtc-audio-processing-sys/build.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/webrtc-audio-processing-sys/build.rs b/webrtc-audio-processing-sys/build.rs index 648aac4..6ca34a7 100644 --- a/webrtc-audio-processing-sys/build.rs +++ b/webrtc-audio-processing-sys/build.rs @@ -15,17 +15,22 @@ fn add_derives(binding_file: &Path) -> Result<(), Error> { // Add PartialEq to structs. // Used for checking partial equality of `Config` struct. - let contents = Regex::new(r"#\s*\[\s*derive\s*\((?P[^)]+)\)\s*\]\s*pub struct")? - .replace_all(&contents, "#[derive($d, PartialEq)]\n pub struct"); + contents = Regex::new(r"#\s*\[\s*derive\s*\((?P[^)]+)\)\s*\]\s*pub\s*struct")? + .replace_all(&contents, "#[derive($d, PartialEq)] pub struct") + .to_string(); - #[cfg(feature = "derive_serde")] { - // Add Serialize and Deserialize derive to enums and structs. - let contents = Regex::new(r"#\s*\[\s*derive\s*\((?P[^)]+)\)\s*\]\s*pub struct|enum")? - .replace_all(&contents, "#[derive($d, Serialize, Deserialize)]\n pub struct"); + #[cfg(feature = "derive_serde")] + { + // Add Serialize and Deserialize to enums and structs. + contents = format!( + "use serde::{{Serialize, Deserialize}};\n{}", + Regex::new(r"#\s*\[\s*derive\s*\((?P[^)]+)\)\s*\]\s*pub\s*(?Pstruct|enum)")? + .replace_all(&contents, "#[derive($d, Serialize, Deserialize)] pub $s") + ) + .to_string(); } - 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(contents.as_bytes())?; Ok(()) } From 14f457607657418e04140fa861d5b903c512790a Mon Sep 17 00:00:00 2001 From: Ryo Kawaguchi Date: Thu, 5 Dec 2019 16:40:57 +0900 Subject: [PATCH 3/3] Use derive_partialeq. --- webrtc-audio-processing-sys/build.rs | 31 +++++++++++----------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/webrtc-audio-processing-sys/build.rs b/webrtc-audio-processing-sys/build.rs index 6ca34a7..b0bb50e 100644 --- a/webrtc-audio-processing-sys/build.rs +++ b/webrtc-audio-processing-sys/build.rs @@ -8,29 +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 to structs. - // Used for checking partial equality of `Config` struct. - contents = Regex::new(r"#\s*\[\s*derive\s*\((?P[^)]+)\)\s*\]\s*pub\s*struct")? - .replace_all(&contents, "#[derive($d, PartialEq)] pub struct") - .to_string(); + let new_contents = format!( + "use serde::{{Serialize, Deserialize}};\n{}", + Regex::new(r"#\s*\[\s*derive\s*\((?P[^)]+)\)\s*\]\s*pub\s*(?Pstruct|enum)")? + .replace_all(&contents, "#[derive($d, Serialize, Deserialize)] pub $s") + ); - #[cfg(feature = "derive_serde")] - { - // Add Serialize and Deserialize to enums and structs. - contents = format!( - "use serde::{{Serialize, Deserialize}};\n{}", - Regex::new(r"#\s*\[\s*derive\s*\((?P[^)]+)\)\s*\]\s*pub\s*(?Pstruct|enum)")? - .replace_all(&contents, "#[derive($d, Serialize, Deserialize)] pub $s") - ) - .to_string(); - } - - File::create(&binding_file)?.write_all(contents.as_bytes())?; + File::create(&binding_file)?.write_all(new_contents.as_bytes())?; Ok(()) } @@ -60,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"); + } }