Skip to content

Commit ba70f59

Browse files
committed
refactor helper methods
Signed-off-by: Ben Brandt <benjamin.j.brandt@gmail.com>
1 parent 31c48e9 commit ba70f59

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

rust/markdown_generator.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ impl MarkdownGenerator {
127127
writeln!(&mut self.output).unwrap();
128128

129129
// Add main description if available
130-
if let Some(desc) = get_def_description(definition) {
130+
if let Some(desc) = Self::get_def_description(definition) {
131131
// Escape # at the beginning of lines to prevent them from being treated as headers
132-
let escaped_desc = self.escape_description(&desc);
132+
let escaped_desc = Self::escape_description(&desc);
133133
writeln!(&mut self.output, "{}", escaped_desc).unwrap();
134134
writeln!(&mut self.output).unwrap();
135135
}
@@ -198,9 +198,8 @@ impl MarkdownGenerator {
198198
writeln!(&mut self.output, "\">").unwrap();
199199

200200
// Get description
201-
if let Some(desc) = get_def_description(variant) {
202-
let escaped_desc = self.escape_mdx(&desc);
203-
write!(&mut self.output, "{}", escaped_desc).unwrap();
201+
if let Some(desc) = Self::get_def_description(variant) {
202+
write!(&mut self.output, "{}", desc).unwrap();
204203
} else {
205204
write!(&mut self.output, "{{\"\"}}").unwrap();
206205
}
@@ -268,7 +267,7 @@ impl MarkdownGenerator {
268267

269268
for (prop_name, prop_schema) in sorted_props {
270269
let is_required = required.contains(&prop_name.as_str());
271-
let type_str = self.get_type_string(prop_schema);
270+
let type_str = Self::get_type_string(prop_schema);
272271

273272
// Simple field without nesting
274273
writeln!(
@@ -282,9 +281,8 @@ impl MarkdownGenerator {
282281
.unwrap();
283282

284283
// Add description if available
285-
if let Some(desc) = get_def_description(prop_schema) {
286-
let escaped_desc = self.escape_mdx(&desc);
287-
writeln!(&mut self.output, "{} {}", indent_str, escaped_desc).unwrap();
284+
if let Some(desc) = Self::get_def_description(prop_schema) {
285+
writeln!(&mut self.output, "{} {}", indent_str, desc).unwrap();
288286
}
289287

290288
// Add constraints if any
@@ -445,7 +443,7 @@ impl MarkdownGenerator {
445443
}
446444
}
447445

448-
fn get_type_string(&self, schema: &Value) -> String {
446+
fn get_type_string(schema: &Value) -> String {
449447
// Check for $ref
450448
if let Some(ref_val) = schema.get("$ref").and_then(|v| v.as_str()) {
451449
let type_name = ref_val.strip_prefix("#/$defs/").unwrap_or(ref_val);
@@ -462,7 +460,7 @@ impl MarkdownGenerator {
462460
return match type_str {
463461
"array" => {
464462
if let Some(items) = schema.get("items") {
465-
let item_type = self.get_type_string(items);
463+
let item_type = Self::get_type_string(items);
466464
format!("<><span>{}</span><span>[]</span></>", item_type)
467465
} else {
468466
"\"array\"".to_string()
@@ -506,7 +504,7 @@ impl MarkdownGenerator {
506504
for variant in arr {
507505
if variant.get("type").and_then(|v| v.as_str()) == Some("null") {
508506
has_null = true;
509-
} else if let Some(t) = self.get_inline_variant_type(variant) {
507+
} else if let Some(t) = Self::get_inline_variant_type(variant) {
510508
other_type = Some(t);
511509
}
512510
}
@@ -528,7 +526,7 @@ impl MarkdownGenerator {
528526
"\"object\"".to_string()
529527
}
530528

531-
fn get_inline_variant_type(&self, variant: &Value) -> Option<String> {
529+
fn get_inline_variant_type(variant: &Value) -> Option<String> {
532530
// Check for simple type
533531
if let Some(type_str) = variant.get("type").and_then(|v| v.as_str()) {
534532
return Some(format!("\"{type_str}\""));
@@ -545,15 +543,15 @@ impl MarkdownGenerator {
545543
None
546544
}
547545

548-
fn escape_mdx(&self, text: &str) -> String {
546+
fn escape_mdx(text: &str) -> String {
549547
text.replace('|', "\\|")
550548
.replace('<', "&lt;")
551549
.replace('>', "&gt;")
552550
.replace('{', "\\{")
553551
.replace('}', "\\}")
554552
}
555553

556-
fn escape_description(&self, text: &str) -> String {
554+
fn escape_description(text: &str) -> String {
557555
// Escape # at the beginning of lines to prevent them from being treated as headers
558556
let lines: Vec<String> = text
559557
.lines()
@@ -569,15 +567,16 @@ impl MarkdownGenerator {
569567
.collect();
570568
lines.join("\n")
571569
}
572-
}
573570

574-
fn get_def_description(def: &Value) -> Option<String> {
575-
let desc = def
576-
.get("description")?
577-
.as_str()?
578-
.replace("[`", "`")
579-
.replace("`]", "`");
580-
Some(desc)
571+
fn get_def_description(def: &Value) -> Option<String> {
572+
let desc = def
573+
.get("description")?
574+
.as_str()?
575+
.replace("[`", "`")
576+
.replace("`]", "`");
577+
let desc = Self::escape_mdx(&desc);
578+
Some(desc)
579+
}
581580
}
582581

583582
struct SideDocs {

0 commit comments

Comments
 (0)