Creating a Graph from an existing Folder Structure?

Given a path, we can recursively list all folders from a starting directory

for entry in WalkDir::new("foo").into_iter().filter_map(|e| e.ok()) {
    println!("{}", entry.path().display());
}

Does a crate exists what provides a graph like structure

struct File (HashMap<String, File>);

If not how would be the best way to populate the hashmap. I cannot seem to take each Component without either doing infinite recursion or having my nested loop go so deep.

You might run into open file limitations.

(It's not a graph.)

1 Like

It's pretty easy to do, here's a previous Q/A.

This is is more what I was thinking of.

The example you gave goes "backwards" from a given path but I want to go "forwards".
This is workable but I am not sure on how good this is as a working example if you could suggest some improvements?

I don't get what that means.

This doesn't compile, and if I fix the missing imports, it always returns an empty vector.

Apologies, I have refactored the code slightly

For the test case you must pass a root folder, Example output of tree -f

C:.
β”œβ”€β”€β”€Bar
β”‚   └───Boo
└───Foo
    β”œβ”€β”€β”€BarFoo
    └───FooBar

Example output from the linked playground code

[src\main.rs:75] path = Ok(
    [
        Entry {
            name: "Bar",
            kind: Folder(
                [
                    Entry {
                        name: "Boo",
                        kind: Folder(
                            [],
                        ),
                    },
                ],
            ),
        },
        Entry {
            name: "Foo",
            kind: Folder(
                [
                    Entry {
                        name: "BarFoo",
                        kind: Folder(
                            [],
                        ),
                    },
                    Entry {
                        name: "FooBar",
                        kind: Folder(
                            [],
                        ),
                    },
                ],
            ),
        },
    ],
)

Doesn't that return the exact same kind of tree that the code in my earlier post does?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.