crate_cli/fmt/yaml/fn.rs
1use super::*;
2
3/// Formats YAML / YML content matching VS Code `esbenp.prettier-vscode` (Prettier 3.7.4: `tabWidth: 2`, `singleQuote: true`, `printWidth: 80`, `bracketSpacing: true`, `proseWrap: "preserve"`, LF).
4///
5/// # Arguments
6///
7/// - `&str` - Raw YAML or YML document text.
8///
9/// # Returns
10///
11/// - `String` - Formatted YAML text, or unchanged input if syntax errors occur.
12pub fn format_yaml(input: &str) -> String {
13 let opts: Options = vscode_tidy_options();
14 tidy::yaml::format(input, &opts.yaml).unwrap_or_else(|_: tidy::Error| input.to_string())
15}
16
17/// Formats YAML content with an explicit file path so `.prettierrc` files are formatted as JSON when valid JSON or YAML otherwise.
18///
19/// # Arguments
20///
21/// - `&str` - Raw YAML or RC file content.
22/// - `&Path` - Source file path hint.
23///
24/// # Returns
25///
26/// - `String` - Formatted YAML or JSON text, or unchanged input if syntax errors occur.
27pub(crate) fn format_yaml_with_path(input: &str, path: &Path) -> String {
28 let mut opts: Options = vscode_tidy_options();
29 opts.yaml.filepath = Some(path.to_string_lossy().into_owned());
30 tidy::yaml::format(input, &opts.yaml).unwrap_or_else(|_: tidy::Error| input.to_string())
31}