Add support for specifying line ending style (#568)

This commit is contained in:
Tom Solberg
2020-08-26 15:28:17 +02:00
committed by GitHub
parent 83cdbd897f
commit 3ca0e75ed9
30 changed files with 307 additions and 2 deletions
+58
View File
@@ -48,6 +48,61 @@ impl FromStr for Language {
deserialize_enum_str!(Language);
/// Controls what type of line endings are used in the generated code.
#[derive(Debug, Clone, Copy)]
pub enum LineEndingStyle {
/// Use Unix-style linefeed characters
LF,
/// Use classic Mac-style carriage-return characters
CR,
/// Use Windows-style carriage-return and linefeed characters
CRLF,
/// Use the native mode for the platform: CRLF on Windows, LF everywhere else.
Native,
}
impl Default for LineEndingStyle {
fn default() -> Self {
LineEndingStyle::LF
}
}
impl LineEndingStyle {
pub fn as_str(&self) -> &'static str {
match self {
Self::LF => "\n",
Self::CR => "\r",
Self::CRLF => "\r\n",
Self::Native => {
#[cfg(target_os = "windows")]
{
Self::CRLF.as_str()
}
#[cfg(not(target_os = "windows"))]
{
Self::LF.as_str()
}
}
}
}
}
impl FromStr for LineEndingStyle {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_ref() {
"native" => Ok(Self::Native),
"lf" => Ok(Self::LF),
"crlf" => Ok(Self::CRLF),
"cr" => Ok(Self::CR),
_ => Err(format!("Unrecognized line ending style: '{}'.", s)),
}
}
}
deserialize_enum_str!(LineEndingStyle);
/// A style of braces to use for generating code.
#[derive(Debug, Clone, PartialEq)]
pub enum Braces {
@@ -745,6 +800,8 @@ pub struct Config {
pub line_length: usize,
/// The amount of spaces in a tab
pub tab_width: usize,
/// The type of line endings to generate
pub line_endings: LineEndingStyle,
/// The language to output bindings for
pub language: Language,
/// Include preprocessor defines in C bindings to ensure C++ compatibility
@@ -801,6 +858,7 @@ impl Default for Config {
braces: Braces::SameLine,
line_length: 100,
tab_width: 2,
line_endings: LineEndingStyle::default(),
language: Language::Cxx,
cpp_compat: false,
style: Style::Type,
+4 -1
View File
@@ -65,6 +65,7 @@ pub struct SourceWriter<'a, F: Write> {
line_number: usize,
max_line_length: usize,
}
pub type MeasureWriter<'a> = SourceWriter<'a, NullFile>;
impl<'a, F: Write> SourceWriter<'a, F> {
@@ -137,7 +138,9 @@ impl<'a, F: Write> SourceWriter<'a, F> {
}
pub fn new_line(&mut self) {
writeln!(self.out).unwrap();
self.out
.write(self.bindings.config.line_endings.as_str().as_bytes())
.unwrap();
self.line_started = false;
self.line_length = 0;
self.line_number += 1;
+1 -1
View File
@@ -35,7 +35,7 @@ braces = "SameLine"
line_length = 100
tab_width = 2
documentation_style = "auto"
line-endings = "LF" # also "CR", "CRLF", "Native"
+1
View File
@@ -0,0 +1 @@
#include <stdarg.h>
@@ -0,0 +1 @@
#include <stdarg.h>
+11
View File
@@ -0,0 +1,11 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct Dummy {
int32_t x;
float y;
} Dummy;
void root(Dummy d);
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct Dummy {
int32_t x;
float y;
} Dummy;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(Dummy d);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+11
View File
@@ -0,0 +1,11 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct Dummy {
int32_t x;
float y;
} Dummy;
void root(Dummy d);
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct Dummy {
int32_t x;
float y;
} Dummy;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(Dummy d);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+1
View File
@@ -0,0 +1 @@
#include <stdarg.h>
+1
View File
@@ -0,0 +1 @@
#include <stdarg.h>
+1
View File
@@ -0,0 +1 @@
#include <cstdarg>
+11
View File
@@ -0,0 +1,11 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
int32_t x;
float y;
} Dummy;
void root(Dummy d);
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
int32_t x;
float y;
} Dummy;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(Dummy d);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+15
View File
@@ -0,0 +1,15 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>
struct Dummy {
int32_t x;
float y;
};
extern "C" {
void root(Dummy d);
} // extern "C"
+11
View File
@@ -0,0 +1,11 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
int32_t x;
float y;
} Dummy;
void root(Dummy d);
+19
View File
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
int32_t x;
float y;
} Dummy;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(Dummy d);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+15
View File
@@ -0,0 +1,15 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>
struct Dummy {
int32_t x;
float y;
};
extern "C" {
void root(Dummy d);
} // extern "C"
+1
View File
@@ -0,0 +1 @@
#include <stdarg.h>
@@ -0,0 +1 @@
#include <stdarg.h>
+11
View File
@@ -0,0 +1,11 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
struct Dummy {
int32_t x;
float y;
};
void root(struct Dummy d);
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
struct Dummy {
int32_t x;
float y;
};
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(struct Dummy d);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+11
View File
@@ -0,0 +1,11 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
struct Dummy {
int32_t x;
float y;
};
void root(struct Dummy d);
@@ -0,0 +1,19 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
struct Dummy {
int32_t x;
float y;
};
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(struct Dummy d);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+8
View File
@@ -0,0 +1,8 @@
#[repr(C)]
struct Dummy {
x: i32,
y: f32,
}
#[no_mangle]
pub extern "C" fn root(d: Dummy) {}
+1
View File
@@ -0,0 +1 @@
line_endings = "CR"
+8
View File
@@ -0,0 +1,8 @@
#[repr(C)]
struct Dummy {
x: i32,
y: f32,
}
#[no_mangle]
pub extern "C" fn root(d: Dummy) {}
+1
View File
@@ -0,0 +1 @@
line_endings = "CRLF"
+8
View File
@@ -0,0 +1,8 @@
#[repr(C)]
struct Dummy {
x: i32,
y: f32,
}
#[no_mangle]
pub extern "C" fn root(d: Dummy) {}
+1
View File
@@ -0,0 +1 @@
line_endings = "LF"