[][src]Struct radicle_surf::file_system::directory::Directory

pub struct Directory { /* fields omitted */ }

A Directory can be thought of as a non-empty set of entries of sub-directories and files. The reason for the non-empty property is that a VCS directory would have at least one artifact as a sub-directory which tracks the VCS work, e.g. git using the .git folder.

On top of that, some VCSes, such as git, will not track an empty directory, and so when creating a new directory to track it will have to contain at least one file.

Implementations

impl Directory[src]

pub fn root() -> Self[src]

Create a root directory.

This function is usually used for testing and demonstation purposes.

pub fn new(label: Label) -> Self[src]

Create a directory, similar to root, except with a given name.

This function is usually used for testing and demonstation purposes.

pub fn list_directory(&self) -> Vec<(Label, SystemType)>[src]

List the current Directory's files and sub-directories.

The listings are a pair of Label and SystemType, where the Label represents the name of the file or directory.

use nonempty::NonEmpty;
use radicle_surf::file_system::{Directory, File, SystemType};
use radicle_surf::file_system::unsound;

let mut directory = Directory::root();

// Root files set up
let root_files = NonEmpty::from((
    (unsound::label::new("foo.rs"), File::new(b"use crate::bar")),
    vec![(
        unsound::label::new("bar.rs"),
        File::new(b"fn hello_world()"),
    )],
));
directory.insert_files(&[], root_files);

// Haskell files set up
let haskell_files = NonEmpty::from((
    (
        unsound::label::new("foo.hs"),
        File::new(b"module Foo where"),
    ),
    vec![(
        unsound::label::new("bar.hs"),
        File::new(b"module Bar where"),
    )],
));

directory.insert_files(&[unsound::label::new("haskell")], haskell_files);

let mut directory_contents = directory.list_directory();
directory_contents.sort();

assert_eq!(
    directory_contents,
    vec![
        SystemType::file(unsound::label::new("bar.rs")),
        SystemType::file(unsound::label::new("foo.rs")),
        SystemType::directory(unsound::label::new("haskell")),
    ]
);

pub fn iter(&self) -> impl Iterator<Item = DirectoryContents> + '_[src]

Get the Label of the current directory.

Examples

use radicle_surf::file_system::{Directory, DirectoryContents, File, Label};
use radicle_surf::file_system::unsound;

let mut root = Directory::root();

let main = File::new(b"println!(\"Hello, world!\")");
root.insert_file(unsound::path::new("main.rs"), main.clone());

let lib = File::new(b"struct Hello(String)");
root.insert_file(unsound::path::new("lib.rs"), lib.clone());

let test_mod = File::new(b"assert_eq!(1 + 1, 2);");
root.insert_file(unsound::path::new("test/mod.rs"), test_mod.clone());

let mut root_iter = root.iter();

assert_eq!(root_iter.next(), Some(DirectoryContents::File {
    name: unsound::label::new("lib.rs"),
    file: lib
}));

assert_eq!(root_iter.next(), Some(DirectoryContents::File {
    name: unsound::label::new("main.rs"),
    file: main
}));

let mut test_dir = Directory::new(unsound::label::new("test"));
test_dir.insert_file(unsound::path::new("mod.rs"), test_mod);

assert_eq!(root_iter.next(), Some(DirectoryContents::Directory(test_dir)));

pub fn find_file(&self, path: Path) -> Option<File>[src]

Find a File in the directory given the Path to the File.

Failures

This operation fails if the path does not lead to a File. If the search is for a Directory then use find_directory.

Examples

Search for a file in the path: * foo/bar/baz.hs * foo * foo/bar/qux.rs

use radicle_surf::file_system::{Directory, File};
use radicle_surf::file_system::unsound;

let file = File::new(b"module Banana ...");

let mut directory = Directory::root();
directory.insert_file(unsound::path::new("foo/bar/baz.rs"), file.clone());

// The file is succesfully found
assert_eq!(directory.find_file(unsound::path::new("foo/bar/baz.rs")), Some(file));

// We shouldn't be able to find a directory
assert_eq!(directory.find_file(unsound::path::new("foo")), None);

// We shouldn't be able to find a file that doesn't exist
assert_eq!(directory.find_file(unsound::path::new("foo/bar/qux.rs")), None);

pub fn find_directory(&self, path: Path) -> Option<Self>[src]

Find a Directory in the directory given the Path to the Directory.

Failures

This operation fails if the path does not lead to the Directory.

Examples

Search for directories in the path: * foo * foo/bar * foo/baz

use radicle_surf::file_system::{Directory, File};
use radicle_surf::file_system::unsound;

let file = File::new(b"module Banana ...");

let mut directory = Directory::root();
directory.insert_file(unsound::path::new("foo/bar/baz.rs"), file.clone());

// Can find the first level
assert!(directory.find_directory(unsound::path::new("foo")).is_some());

// Can find the second level
assert!(directory.find_directory(unsound::path::new("foo/bar")).is_some());

// Cannot find 'baz' since it does not exist
assert!(directory.find_directory(unsound::path::new("foo/baz")).is_none());

// 'baz.rs' is a file and not a directory
assert!(directory.find_directory(unsound::path::new("foo/bar/baz.rs")).is_none());

pub fn current(&self) -> Label[src]

Get the Label of the current directory.

Examples

use radicle_surf::file_system::{Directory, File, Label};
use radicle_surf::file_system::unsound;

let mut root = Directory::root();
root.insert_file(unsound::path::new("main.rs"), File::new(b"println!(\"Hello, world!\")"));
root.insert_file(unsound::path::new("lib.rs"), File::new(b"struct Hello(String)"));
root.insert_file(unsound::path::new("test/mod.rs"), File::new(b"assert_eq!(1 + 1, 2);"));

assert_eq!(root.current(), Label::root());

let test = root.find_directory(
    unsound::path::new("test")
).expect("Missing test directory");
assert_eq!(test.current(), unsound::label::new("test"));

pub fn size(&self) -> usize[src]

Get the total size, in bytes, of a Directory. The size is the sum of all files that can be reached from this Directory.

Examples

use radicle_surf::file_system::{Directory, File};
use radicle_surf::file_system::unsound;

let mut root = Directory::root();
root.insert_file(unsound::path::new("main.rs"), File::new(b"println!(\"Hello, world!\")"));
root.insert_file(unsound::path::new("lib.rs"), File::new(b"struct Hello(String)"));
root.insert_file(unsound::path::new("test/mod.rs"), File::new(b"assert_eq!(1 + 1, 2);"));

assert_eq!(root.size(), 66);

pub fn insert_file(&mut self, path: Path, file: File)[src]

Insert a file into a directory, given the full path to file (file name inclusive) and the File itself.

This function is usually used for testing and demonstation purposes.

pub fn insert_files(
    &mut self,
    directory_path: &[Label],
    files: NonEmpty<(Label, File)>
)
[src]

Insert files into a shared directory path.

directory_path is used as the prefix to where the files should go. If empty the files will be placed in the current Directory.

files are pairs of file name and the File itself.

This function is usually used for testing and demonstation purposes.

Trait Implementations

impl Clone for Directory[src]

impl Debug for Directory[src]

impl Eq for Directory[src]

impl PartialEq<Directory> for Directory[src]

impl StructuralEq for Directory[src]

impl StructuralPartialEq for Directory[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Conv for T

impl<T> Conv for T

impl<T> FmtForward for T

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pipe for T where
    T: ?Sized

impl<T> Pipe for T

impl<T> PipeAsRef for T

impl<T> PipeBorrow for T

impl<T> PipeDeref for T

impl<T> PipeRef for T

impl<T> Tap for T

impl<T> Tap for T

impl<T, U> TapAsRef<U> for T where
    U: ?Sized

impl<T, U> TapBorrow<U> for T where
    U: ?Sized

impl<T> TapDeref for T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> TryConv for T

impl<T> TryConv for T

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.