Skip to main content

escape_for_docstring

Function escape_for_docstring 

Source
pub fn escape_for_docstring(s: &str, context: EscapeContext) -> String
Expand description

Escape a string for use in docstrings or documentation comments.

Docstrings have language-specific delimiters and rules:

  • Python: Triple-quoted strings (""") with different escape patterns
  • JavaScript/TypeScript: JSDoc comments with /** and */
  • Ruby: YARD documentation with special comment markers
  • PHP: PHPDoc comments with special markers
  • Rust: rustdoc with /// or //!

§Arguments

  • s - The string to escape
  • context - The target language context

§Returns

A string escaped for safe embedding in docstrings of the target language

§Examples

use spikard_cli::codegen::common::escaping::{EscapeContext, escape_for_docstring};

// Python docstrings use triple quotes, which must be escaped
let result = escape_for_docstring("Description with \"\"\" in it", EscapeContext::Python);
assert!(!result.contains("\"\"\""));
assert!(result.contains("\\\""));