Skip to main content

winreg_core/
path.rs

1//! Key path reconstruction — walk the parent chain to build full paths.
2
3use crate::cell_reader::Cell;
4use crate::error::Result;
5use crate::key::Key;
6
7impl Key<'_> {
8    /// Reconstruct the full path from root to this key.
9    ///
10    /// Walks the parent chain upward until the root key (`KEY_HIVE_ENTRY`) is found.
11    pub fn path(&self) -> Result<String> {
12        let mut parts: Vec<String> = vec![self.name()];
13        let mut current_parent = self.node.parent;
14
15        // Walk up to root (max 512 levels to prevent infinite loops on corrupt hives).
16        for _ in 0..512 {
17            if current_parent.is_null() {
18                break;
19            }
20
21            let cell = self.hive.read_cell(current_parent)?;
22            match cell {
23                Cell::KeyNode(nk) => {
24                    if nk.is_root() {
25                        break; // Don't include root key name in path
26                    }
27                    parts.push(nk.key_name());
28                    current_parent = nk.parent;
29                }
30                _ => break,
31            }
32        }
33
34        parts.reverse();
35        Ok(parts.join("\\"))
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    // Path reconstruction tests require TestHiveBuilder with nested keys.
42    //
43    // #[test]
44    // fn path_of_root_key() { ... }
45    //
46    // #[test]
47    // fn path_of_nested_key() { ... }
48}