blob: a0a42246de976cd978e2989eca54320b6eae2d55 [file]
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::io::Write;
use syn;
use bindgen::config::{Config, DocumentationStyle, Language};
use bindgen::utilities::SynAttributeHelpers;
use bindgen::writer::{Source, SourceWriter};
#[derive(Debug, Clone)]
pub struct Documentation {
pub doc_comment: Vec<String>,
}
impl Documentation {
pub fn load(attrs: &[syn::Attribute]) -> Self {
let doc = attrs
.get_comment_lines()
.into_iter()
.filter(|x| !x.is_empty() && !x.starts_with("cbindgen:"))
.collect();
Documentation { doc_comment: doc }
}
pub fn none() -> Self {
Documentation {
doc_comment: Vec::new(),
}
}
}
impl Source for Documentation {
fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
if self.doc_comment.is_empty() || !config.documentation {
return;
}
let style = match config.documentation_style {
DocumentationStyle::Auto if config.language == Language::C => DocumentationStyle::Doxy,
DocumentationStyle::Auto if config.language == Language::Cxx => DocumentationStyle::Cxx,
DocumentationStyle::Auto => DocumentationStyle::C, // Fallback if `Language` gets extended.
other => other,
};
// Following these documents for style conventions:
// https://en.wikibooks.org/wiki/C++_Programming/Code/Style_Conventions/Comments
// https://www.cs.cmu.edu/~410/doc/doxygen.html
match style {
DocumentationStyle::C => {
out.write("/*");
out.new_line();
}
DocumentationStyle::Doxy => {
out.write("/**");
out.new_line();
}
_ => (),
}
for line in &self.doc_comment {
match style {
DocumentationStyle::C => out.write(""),
DocumentationStyle::Doxy => out.write(" *"),
DocumentationStyle::Cxx => out.write("///"),
DocumentationStyle::Auto => unreachable!(), // Auto case should always be covered
}
if line.len() != 0 {
out.write(" ");
}
write!(out, "{}", line);
out.new_line();
}
match style {
DocumentationStyle::C => {
out.write(" */");
out.new_line();
}
DocumentationStyle::Doxy => {
out.write(" */");
out.new_line();
}
_ => (),
}
}
}