Skip to content

Commit 067d286

Browse files
committed
Generate docs strs for enums
1 parent 2919bdb commit 067d286

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

codegen/enums.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub struct CustomEnumType {
66
pub raw_type: String,
77
pub value: Option<String>,
88
pub snake_case_name: String,
9+
pub doc_str: Option<Vec<String>>,
910
}
1011

1112
fn camel_to_snake_case(variable_name: &str) -> String {
@@ -79,14 +80,17 @@ impl EnumBindGenerator {
7980
})
8081
}
8182

82-
pub fn raw_types_to_custom(raw_types: Vec<(&str, &str)>) -> Vec<CustomEnumType> {
83+
pub fn raw_types_to_custom(
84+
raw_types: Vec<(&str, &str, Option<Vec<String>>)>,
85+
) -> Vec<CustomEnumType> {
8386
raw_types
8487
.into_iter()
85-
.map(|(name, raw_type)| CustomEnumType {
88+
.map(|(name, raw_type, doc_str)| CustomEnumType {
8689
name: name.to_string(),
8790
raw_type: raw_type.to_string(),
8891
value: None,
8992
snake_case_name: String::new(),
93+
doc_str,
9094
})
9195
.collect()
9296
}
@@ -103,6 +107,7 @@ impl EnumBindGenerator {
103107
let mut file_line_start = struct_pos + impl_pos + impl_definition.len();
104108
let mut lines = contents[file_line_start..].split('\n');
105109
let mut types = Vec::new();
110+
let mut docs = Vec::new();
106111

107112
loop {
108113
let line = lines.next()?;
@@ -112,8 +117,15 @@ impl EnumBindGenerator {
112117
break;
113118
}
114119

120+
if line_trim.starts_with("///") {
121+
docs.push(line_trim.trim_start_matches("///").trim().to_string());
122+
file_line_start += line.len();
123+
continue;
124+
}
125+
115126
if line_trim.starts_with("//") {
116127
file_line_start += line.len();
128+
docs.clear();
117129
continue;
118130
}
119131

@@ -129,7 +141,16 @@ impl EnumBindGenerator {
129141
.trim_start_matches("Self(")
130142
.trim_end_matches(')');
131143

132-
types.push((variable_name, variable_value));
144+
let docs = if docs.is_empty() {
145+
None
146+
} else {
147+
let docs_copy = docs.clone();
148+
docs.clear();
149+
150+
Some(docs_copy)
151+
};
152+
153+
types.push((variable_name, variable_value, docs));
133154

134155
file_line_start += line.len();
135156
}

codegen/pyi.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ pub fn generator(type_data: &[PythonBindType]) -> io::Result<()> {
7676

7777
let variable_type = variable_info.raw_type.as_str();
7878
write_fmt!(file, " {variable_name} = {type_name}({variable_type})");
79+
80+
if let Some(docs) = variable_info.doc_str.as_ref() {
81+
write_str!(file, " \"\"\"");
82+
83+
for line in docs {
84+
write_fmt!(file, " {line}");
85+
}
86+
87+
write_str!(file, " \"\"\"");
88+
}
7989
}
8090

8191
write_str!(file, "");

0 commit comments

Comments
 (0)