feat: add flattenEntries/flattenGroups tree-flattening helpers

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WYqycDFsynHH9VnnK7LNSf
This commit is contained in:
2026-09-19 16:55:41 +02:00
co-authored by Claude Haiku 4.5
parent dc287cff3d
commit 5ca42a4e2a
2 changed files with 98 additions and 0 deletions
@@ -0,0 +1,46 @@
import Foundation
/// An entry paired with the full ancestor chain of groups leading to it, excluding the
/// vault's root group. Empty `breadcrumb` means the entry lives directly in the root group.
public struct FlatEntry: Identifiable, Equatable {
public let entry: Entry
public let parentGroupId: UUID
public let breadcrumb: [Group]
public var id: UUID { entry.id }
}
/// A group paired with the ancestor chain leading to its *parent*, excluding the vault's
/// root group. The root group itself appears with an empty breadcrumb.
public struct FlatGroup: Identifiable, Equatable {
public let group: Group
public let breadcrumb: [Group]
public var id: UUID { group.id }
}
/// Walks the vault tree once, pairing every entry with its full ancestor path.
public func flattenEntries(in root: Group) -> [FlatEntry] {
var result: [FlatEntry] = []
func walk(_ group: Group, ancestorBreadcrumb: [Group]) {
for entry in group.entries {
result.append(FlatEntry(entry: entry, parentGroupId: group.id, breadcrumb: ancestorBreadcrumb))
}
for sub in group.subgroups {
walk(sub, ancestorBreadcrumb: ancestorBreadcrumb + [sub])
}
}
walk(root, ancestorBreadcrumb: [])
return result
}
/// Walks the vault tree once, producing every group (including the root) with its ancestor path.
public func flattenGroups(in root: Group) -> [FlatGroup] {
var result: [FlatGroup] = [FlatGroup(group: root, breadcrumb: [])]
func walk(_ group: Group, ancestorBreadcrumb: [Group]) {
for sub in group.subgroups {
result.append(FlatGroup(group: sub, breadcrumb: ancestorBreadcrumb))
walk(sub, ancestorBreadcrumb: ancestorBreadcrumb + [sub])
}
}
walk(root, ancestorBreadcrumb: [])
return result
}