FSUtil

Struct FSUtil 

Source
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

Source

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 join base and path into 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"));
Source

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.

Source

pub fn file_exists(&self, path: &str) -> bool

Returns true if file exists at path.

Source

pub fn file_is_directory(&self, path: &str) -> bool

Returns true if file at path is a directory.

Source

pub fn path_join(&self, base: &str, path: &str) -> String

Returns a joined path of base and path.

Source

pub fn parent_directory(&self, path: &str) -> String

Returns the parent directory of path.

Source

pub fn path_is_absolute(&self, path: &str) -> bool

Returns true if path is an absolute path.

Source

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§

Source§

impl Default for FSUtil

Provide a default implementation for file system utility.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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> 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, 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.