Skip to main content

module_key

Function module_key 

Source
pub fn module_key(
    path: &str,
    module_roots: &[String],
    module_depth: usize,
) -> String
Expand description

Compute a module key from an input path.

Rules:

  • Root-level files become "(root)".
  • If the first directory segment is in module_roots, include up to module_depth directory segments.
  • Otherwise, the module key is the first directory segment.

ยงExamples

use tokmd_module_key::module_key;

// Root-level files map to "(root)"
assert_eq!(module_key("Cargo.toml", &[], 2), "(root)");

// Files under a module root include deeper segments
let roots = vec!["crates".into()];
assert_eq!(module_key("crates/foo/src/lib.rs", &roots, 2), "crates/foo");

// Non-root directories use only the first segment
assert_eq!(module_key("src/lib.rs", &roots, 2), "src");

Windows-style paths and empty roots:

use tokmd_module_key::module_key;

let roots = vec!["crates".into()];

// Backslash paths are normalized before key computation
assert_eq!(module_key("crates\\foo\\src\\lib.rs", &roots, 2), "crates/foo");

// With no module roots every path uses the first directory segment
assert_eq!(module_key("crates/foo/src/lib.rs", &[], 2), "crates");