Skip to main content

prefer_in_folder

Function prefer_in_folder 

Source
pub fn prefer_in_folder<'a>(field: &'a [String], from: &str) -> Option<&'a str>
Expand description

Pick the winner from a shortest-first candidate field, as written in from.

The one place the ambiguity rule lives. LinkIndex keeps a single winner per key and reaches for this only when a name is contested. A tool that keeps whole candidate fields of its own, such as a rename planner that must answer “is this bare name still unambiguous after the move”, can call it too. Two shapes of storage, and exactly one rule deciding which file a contested name means — because a reader seeing one file while a rename rewrites another is the failure that costs.

field must be ordered shortest-first, which is what makes the first in-folder hit also the shortest one and lets both rules fall out of a single pass.

§Examples

use markdown_link::prefer_in_folder;

let field = vec!["b/a.md".to_string(), "notes/a.md".to_string()];
// Written in `notes/`: the candidate in the same folder wins.
assert_eq!(prefer_in_folder(&field, "notes/x.md"), Some("notes/a.md"));
// Written at the root: the shortest path wins.
assert_eq!(prefer_in_folder(&field, "x.md"), Some("b/a.md"));
assert_eq!(prefer_in_folder(&[], "x.md"), None);
// `notes/` does not match the sibling folder `notes2/`.
let field = vec!["notes2/a.md".to_string(), "z/notes/a.md".to_string()];
assert_eq!(prefer_in_folder(&field, "notes/x.md"), Some("notes2/a.md"));