Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions lib/codegen/fromcto/rust/rustvisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class RustVisitor {
* @private
*/
visitClassDeclaration(classDeclaration, parameters) {
this.writeDescription(classDeclaration, parameters, 0);
parameters.fileWriter.writeLine(
0,
'#[derive(Debug, Clone, Serialize, Deserialize)]'
Expand Down Expand Up @@ -513,6 +514,8 @@ class RustVisitor {
if (field.isOptional?.()) {
hashMapType = this.wrapAsOption(hashMapType);
}
// Add description for HashMap fields
this.writeDescription(field, parameters, 1);

// Generate serde attributes for HashMap with DateTime serialization support
parameters.fileWriter.writeLine(1, '#[serde(');
Expand Down Expand Up @@ -607,6 +610,10 @@ class RustVisitor {
}

// Handle regular (non-HashMap) fields

// Add description for regular fields
this.writeDescription(field, parameters, 1);

parameters.fileWriter.writeLine(1, '#[serde(');
parameters.fileWriter.writeLine(2, `rename = "${field.name}",`);
if (field.isOptional?.()) {
Expand Down Expand Up @@ -752,6 +759,7 @@ class RustVisitor {
if (relationshipDeclaration.isOptional?.()) {
type = `Option<${type}>`;
}
this.writeDescription(relationshipDeclaration, parameters, 1);

// Start serde attribute block
parameters.fileWriter.writeLine(1, '#[serde(');
Expand Down Expand Up @@ -1327,6 +1335,24 @@ class RustVisitor {

parameters.fileWriter.closeFile();
}
/**
* Writes the description of a declaration or property as a Rust documentation comment.
* @param {Object} thing - the declaration or property
* @param {Object} parameters - the parameters
* @param {number} indent - the indentation level
* @private
*/
writeDescription(thing, parameters, indent) {
if (thing.getDescription && thing.getDescription()) {
const description = thing.getDescription();
// Split by newline to handle multi-line comments cleanly
const lines = description.split(/\r?\n/);
lines.forEach(line => {
// Write '/// ' followed by the trimmed line
parameters.fileWriter.writeLine(indent, `/// ${line.trim()}`);
});
}
}
}

module.exports = RustVisitor;