Struct file_ext::FileExt

source ·
pub struct FileExt;

Implementations§

Returns portion of a file of specified range. Range described as starting from byte M up to byte N.

Examples
use file_ext::FileExt;
#[test]
fn partial_read() {
    let path = "test/index.html";
    let file_raw_bytes = FileExt::read_file_partially(path, 4, 10).unwrap();
    let content = String::from_utf8(file_raw_bytes).unwrap();

    let expected_content = "CTYPE h";

    assert_eq!(expected_content, content);
}

Returns file content

Examples
 use file_ext::FileExt;
 #[test]
 fn file_content() {
     let path = "test/index.html";
     let file_raw_bytes = FileExt::read_file(path).unwrap();
     let content = String::from_utf8(file_raw_bytes).unwrap();
  
     let content_escaped_newline_carriage_return = str::replace(content.as_str(), "\r\n", "\n");

     let expected_content = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <title>Title</title>\n</head>\n<body>\n\n</body>\n</html>";

     assert_eq!(expected_content, content_escaped_newline_carriage_return);
 }

Returns file modification timestamp as nanoseconds in Unix epoch

Examples
 use std::{thread, time};
 use file_ext::FileExt;
 #[test]
 fn modification_timestamp() {

     let content = "data".as_bytes();
     let path = "modification_timestamp-test.content";

     FileExt::create_file(path).unwrap();
     FileExt::write_file(path, content).unwrap();

     let does_exist = FileExt::does_file_exist(path);
     assert!(does_exist);

     let modified_timestamp = FileExt::file_modified_utc(path).unwrap();

     let one_second = time::Duration::from_secs(1);
     thread::sleep(one_second);

     FileExt::write_file(path, "\nnewline and some data".as_bytes()).unwrap();

     let after_update_modified_timestamp = FileExt::file_modified_utc(path).unwrap();
     assert!(after_update_modified_timestamp > modified_timestamp);


     FileExt::delete_file(path).unwrap();
     let doesnt_exist = !FileExt::does_file_exist(path);
     assert!(doesnt_exist);
 }
Examples
use file_ext::FileExt;
#[test]
fn unix_path_delimiter() {
    let expected = SYMBOL.slash.to_string();
    let actual = FileExt::get_path_separator();
}

Will return absolute file path to the working directory

Examples
use file_ext::FileExt;
#[test]
fn absolute_path_to_working_directory() {
    let boxed_path = FileExt::get_static_filepath("");
    assert!(boxed_path.is_ok());
    let path = boxed_path.unwrap();
}

Will try to read from file. If file does not exist, will create and write to it given byte array

Examples
 use file_ext::FileExt;
 #[test]
 fn read_or_create_and_write() {
     let content = "data".as_bytes();
     let tmp_folder = FileExt::get_temp_folder_path().unwrap();

     let path = [tmp_folder, "test.txt".to_string()].join(FileExt::get_path_separator().as_str());

     let doesnt_exist = !FileExt::does_file_exist(path.as_str());
     assert!(doesnt_exist);

     FileExt::read_or_create_and_write(path.as_str(), content).unwrap();

     let does_exist = FileExt::does_file_exist(path.as_str());
     assert!(does_exist);

     let new_content = "updated data".as_bytes();
     FileExt::read_or_create_and_write(path.as_str(), new_content).unwrap();

     let file_content = FileExt::read_file(path.as_str()).unwrap();
     assert_eq!(content, file_content);

     FileExt::delete_file(path.as_str()).unwrap();
     let doesnt_exist = !FileExt::does_file_exist(path.as_str());
     assert!(doesnt_exist);
 }

Will create a file on the path

Examples
use file_ext::FileExt;
#[test]
fn file_creation_deletion() {
    let path = "test/file-creation.txt";

    let exists = FileExt::does_file_exist(path);
    assert!(!exists);

    FileExt::create_file(path).unwrap();

    let content = FileExt::read_file(path).unwrap();
    assert_eq!(content.len(), 0);

    FileExt::delete_file(path).unwrap();

    let exists = FileExt::does_file_exist(path);
    assert!(!exists);
}

Returns boolean indicating file existence on the path

Examples
use file_ext::FileExt;
#[test]
fn file_exists() {
    let path = "test/index_rewrite";
    let exists = FileExt::does_file_exist(path);
    assert!(exists);
}

Returns boolean indicating directory existence on the path

Examples
use file_ext::FileExt;
#[test]
fn directory_exists() {
    let path = "test";
    let exists = FileExt::does_directory_exist(path);
    assert!(exists);
}

Will create a new directory on specified path

Examples
 use file_ext::FileExt;
 #[test]
 fn new_directory_create_delete() {
     let path = "new_directory";

     let boxed_create = FileExt::create_directory(path);
     assert!(boxed_create.is_ok());

     assert!(FileExt::does_directory_exist(path));

     let boxed_delete = FileExt::delete_directory(path);
     assert!(boxed_delete.is_ok());
 }

Will delete directory and all of the content on specified path (won’t follow symlinks)

Examples
 use file_ext::FileExt;
 #[test]
 fn new_directory_create_delete() {
     let path = "new_directory";

     let boxed_create = FileExt::create_directory(path);
     assert!(boxed_create.is_ok());

     assert!(FileExt::does_directory_exist(path));

     let boxed_delete = FileExt::delete_directory(path);
     assert!(boxed_delete.is_ok());
 }

Returns boolean indicating symlink existence on the path

Examples
use file_ext::FileExt;
#[test]
fn symlink_exists() {
  let symlink_path = ["test", "index-link2"].join(FileExt::get_path_separator().as_str());

  if FileExt::does_symlink_exist(symlink_path.as_str()) {
    FileExt::delete_file(symlink_path.as_str()).unwrap();
  }

  let path = [SYMBOL.empty_string, "test", SYMBOL.empty_string].join(FileExt::get_path_separator().as_str());
  let path_prefix = FileExt::get_static_filepath(path.as_str()).unwrap();
  let points_to = [path_prefix.to_string(), "index.html".to_string()].join("");

  let boxed_symlink = FileExt::create_symlink(
    path_prefix.as_str(),
    "index-link2",
    points_to.as_str()
  );


  assert!(boxed_symlink.is_ok());

  let symlink_created = FileExt::does_symlink_exist(symlink_path.as_str());
  assert!(symlink_created);

  let actual_points_to = FileExt::symlink_points_to(symlink_path.as_str()).unwrap();
  assert_eq!(points_to, actual_points_to);

  FileExt::delete_file(symlink_path.as_str()).unwrap();
}

Returns absolute path, symlink points to. Takes 2 parameters: path to the directory, where symlink is located and where symlink points to

Examples
use file_ext::FileExt;
#[test]
fn resolve_symlink_path() {
   let base_dir = "/home/someuser/folder/subfolder/subsubfolder";
   let symlink_points_to = "../../subfolder2/subsubfolder2";

   let expected_path = "/home/someuser/folder/subfolder2/subsubfolder2";
   let actual_path = FileExt::resolve_symlink_path(base_dir, symlink_points_to).unwrap();

   assert_eq!(expected_path, actual_path);
}

Will write given byte array to a file on the path

Examples
 use file_ext::FileExt;
#[test]
 fn write() {
     let filename = "write-test.content";
     FileExt::create_file(filename).unwrap();

     let expected_content = "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <title>Title</title>\n</head>\n<body>\n\n</body>\n</html>";
     FileExt::write_file(filename, expected_content.as_bytes()).unwrap();

     let actual = FileExt::read_file(filename).unwrap();
     assert_eq!(actual, expected_content.as_bytes());

 }

Will delete file on a given path

Examples
use file_ext::FileExt;
#[test]
fn file_creation_deletion() {
    let path = "test/file-creation.txt";

    let exists = FileExt::does_file_exist(path);
    assert!(!exists);

    FileExt::create_file(path).unwrap();

    let content = FileExt::read_file(path).unwrap();
    assert_eq!(content.len(), 0);

    FileExt::delete_file(path).unwrap();

    let exists = FileExt::does_file_exist(path);
    assert!(!exists);
}

Will create symlink on path symlink_path with the specified name symlink_name. Symlink will point to specific file or directory symlink_points_to. Paths are absolute.

Examples
use file_ext::FileExt;
#[test]
fn symlink_creation() {
   let symlink_path = "test/index-link";

   if FileExt::does_symlink_exist(symlink_path) {
       FileExt::delete_file(symlink_path).unwrap();
   }

   let path_prefix = FileExt::get_static_filepath("/test/").unwrap();
   let points_to = [path_prefix.to_string(), "index.html".to_string()].join("");

   let boxed_symlink = FileExt::create_symlink(
       path_prefix.as_str(),
       "index-link",
       points_to.as_str());

    assert!(boxed_symlink.is_ok());

    let symlink_created = FileExt::does_symlink_exist(symlink_path);
    assert!(symlink_created);

    let actual_points_to = FileExt::symlink_points_to(symlink_path).unwrap();
    assert_eq!(points_to, actual_points_to);

    FileExt::delete_file(symlink_path).unwrap();
}

Checks if the file is symlink

Examples
use file_ext::FileExt;
#[test]
fn is_link() {
    let path: String = ["test", "index_rewrite"].join(&FileExt::get_path_separator());
    let is_symlink = FileExt::is_symlink(path.as_str()).unwrap();
    assert!(is_symlink);
}

Returns path to a file, symlink points to

Examples
use file_ext::FileExt;
#[test]
fn link_points_to() {
    let path: String = ["test", "index_rewrite"].join(&FileExt::get_path_separator());
    let points_to = FileExt::symlink_points_to(path.as_str()).unwrap();
    assert_eq!("index.html", points_to);
}

Builds a path from a given node list

Examples
use file_ext::FileExt;

#[test]
#[cfg(target_family = "unix")]
fn build_path() {
    let root = FileExt::root();
    let folder_up = FileExt::folder_up();

    let node_list =
        [
            root.as_str(),
            "home",
            "someuser",
            "folder",
            "subfolder",
            "subsubfolder",
        ];

    let another_node_list =
        [
            folder_up.as_str(),
            folder_up.as_str(),
            "subfolder2",
            "subsubfolder2",
        ];
    let path = PathExtImpl::build_path(&node_list);
    let another_path = PathExtImpl::build_path(&another_node_list);

    assert_eq!("/home/someuser/folder/subfolder/subsubfolder", path);
    assert_eq!("../../subfolder2/subsubfolder2", another_path);
}


#[test]
#[cfg(target_family = "windows")]
fn build_path() {
    let root = FileExt::root();
    let folder_up = FileExt::folder_up();

    let node_list =
        [
            root.as_str(),
            "Users",
            "someuser",
            "folder",
            "subfolder",
            "subsubfolder",
        ];

    let path = PathExtImpl::build_path(&node_list);

    assert_eq!("C:\\Users\\someuser\\folder\\subfolder\\subsubfolder", path);
}

Root node of the system. It is meant to be used in build_path function. On Linux and macOS build_path function will evaluate it to /, on Windows it will be C:

Folder up, or ... It is meant to be used in build_path function.

Returns name of the user running the process

Examples
 use file_ext::FileExt;
 #[test]
 #[cfg(target_family = "unix")]
 fn current_user() {
     let boxed_user = FileExt::get_current_user();
     assert!(boxed_user.is_ok());

     let user = boxed_user.unwrap();
 }

Returns path to the temporary folder

Examples
 use file_ext::FileExt;
 #[test]
 #[cfg(target_family = "unix")]
 fn temp_folder() {
     let temp_folder_path = FileExt::get_temp_folder_path().unwrap();
     assert_eq!(temp_folder_path, "/tmp")
 }

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.