|
18 | 18 | * @param {string} line - A string containing the full struct declaration, including its body. |
19 | 19 | * @returns {string|null} The formatted doc comment block, or `null` if the input is not a valid documentable struct. |
20 | 20 | */ |
21 | | -function generateStructDoc(line) { |
| 21 | +function generateStructDoc(line, includeExamples, examplesOnlyForPublicOrExtern) { |
| 22 | + // Check if struct is public or externed. |
| 23 | + const isPublicOrExtern = /\b(pub(\s*\([^)]*\)|\s+self|\s+super)?|extern(\s*"[^"]*")?)\b/.test(line); |
| 24 | + |
22 | 25 | // Reject unit structs (single struct no fields) |
23 | 26 | if (/struct\s+\w+\s*(;|\{\})/.test(line)) return null; // Struct ends with ';' or '{}' |
24 | 27 |
|
@@ -60,25 +63,27 @@ function generateStructDoc(line) { |
60 | 63 | docLines.push(``, `# Fields`, ``, ...fields); |
61 | 64 | } |
62 | 65 |
|
63 | | - // Examples section |
64 | | - docLines.push(``, `# Examples`, ``, '```', `use crate::\${${currentTabStop++}:...};`, ``); |
| 66 | + if (includeExamples && (!examplesOnlyForPublicOrExtern || isPublicOrExtern)) { |
| 67 | + // Examples section |
| 68 | + docLines.push(``, `# Examples`, ``, '```', `use crate::\${${currentTabStop++}:...};`, ``); |
65 | 69 |
|
66 | | - if (body.startsWith('{')) { |
67 | | - // Field-style struct |
68 | | - docLines.push(`let s = ${name} {`); |
69 | | - for (const field of fields) { |
70 | | - const fieldName = field.match(/`([^`]+)`/)[1]; // extract field name |
71 | | - docLines.push(` ${fieldName}: value,`); |
| 70 | + if (body.startsWith('{')) { |
| 71 | + // Field-style struct |
| 72 | + docLines.push(`let s = ${name} {`); |
| 73 | + for (const field of fields) { |
| 74 | + const fieldName = field.match(/`([^`]+)`/)[1]; // extract field name |
| 75 | + docLines.push(` ${fieldName}: value,`); |
| 76 | + } |
| 77 | + docLines.push(`};`); |
| 78 | + } else if (body.startsWith('(')) { |
| 79 | + // Tuple-style struct |
| 80 | + const types = body.slice(1, -1).split(',').map(t => t.trim()).filter(Boolean); |
| 81 | + const tupleArgs = types.map(() => `value`).join(', '); |
| 82 | + docLines.push(`let s = ${name}(${tupleArgs});`); |
72 | 83 | } |
73 | | - docLines.push(`};`); |
74 | | - } else if (body.startsWith('(')) { |
75 | | - // Tuple-style struct |
76 | | - const types = body.slice(1, -1).split(',').map(t => t.trim()).filter(Boolean); |
77 | | - const tupleArgs = types.map(() => `value`).join(', '); |
78 | | - docLines.push(`let s = ${name}(${tupleArgs});`); |
79 | | - } |
80 | 84 |
|
81 | | - docLines.push('```'); // End the example section markdown code block |
| 85 | + docLines.push('```'); // End the example section markdown code block |
| 86 | + } |
82 | 87 |
|
83 | 88 | // Format as Rust doc comment block |
84 | 89 | return [docLines[0], ...docLines.slice(1).map(line => `/// ${line}`)].join('\n'); |
|
0 commit comments