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
impl Simpath
Sourcepub fn new(var_name: &str) -> Self
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)
}
}
Sourcepub fn new_with_separator(var_name: &str, separator: char) -> Self
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)
}
}
Sourcepub fn separator(&self) -> char
pub fn separator(&self) -> char
Get the currently set separator character that is used when parsing entries from an environment variable
Sourcepub fn name(&self) -> &str
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());
}
Sourcepub fn directories(&self) -> &HashSet<PathBuf>
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());
}
Sourcepub fn find(&self, file_name: &str) -> Result<FoundType, Error>
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())
}
}
Sourcepub fn find_type(
&self,
file_name: &str,
file_type: FileType,
) -> Result<FoundType, Error>
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())
}
}
Sourcepub fn add(&mut self, entry: &str)
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);
}
Sourcepub fn add_directory(&mut self, dir: &str)
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());
}
Sourcepub fn contains(&self, entry: &str) -> bool
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!");
}
}
Sourcepub fn add_from_env_var(&mut self, var_name: &str)
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());
}
}
Sourcepub fn add_from_env_var_with_separator(
&mut self,
var_name: &str,
separator: char,
)
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());
}
}
Sourcepub fn is_empty(&self) -> bool
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");
}