Skip to content

Commit

Permalink
Convert config fields header, trailer, after_includes to text fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviojs committed Jun 18, 2024
1 parent 3162981 commit c158749
Show file tree
Hide file tree
Showing 95 changed files with 147 additions and 1,299 deletions.
6 changes: 3 additions & 3 deletions src/bindgen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Builder {

#[allow(unused)]
pub fn with_header<S: AsRef<str>>(mut self, header: S) -> Builder {
self.config.header = Some(String::from(header.as_ref()));
self.config.header = String::from(header.as_ref()).into();
self
}

Expand All @@ -63,13 +63,13 @@ impl Builder {

#[allow(unused)]
pub fn with_after_include<S: AsRef<str>>(mut self, line: S) -> Builder {
self.config.after_includes = Some(String::from(line.as_ref()));
self.config.after_includes = String::from(line.as_ref()).into();
self
}

#[allow(unused)]
pub fn with_trailer<S: AsRef<str>>(mut self, trailer: S) -> Builder {
self.config.trailer = Some(String::from(trailer.as_ref()));
self.config.trailer = String::from(trailer.as_ref()).into();
self
}

Expand Down
84 changes: 78 additions & 6 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,22 +894,94 @@ pub struct CythonConfig {
pub cimports: BTreeMap<String, Vec<String>>,
}

/// A line field can be a single line `field = "line"` or a struct `field = { language = "C", line = "struct" }`.
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum Line {
AnyBackend(String),
WithBackend { language: Language, line: String },
}
impl Line {
pub fn line_for(&self, target: Language) -> Option<&str> {
match *self {
Line::AnyBackend(ref line) => Some(line.as_str()),
Line::WithBackend { language, ref line } if language == target => Some(line.as_str()),
_ => None,
}
}
}
impl From<String> for Line {
fn from(value: String) -> Self {
Self::AnyBackend(value)
}
}

/// A text field can be a line field or a sequence of lines or structs:
/// ```toml
/// field = [
/// "sequence of lines",
/// { language = "C", line = "or structs" },
/// ]
/// ```
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum Text {
Line(Line),
Lines(Vec<Line>),
}
impl Text {
pub fn is_empty(&self) -> bool {
match *self {
Text::Line(_) => false,
Text::Lines(ref lines) => lines.is_empty(),
}
}
pub fn text_for(&self, target: Language) -> Vec<&str> {
let mut text = Vec::new();
match self {
Text::Line(line) => {
if let Some(s) = line.line_for(target) {
text.push(s);
}
}
Text::Lines(lines) => {
for line in lines.iter() {
if let Some(s) = line.line_for(target) {
text.push(s);
}
}
}
}
text
}
}
impl From<String> for Text {
fn from(value: String) -> Self {
Self::Line(value.into())
}
}
impl Default for Text {
fn default() -> Self {
Self::Lines(Vec::new())
}
}

/// A collection of settings to customize the generated bindings.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
#[serde(default)]
pub struct Config {
/// Optional text to output at the beginning of the file
pub header: Option<String>,
pub header: Text,
/// A list of additional includes to put at the beginning of the generated header
pub includes: Vec<String>,
/// A list of additional system includes to put at the beginning of the generated header
pub sys_includes: Vec<String>,
/// Optional verbatim code added after the include blocks
pub after_includes: Option<String>,
pub after_includes: Text,
/// Optional text to output at the end of the file
pub trailer: Option<String>,
pub trailer: Text,
/// Optional name to use for an include guard
pub include_guard: Option<String>,
/// Add a `#pragma once` guard
Expand Down Expand Up @@ -1032,11 +1104,11 @@ pub struct Config {
impl Default for Config {
fn default() -> Config {
Config {
header: None,
header: Text::default(),
includes: Vec::new(),
sys_includes: Vec::new(),
after_includes: None,
trailer: None,
after_includes: Text::default(),
trailer: Text::default(),
include_guard: None,
pragma_once: false,
autogen_warning: None,
Expand Down
8 changes: 4 additions & 4 deletions src/bindgen/language_backend/clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
write!(out, "/* Package version: {} */", package_version);
out.new_line();
}
if let Some(ref f) = self.config.header {
for line in self.config.header.text_for(self.config.language) {
out.new_line_if_not_start();
write!(out, "{}", f);
write!(out, "{}", line);
out.new_line();
}
if let Some(f) = self.config.include_guard() {
Expand Down Expand Up @@ -379,7 +379,7 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
if self.config.no_includes
&& self.config.sys_includes().is_empty()
&& self.config.includes().is_empty()
&& self.config.after_includes.is_none()
&& self.config.after_includes.is_empty()
{
return;
}
Expand Down Expand Up @@ -439,7 +439,7 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
out.new_line();
}

if let Some(ref line) = self.config.after_includes {
for line in self.config.after_includes.text_for(self.config.language) {
write!(out, "{}", line);
out.new_line();
}
Expand Down
8 changes: 4 additions & 4 deletions src/bindgen/language_backend/cython.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ impl LanguageBackend for CythonLanguageBackend<'_> {
write!(out, "''' Package version: {} '''", package_version);
out.new_line();
}
if let Some(ref f) = self.config.header {
for line in self.config.header.text_for(self.config.language) {
out.new_line_if_not_start();
write!(out, "{}", f);
write!(out, "{}", line);
out.new_line();
}

Expand All @@ -72,7 +72,7 @@ impl LanguageBackend for CythonLanguageBackend<'_> {
&& self.config.sys_includes().is_empty()
&& self.config.includes().is_empty()
&& (self.config.cython.cimports.is_empty())
&& self.config.after_includes.is_none()
&& self.config.after_includes.is_empty()
{
return;
}
Expand All @@ -98,7 +98,7 @@ impl LanguageBackend for CythonLanguageBackend<'_> {
out.new_line();
}

if let Some(ref line) = &self.config.after_includes {
for line in self.config.after_includes.text_for(self.config.language) {
write!(out, "{}", line);
out.new_line();
}
Expand Down
6 changes: 3 additions & 3 deletions src/bindgen/language_backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ pub trait LanguageBackend: Sized {
}

fn write_trailer<W: Write>(&mut self, out: &mut SourceWriter<W>, b: &Bindings) {
if let Some(ref f) = b.config.trailer {
for line in b.config.trailer.text_for(b.config.language) {
out.new_line_if_not_start();
write!(out, "{}", f);
if !f.ends_with('\n') {
write!(out, "{}", line);
if !line.ends_with('\n') {
out.new_line();
}
}
Expand Down
14 changes: 0 additions & 14 deletions tests/expectations/box.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down
14 changes: 0 additions & 14 deletions tests/expectations/box.compat.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down
10 changes: 0 additions & 10 deletions tests/expectations/box.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <cstdarg>
Expand Down
14 changes: 0 additions & 14 deletions tests/expectations/box.pyx
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#if 0
''' '
#endif
#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif
#if 0
' '''
#endif


from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
Expand Down
14 changes: 0 additions & 14 deletions tests/expectations/box_both.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down
14 changes: 0 additions & 14 deletions tests/expectations/box_both.compat.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down
14 changes: 0 additions & 14 deletions tests/expectations/box_tag.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down
14 changes: 0 additions & 14 deletions tests/expectations/box_tag.compat.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#if 0
''' '
#endif

#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif

#if 0
' '''
#endif


#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
Expand Down
14 changes: 0 additions & 14 deletions tests/expectations/box_tag.pyx
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
#if 0
''' '
#endif
#ifdef __cplusplus
template <typename T>
using Box = T*;
#endif
#if 0
' '''
#endif


from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
Expand Down
Loading

0 comments on commit c158749

Please sign in to comment.