Skip to content

Commit 29a99ec

Browse files
committed
Add doc strings for struct definitions
1 parent 067d286 commit 29a99ec

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

codegen/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,37 @@ impl PythonBindType {
8181
#[cfg(windows)]
8282
let contents = contents.replace("\r\n", "\n");
8383

84+
let struct_def = format!("pub struct {struct_name}");
85+
let struct_def_pos = contents.find(&struct_def).unwrap();
86+
87+
let mut docs = Vec::new();
88+
89+
let binding = contents[..struct_def_pos].to_string();
90+
for line in binding.lines().rev() {
91+
if line.starts_with("///") {
92+
docs.push(line.trim().trim_start_matches("///").trim());
93+
} else {
94+
break;
95+
}
96+
}
97+
98+
let struct_doc_str = if docs.is_empty() {
99+
None
100+
} else {
101+
Some(
102+
docs.into_iter()
103+
.map(|s| s.to_string())
104+
.rev()
105+
.collect::<Vec<_>>(),
106+
)
107+
};
108+
84109
if let Some(types) = StructBindGenerator::get_types(&contents, &struct_t_name) {
85110
return Some(Self::Struct(StructBindGenerator::new(
86111
filename.to_string(),
87112
struct_name,
88113
struct_t_name,
114+
struct_doc_str,
89115
contents,
90116
types,
91117
)?));

codegen/pyi.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
105105
write_str!(file, " def __hash__(self) -> str: ...");
106106
}
107107
PythonBindType::Struct(gen) => {
108+
if let Some(docs) = gen.struct_doc_str.as_ref() {
109+
write_str!(file, " \"\"\"");
110+
111+
for line in docs {
112+
write_fmt!(file, " {line}");
113+
}
114+
115+
write_str!(file, " \"\"\"");
116+
}
117+
108118
let mut python_types = Vec::new();
109119

110120
'outer: for variable_info in &gen.types {

codegen/structs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct StructBindGenerator {
4848
pub filename: String,
4949
pub struct_name: String,
5050
struct_t_name: String,
51+
pub struct_doc_str: Option<Vec<String>>,
5152
pub types: Vec<CustomType>,
5253
file_contents: Vec<Cow<'static, str>>,
5354
has_complex_pack: bool,
@@ -74,6 +75,7 @@ impl StructBindGenerator {
7475
filename: String,
7576
struct_name: String,
7677
struct_t_name: String,
78+
struct_doc_str: Option<Vec<String>>,
7779
contents: String,
7880
types: Vec<CustomType>,
7981
) -> Option<Self> {
@@ -118,6 +120,7 @@ impl StructBindGenerator {
118120
filename,
119121
struct_name,
120122
struct_t_name,
123+
struct_doc_str,
121124
types,
122125
file_contents,
123126
has_complex_pack,

0 commit comments

Comments
 (0)