{"record":{"id":"e23afdc21c7b9273","repo":"libnyanpasu/clash-nyanpasu","slug":"failed-to-transform-to-yaml-mapping","errorCode":null,"errorMessage":"failed to transform to yaml mapping \"{}\"","messagePattern":"failed to transform to yaml mapping \"(.+?)\"","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/tauri/src/utils/help.rs","lineNumber":52,"sourceCode":"        .with_context(|| format!(\"failed to read the file \\\"{}\\\"\", path.display()))?;\n\n    serde_yaml::from_str::<T>(&yaml_str).with_context(|| {\n        format!(\n            \"failed to read the file with yaml format \\\"{}\\\"\",\n            path.display()\n        )\n    })\n}\n\n/// read mapping from yaml fix #165\npub fn read_merge_mapping(path: &PathBuf) -> Result<Mapping> {\n    let mut val: Value = read_yaml(path)?;\n    val.apply_merge()\n        .with_context(|| format!(\"failed to apply merge \\\"{}\\\"\", path.display()))?;\n\n    Ok(val\n        .as_mapping()\n        .ok_or(anyhow!(\n            \"failed to transform to yaml mapping \\\"{}\\\"\",\n            path.display()\n        ))?\n        .to_owned())\n}\n\n/// save the data to the file\n/// can set `prefix` string to add some comments\npub fn save_yaml<T: Serialize, P: AsRef<Path>>(\n    path: P,\n    data: &T,\n    prefix: Option<&str>,\n) -> Result<()> {\n    let path = path.as_ref();\n    let data_str = serde_yaml::to_string(data)?;\n\n    let yaml_str = match prefix {\n        Some(prefix) => format!(\"{prefix}\\n\\n{data_str}\"),","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/utils/help.rs#L34-L70","documentation":"read_merge_mapping reads a YAML file and applies merge keys, then requires the document root to be a YAML mapping. If the file parses but its top level is a scalar, a sequence, or empty/null, it throws this error including the file path.","triggerScenarios":"Calling read_merge_mapping on a merge/patch YAML whose root is a list, a bare scalar, an empty file (null), or only comments.","commonSituations":"Users create an empty merge.yaml from a template, paste a rules-only YAML list instead of a mapping, or save a file containing only comment lines.","solutions":["Ensure the file's top level is a key: value mapping, e.g. `rules: [...]`.","Remove comment-only content and save the file with at least one top-level key.","Validate the YAML root type in a YAML parser before loading.","If the file should be empty, use `{}` as content instead of a zero-byte file."],"exampleFix":"# before (invalid: list root)\n- MATCH,DIRECT\n# after (valid: mapping root)\nrules:\n  - MATCH,DIRECT","handlingStrategy":"validation","validationCode":"import yaml\ncfg = yaml.safe_load(open(merge_path))\nassert isinstance(cfg, dict), f'{merge_path} root must be a mapping'","typeGuard":null,"tryCatchPattern":"try {\n  await loadMergeProfile(path);\n} catch (e) {\n  if (String(e).includes('failed to transform to yaml mapping')) {\n    openEditorAtPath(path); // let the user fix the root type\n  } else throw e;\n}","preventionTips":["Keep merge files as mappings, never bare lists or scalars","Seed new merge files with `{}` instead of empty content","Validate YAML before saving in an editor plugin"],"tags":["yaml","config","merge"],"backgroundTag":"config-type-mismatch","analyzedSha":"f7dbce2997c633e484f54788035e770b3ee99773","analyzedAt":"2026-09-08T01:24:59.197Z","contentChangedAt":"2026-09-08T01:24:59.197Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}