feat: implement rustdoc comment generation support#190
feat: implement rustdoc comment generation support#190rockaxorb13 wants to merge 1 commit intoaccordproject:mainfrom
Conversation
Signed-off-by: Aadityavardhan Singh <singhrashmi018@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR adds RustDoc (///) documentation comment generation to the Rust CTO code generator by introducing a writeDescription helper and calling it from several visitor methods when emitting Rust structs and their members.
Changes:
- Added
writeDescription(thing, parameters, indent)to emit///lines fromgetDescription(). - Updated
visitClassDeclarationto emit RustDoc comments for generated structs. - Updated
visitField(including HashMap handling) andvisitRelationshipDeclarationto emit RustDoc comments for fields/relationships.
Comments suppressed due to low confidence (1)
lib/codegen/fromcto/rust/rustvisitor.js:311
- PR description mentions generating docs for structs and enums, but this change only calls writeDescription from visitClassDeclaration (structs). If enum docs are intended, visitEnumDeclaration (and possibly visitEnumValueDeclaration) should also call writeDescription; otherwise please update the PR description to match actual behavior.
this.writeDescription(classDeclaration, parameters, 0);
parameters.fileWriter.writeLine(
0,
'#[derive(Debug, Clone, Serialize, Deserialize)]'
);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Write '/// ' followed by the trimmed line | ||
| parameters.fileWriter.writeLine(indent, `/// ${line.trim()}`); |
There was a problem hiding this comment.
Trimming every doc line with line.trim() removes leading whitespace that may be meaningful for Markdown formatting (e.g., indented code blocks/lists). Also, for empty lines this generates "/// " (trailing whitespace) in the output; consider preserving leading whitespace (trim end only) and emitting "///" for blank lines to avoid trailing spaces in generated Rust.
| // Write '/// ' followed by the trimmed line | |
| parameters.fileWriter.writeLine(indent, `/// ${line.trim()}`); | |
| // 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}`); | |
| } |
| const description = thing.getDescription(); | ||
| // Split by newline to handle multi-line comments cleanly | ||
| const lines = description.split(/\r?\n/); | ||
| lines.forEach(line => { |
There was a problem hiding this comment.
This file consistently uses parentheses for single-arg arrow functions (e.g., .forEach((property) => ...)); for consistency, consider changing lines.forEach(line => { ... }) to lines.forEach((line) => { ... }).
| lines.forEach(line => { | |
| lines.forEach((line) => { |
| * @private | ||
| */ | ||
| visitClassDeclaration(classDeclaration, parameters) { | ||
| this.writeDescription(classDeclaration, parameters, 0); |
There was a problem hiding this comment.
New behavior (emitting RustDoc comments for classes/fields/relationships) isn’t covered by the existing RustVisitor unit tests. Please add tests that stub getDescription() for ClassDeclaration/Field/RelationshipDeclaration and assert the expected /// lines are written (including multi-line descriptions).
| if (thing.getDescription && thing.getDescription()) { | ||
| const description = thing.getDescription(); |
There was a problem hiding this comment.
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.
| if (thing.getDescription && thing.getDescription()) { | |
| const description = thing.getDescription(); | |
| const description = thing.getDescription && thing.getDescription(); | |
| if (typeof description === 'string' && description.length > 0) { |
Description
This PR brings the Rust generator closer to feature parity by adding support for documentation comments. It implements a
writeDescriptionhelper method that converts Concerto model descriptions into standard Rust documentation comments (///).Changes
writeDescriptionhelper method toRustVisitorclass.visitClassDeclarationto generate documentation for Structs and Enums.visitFieldto generate documentation for Fields (including HashMap support).visitRelationshipDeclarationto generate documentation for Relationships.Testing & Verification
I verified this logic locally using a debug script.
Note on Upstream Parser Behavior:
I discovered that the current version of
@accordproject/concerto-cto(v3.25.0) used in this repository appears to strip comments during AST parsing. As a result,thing.getDescription()returnsundefinedby default when parsing a string.To verify that my
RustVisitorlogic works correctly, I used a script that manually injects descriptions into theClassDeclarationobjects in memory after parsing. This confirmed that the generator correctly formats and outputs the RustDoc comments when the data is present in the model.Verification Script (
debug-rust.js)here is the debug script output which I got, after the modifications I made to rustvisitor.js:

Line 7 shows that the comment has been injected.
I am uploading the two files,
debug-rust.jshere:debug-rust.js