Struct Simpath

Source
pub struct Simpath { /* private fields */ }
Expand description

Simpath is the struct returned when you create a new on using a named environment variable which you then use to interact with the Simpath

Implementations§

Source§

impl Simpath

Source

pub fn new(var_name: &str) -> Self

Create a new simpath, providing the name of the environment variable to initialize the search path with. If an environment variable of that name exists and it will be parsed as a ‘:’ separated list of paths to search. Only paths detected as directories will be used, not files.

If an environment variable of that name is not found, a new simpath will be created anyway and it can have directories added to it programatically and used in the normal fashion to search for files

extern crate simpath;
use simpath::Simpath;

fn main() {
    let search_path = Simpath::new("PATH");
    let ls_file = search_path.find("ls");
    match ls_file {
        Ok(found) => println!("'ls' was found at '{:?}'", found),
        Err(e)   => println!("{}", e)
    }
}
Source

pub fn new_with_separator(var_name: &str, separator: char) -> Self

Create a new simpath, providing the name of the environment variable to initialize the search path with and the separator character for this search path to be used from here on. If an environment variable of that name exists and it will be parsed as a list of paths to search. Only paths detected as directories will be used, not files.

If an environment variable of that name is not found, a new simpath will be created anyway and it can have directories added to it programatically and used in the normal fashion to search for files.

In all cases, the separator char for this search path will be set to separator from here on.

extern crate simpath;
use simpath::Simpath;
use std::env;

fn main() {
    env::set_var("TEST", "/,.,~");
    let search_path = Simpath::new("TEST");
    let two = search_path.find(".");
    match two {
        Ok(found) => println!("'.' was found at '{:?}'", found),
        Err(e)   => println!("{}", e)
    }
}
Source

pub fn separator(&self) -> char

Get the currently set separator character that is used when parsing entries from an environment variable

Source

pub fn name(&self) -> &str

Get the name associated with the simpath. Note that this could be an empty String

extern crate simpath;
use simpath::Simpath;

fn main() {
    let search_path = Simpath::new("PATH");
    println!("Directories in Search Path: {:?}", search_path.name());
}
Source

pub fn directories(&self) -> &HashSet<PathBuf>

Get the list of directories that are included in the Search Path

extern crate simpath;
use simpath::Simpath;

fn main() {
    let search_path = Simpath::new("PATH");
    println!("Directories in Search Path: {:?}", search_path.directories());
}
Source

pub fn find(&self, file_name: &str) -> Result<FoundType, Error>

Try to find a file or resource by name (not full path) on a search path. Searching for a file could cause errors, so Result<FoundType, io::Error> is returned If it is found Ok(FoundType) is returned indicating where the resource/file can be found. If it is not found then Err is returned.

extern crate simpath;
use simpath::Simpath;

fn main() {
    let search_path = Simpath::new("PATH");
    match search_path.find("my-file") {
        Ok(_found_dir) => println!("Didn't expect that!!"),
        Err(e)         => println!("{}", e.to_string())
    }
}
Source

pub fn find_type( &self, file_name: &str, file_type: FileType, ) -> Result<FoundType, Error>

find an entry of a specific FileType in a Path

extern crate simpath;
use simpath::Simpath;

fn main() {
    use simpath::FileType;
    let search_path = Simpath::new("PATH");
    match search_path.find_type("my-file", FileType::Directory) {
        Ok(_found_dir) => println!("Didn't expect that!!"),
        Err(e)         => println!("{}", e.to_string())
    }
}
Source

pub fn add(&mut self, entry: &str)

Add an to the search path.

if “urls” feature is enabled: If it parses as as web Url it will be added to the list of base Urls to search, otherwise it will be added to the list of directories to search. if “urls” feature is not enabled: It is assumed to be a directory and added using add_directory()

extern crate simpath;
use simpath::Simpath;

fn main() {
    let mut search_path = Simpath::new("PATH");
    search_path.add(".");

#[cfg(feature = "urls")]
    search_path.add("http://ibm.com");

    println!("{}", search_path);
}
Source

pub fn add_directory(&mut self, dir: &str)

Add a directory to the list of directories to search for files.

extern crate simpath;
use simpath::Simpath;

fn main() {
    let mut search_path = Simpath::new("PATH");
    search_path.add_directory(".");
    println!("Directories in Search Path: {:?}", search_path.directories());
}
Source

pub fn contains(&self, entry: &str) -> bool

Check if a search path contains an entry

extern crate simpath;
use simpath::Simpath;

fn main() {
    let mut search_path = Simpath::new("FakeEnvVar");
    if search_path.contains(".") {
        println!("Well that's a surprise!");
    }
}
Source

pub fn add_from_env_var(&mut self, var_name: &str)

Add entries to the search path, by reading them from an environment variable.

The environment variable should have a set of entries separated by the separator character. By default the separator char is ":" (on non-windows platforms) and ";" (on windows) but it can be modified after creation of search path.

The environment variable is parsed using the separator char set at the time this function is called.

To be added each entry must exist and be readable.

extern crate simpath;
use simpath::Simpath;

fn main() {
    let mut search_path = Simpath::new("MyPathName");
    search_path.add_from_env_var("PATH");
    if search_path.contains(".") {
        println!("'.' was in your 'PATH' and has been added to the search path called '{}'",
                 search_path.name());
    }
}
Source

pub fn add_from_env_var_with_separator( &mut self, var_name: &str, separator: char, )

Add entries to the search path, by reading them from an environment variable.

The environment variable should have a set of entries separated by the specified separator character.

To be added each entry must exist and be readable.

NOTE: The separator char is only used while parsing the specified environment variable and does not modify the separator character in use in the Simpath after this function completes.

extern crate simpath;
use simpath::Simpath;
use std::env;

fn main() {
    let mut search_path = Simpath::new("MyPathName");
    env::set_var("TEST", "/,.,~");
    search_path.add_from_env_var_with_separator("TEST", ',');
    if search_path.contains(".") {
        println!("'.' was in your 'TEST' environment variable and has been added to the search path called '{}'",
                 search_path.name());
    }
}
Source

pub fn is_empty(&self) -> bool

Check if the path is empty, i.e. has no directories added to it, and if the “urls” feature is enabled, that is has no urls added to it either.

extern crate simpath;
use simpath::Simpath;
use std::env;

fn main() {
    let mut search_path = Simpath::new("Foo");
    assert!(search_path.is_empty(), "The 'Foo' SearchPath should be empty");
}

Trait Implementations§

Source§

impl Clone for Simpath

Source§

fn clone(&self) -> Simpath

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Simpath

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Simpath

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.