pub struct FSUtil { /* private fields */ }Expand description
§File system utility
The file system utility provides a set of functions to interact with the file system. This is designed with compatibility in mind. Not every platform that we target supports the standard Rust file system library. For example, WASM doesn’t support Rust file system functions in the stdlib.
Implementations§
Source§impl FSUtil
impl FSUtil
Sourcepub fn new(
read_file: fn(path: &str) -> Option<String>,
file_exists: fn(path: &str) -> bool,
file_is_directory: fn(path: &str) -> bool,
path_join: fn(base: &str, path: &str) -> String,
parent_directory: fn(path: &str) -> String,
path_is_absolute: fn(path: &str) -> bool,
) -> Self
pub fn new( read_file: fn(path: &str) -> Option<String>, file_exists: fn(path: &str) -> bool, file_is_directory: fn(path: &str) -> bool, path_join: fn(base: &str, path: &str) -> String, parent_directory: fn(path: &str) -> String, path_is_absolute: fn(path: &str) -> bool, ) -> Self
Create a new instance of file system utility.
§Arguments
read_file- A function to read file content.file_exists- A function to check whether a file exists.file_is_directory- A function to check if file is a directory.path_join- A function to joinbaseandpathinto a single path.parent_directory- A function to get the parent directory of the argument.
§Examples
use teo_language_parser::fsutil::FSUtil;
use std::fs;
use std::path::{Path, PathBuf};
fn read_file(path: &str) -> Option<String> {
match fs::read_to_string(Path::new(path)) {
Ok(s) => Some(s),
Err(_) => None,
}
}
fn file_exists(path: &str) -> bool {
Path::new(path).exists()
}
fn parent_directory(path: &str) -> String {
let mut path = PathBuf::from(path);
path.pop();
path.to_str().unwrap().to_string()
}
fn path_is_absolute(path: &str) -> bool {
path.starts_with("/")
}
fn path_join(base: &str, path: &str) -> String {
format!("{}/{}", base, path)
}
fn file_is_directory(path: &str) -> bool {
Path::new(path).is_dir()
}
let fs_util = FSUtil::new(
read_file,
file_exists,
file_is_directory,
path_join,
parent_directory,
path_is_absolute
);
assert!(fs_util.file_exists("Cargo.toml"));
assert!(!fs_util.file_is_directory("Cargo.toml"));
assert_eq!(fs_util.path_join("src", "main.rs"), "src/main.rs");
assert_eq!(fs_util.parent_directory("src/main.rs"), "src");
assert!(fs_util.path_is_absolute("/home/user"));Sourcepub fn read_file(&self, path: &str) -> Option<String>
pub fn read_file(&self, path: &str) -> Option<String>
Read the file content from path into a String.
None if file doesn’t exist or cannot be read.
Sourcepub fn file_exists(&self, path: &str) -> bool
pub fn file_exists(&self, path: &str) -> bool
Returns true if file exists at path.
Sourcepub fn file_is_directory(&self, path: &str) -> bool
pub fn file_is_directory(&self, path: &str) -> bool
Returns true if file at path is a directory.
Sourcepub fn path_join(&self, base: &str, path: &str) -> String
pub fn path_join(&self, base: &str, path: &str) -> String
Returns a joined path of base and path.
Sourcepub fn parent_directory(&self, path: &str) -> String
pub fn parent_directory(&self, path: &str) -> String
Returns the parent directory of path.
Sourcepub fn path_is_absolute(&self, path: &str) -> bool
pub fn path_is_absolute(&self, path: &str) -> bool
Returns true if path is an absolute path.
Sourcepub fn import_path(&self, source_path: &str, path: &str) -> String
pub fn import_path(&self, source_path: &str, path: &str) -> String
Get the parent directory of source_path and join it with path.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for FSUtil
impl RefUnwindSafe for FSUtil
impl Send for FSUtil
impl Sync for FSUtil
impl Unpin for FSUtil
impl UnwindSafe for FSUtil
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more