MCPcopy Create free account
hub / github.com/ChrisFeldmeier/OpenCodeRust / fallback_sanitize_yaml

Function fallback_sanitize_yaml

crates/opencode-config/src/loader.rs:774–817  ·  view source on GitHub ↗

Fallback sanitization for invalid YAML frontmatter. Matches TS `ConfigMarkdown.fallbackSanitization`: if a top-level value contains a colon (which confuses simple YAML parsers), convert it to a block scalar so the value is preserved verbatim.

(yaml: &str)

Source from the content-addressed store, hash-verified

772/// contains a colon (which confuses simple YAML parsers), convert it to a
773/// block scalar so the value is preserved verbatim.
774fn fallback_sanitize_yaml(yaml: &str) -> String {
775 let mut result: Vec<String> = Vec::new();
776 for line in yaml.lines() {
777 let trimmed = line.trim();
778 // Pass through comments and empty lines
779 if trimmed.starts_with('#') || trimmed.is_empty() {
780 result.push(line.to_string());
781 continue;
782 }
783 // Pass through continuation/indented lines
784 if line.starts_with(char::is_whitespace) {
785 result.push(line.to_string());
786 continue;
787 }
788 // Match top-level key: value
789 let kv_re = regex::Regex::new(r"^([a-zA-Z_][a-zA-Z0-9_]*)\s*:\s*(.*)$").unwrap();
790 let Some(caps) = kv_re.captures(line) else {
791 result.push(line.to_string());
792 continue;
793 };
794 let key = &caps[1];
795 let value = caps[2].trim();
796 // Skip if value is empty, already quoted, or uses block scalar indicator
797 if value.is_empty()
798 || value == ">"
799 || value == "|"
800 || value == "|-"
801 || value == ">-"
802 || value.starts_with('"')
803 || value.starts_with('\'')
804 {
805 result.push(line.to_string());
806 continue;
807 }
808 // If value contains a colon, convert to block scalar
809 if value.contains(':') {
810 result.push(format!("{}: |-", key));
811 result.push(format!(" {}", value));
812 continue;
813 }
814 result.push(line.to_string());
815 }
816 result.join("\n")
817}
818
819/// Parse a YAML scalar value string into a JSON value.
820fn yaml_scalar_to_json(value: &str) -> serde_json::Value {

Calls 3

newFunction · 0.85
is_emptyMethod · 0.80
containsMethod · 0.80