-
Notifications
You must be signed in to change notification settings - Fork 3
Examples
Let's walk through creating a few files and folders, then navigate through them using Constellation.
This functionality is also exposed via the Retro Relay.
Let's start by talking about directories. We'll create a basic placeholder below.
// ...
let mut directory = Directory::new("SomeDirName");
// ...
Great, now we've got a new directory created at the root of our filesystem. Let's go ahead and add a sub-directory to it!
// ...
let sub = Directory::new("Sub Directory");
directory.add_item(sub)?;
// ...
Now we've got two directories, SomeDirName with a subdirectory of Sub Directory. We can validate that our filesystem looks as expected.
// ...
if directory.has_item("Sub Directory") { ... }
// ...
This will return a true if the directory we created exists. Lastly, you may want to get the directory later. We can easily do that using the example below.
// ...
let my_test_dir = directory.get_item("Sub Directory")?;
If you want to obtain a mutable reference of the directory we can do the following
let mut my_test_dir = directory.get_item_mut("Sub Directory")?;
Removing a directory will remove the content of the directory as well as the directory itself. You can delete a directory using the example below.
directory.remove_item("Sub Directory")?;
Uploading a file is simple, we'll use the directory we created earlier to upload a simple text file with the contents of "hello world".
let mut filesystem = ExampleFileSystem::new(); // Our example filesystem
filesystem.put("/Sub Directory/test.txt", "test.txt")?;
You can also download the file that we uploaded earlier.
filesystem.get("/Sub Directory/test.txt", "test.txt")?;
filesystem.remove("/Sub Directory/test.txt")?;
It may be helpful in some use cases to be able to navigate the filesystem using Warp itself to keep track of your path.
Let's first navigate into the directory we created earlier.
let mut filesystem = ExampleFileSystem::new(); // Our example filesystem
filesystem.select("Sub Directory")?;
Now let's get the current directory that we selected, as well as check the current path to ensure we're where we expected.
let current_directory = filesystem.current_directory();
if current_directory.name == "Sub Directory" { ... }
Now if we upload a file, it will automatically be placed inside our current directory path.
if current_directory.has_item("test.txt") { ... }
Great now let's head back to the root of our constellation.
filesystem.go_back()?;
As you can see it's pretty straightforward but can help alleviate needing to write front-end utilities to navigate through the index.