Skip to content

Commit

Permalink
Add documentation_style to with short and full options
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar authored and emilio committed Nov 2, 2021
1 parent 24c130b commit 0e3f9bd
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,15 @@ documentation = true
# default: "auto"
documentation_style = "doxy"

# How much of the documentation for each item is output.
#
# possible values:
# * "short": Only the first line.
# * "full": The full documentation.
#
# default: "full"
documentation_style = "short"




Expand Down
24 changes: 24 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,27 @@ impl FromStr for DocumentationStyle {

deserialize_enum_str!(DocumentationStyle);

/// How much of the documentation to include in the header file.
#[derive(Debug, Clone, Copy)]
pub enum DocumentationLength {
Short,
Full,
}

impl FromStr for DocumentationLength {
type Err = String;

fn from_str(s: &str) -> Result<DocumentationLength, Self::Err> {
match s.to_lowercase().as_ref() {
"short" => Ok(DocumentationLength::Short),
"full" => Ok(DocumentationLength::Full),
_ => Err(format!("Unrecognized documentation style: '{}'.", s)),
}
}
}

deserialize_enum_str!(DocumentationLength);

/// A style of Style to use when generating structs and enums.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Style {
Expand Down Expand Up @@ -935,6 +956,8 @@ pub struct Config {
pub documentation: bool,
/// How documentation comments should be styled.
pub documentation_style: DocumentationStyle,
/// How much of the documentation should be output for each item.
pub documentation_length: DocumentationLength,
/// Configuration options for pointers
#[serde(rename = "ptr")]
pub pointer: PtrConfig,
Expand Down Expand Up @@ -1013,6 +1036,7 @@ impl Default for Config {
defines: HashMap::new(),
documentation: true,
documentation_style: DocumentationStyle::Auto,
documentation_length: DocumentationLength::Full,
pointer: PtrConfig::default(),
only_target_dependencies: false,
cython: CythonConfig::default(),
Expand Down
9 changes: 7 additions & 2 deletions src/bindgen/ir/documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::io::Write;

use crate::bindgen::config::{Config, DocumentationStyle, Language};
use crate::bindgen::config::{Config, DocumentationLength, DocumentationStyle, Language};
use crate::bindgen::utilities::SynAttributeHelpers;
use crate::bindgen::writer::{Source, SourceWriter};

Expand Down Expand Up @@ -76,7 +76,12 @@ impl Source for Documentation {
_ => (),
}

for line in &self.doc_comment {
let end = match config.documentation_length {
DocumentationLength::Short => 1,
DocumentationLength::Full => self.doc_comment.len(),
};

for line in &self.doc_comment[..end] {
match style {
DocumentationStyle::C => out.write(""),
DocumentationStyle::Doxy => out.write(" *"),
Expand Down
1 change: 1 addition & 0 deletions template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ line_length = 100
tab_width = 2
documentation = true
documentation_style = "auto"
documentation_length = "full"
line_endings = "LF" # also "CR", "CRLF", "Native"


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

/**
* The root of all evil.
*/
void root(void);

/**
* A little above the root, and a lot more visible, with a run-on sentence
*/
void trunk(void);
22 changes: 22 additions & 0 deletions tests/expectations/doclength_short.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

/**
* The root of all evil.
*/
void root(void);

/**
* A little above the root, and a lot more visible, with a run-on sentence
*/
void trunk(void);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
15 changes: 15 additions & 0 deletions tests/expectations/doclength_short.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

extern "C" {

/// The root of all evil.
void root();

/// A little above the root, and a lot more visible, with a run-on sentence
void trunk();

} // extern "C"
20 changes: 20 additions & 0 deletions tests/expectations/doclength_short.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 *:
ctypedef bint bool
ctypedef struct va_list

cdef extern from *:

# The root of all evil.
#
# But at least it contains some more documentation as someone would expect
# from a simple test case like this. Though, this shouldn't appear in the
# output.
void root();

# A little above the root, and a lot more visible, with a run-on sentence
# to test going over the first line.
#
# Still not here, though.
void trunk();
16 changes: 16 additions & 0 deletions tests/rust/doclength_short.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// The root of all evil.
///
/// But at least it contains some more documentation as someone would expect
/// from a simple test case like this. Though, this shouldn't appear in the
/// output.
#[no_mangle]
pub extern "C" fn root() {
}

/// A little above the root, and a lot more visible, with a run-on sentence
/// to test going over the first line.
///
/// Still not here, though.
#[no_mangle]
pub extern "C" fn trunk() {
}
1 change: 1 addition & 0 deletions tests/rust/doclength_short.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
documentation_length = "short"
4 changes: 4 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ fn run_compile_test(

cbindgen_outputs.insert(cbindgen_output);

if env::var_os("CBINDGEN_TEST_NO_COMPILE").is_some() {
return;
}

compile(
&generated_file,
&tests_path,
Expand Down

0 comments on commit 0e3f9bd

Please sign in to comment.