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
33 changes: 33 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);
Comment thread
mttrbrts marked this conversation as resolved.
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,31 @@ 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();
Comment on lines +1346 to +1347
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

writeDescription calls thing.getDescription() multiple times (in the condition and again when assigning), which can be inefficient and can repeat side effects if getDescription is computed. Consider calling it once, validating it's a string (or coercing via String()), and reusing the value.

Suggested change
if (thing.getDescription && thing.getDescription()) {
const description = thing.getDescription();
const description = thing.getDescription && thing.getDescription();
if (typeof description === 'string' && description.length > 0) {

Copilot uses AI. Check for mistakes.
// Split by newline to handle multi-line comments cleanly
const lines = description.split(/\r?\n/);
lines.forEach(line => {
Comment thread
mttrbrts marked this conversation as resolved.
// Preserve leading whitespace (important for Markdown),
// but trim trailing whitespace. For blank lines, emit
// "///" without a trailing space.
const trimmedEnd = line.replace(/\s+$/, '');
if (trimmedEnd === '') {
parameters.fileWriter.writeLine(indent, '///');
} else {
parameters.fileWriter.writeLine(indent, `/// ${trimmedEnd}`);
}
});
}
}
}

module.exports = RustVisitor;
Loading