Skip to main content

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.
12#[inline]
13pub fn format_yaml(input: &str) -> String {
14    let opts: &'static Options = vscode_tidy_options_ref();
15    tidy::yaml::format(input, &opts.yaml).unwrap_or_else(|_: tidy::Error| input.to_string())
16}
17
18/// Formats YAML content with an explicit file path so `.prettierrc` files are formatted as JSON when valid JSON or YAML otherwise.
19///
20/// # Arguments
21///
22/// - `&str` - Raw YAML or RC file content.
23/// - `&Path` - Source file path hint.
24///
25/// # Returns
26///
27/// - `String` - Formatted YAML or JSON text, or unchanged input if syntax errors occur.
28#[inline]
29pub(crate) fn format_yaml_with_path(input: &str, path: &Path) -> String {
30    let mut yaml_opts: tidy::yaml::Options = vscode_tidy_options_ref().yaml.clone();
31    yaml_opts.filepath = Some(path.to_string_lossy().into_owned());
32    tidy::yaml::format(input, &yaml_opts).unwrap_or_else(|_: tidy::Error| input.to_string())
33}