8 releases

0.4.0 Aug 6, 2022
0.3.1 May 24, 2022
0.3.0 Sep 13, 2021
0.2.0 Mar 8, 2021
0.1.2 Feb 21, 2020

#170 in Filesystem

Download history 20357/week @ 2024-07-24 20394/week @ 2024-07-31 19869/week @ 2024-08-07 19088/week @ 2024-08-14 20498/week @ 2024-08-21 18144/week @ 2024-08-28 20554/week @ 2024-09-04 18124/week @ 2024-09-11 17321/week @ 2024-09-18 20282/week @ 2024-09-25 21520/week @ 2024-10-02 18226/week @ 2024-10-09 21633/week @ 2024-10-16 24645/week @ 2024-10-23 27066/week @ 2024-10-30 24698/week @ 2024-11-06

101,626 downloads per month
Used in 189 crates (7 directly)

Apache-2.0

1MB
1K SLoC

Contains (WOFF font, 400KB) docs/api/NanumBarunGothic.ttf.woff2, (WOFF font, 135KB) docs/api/FiraSans-Medium.woff2, (WOFF font, 130KB) docs/api/FiraSans-Regular.woff2, (WOFF font, 82KB) docs/api/SourceSerif4-Bold.ttf.woff2, (WOFF font, 77KB) docs/api/SourceSerif4-Regular.ttf.woff2, (WOFF font, 45KB) docs/api/SourceCodePro-It.ttf.woff2 and 3 more.

fsio

crates.io CI codecov
license Libraries.io for GitHub Documentation downloads
Built with cargo-make

File System and Path utility functions.

Overview

This crate contains utility functions for path, file and directory handling.

Usage

There are multiple main modules for fsio:

  • fsio::path - Holds path related functions and traits. They do not directly modify the file system.
  • fsio::file - File utility functions such as read_file, write_file, ...
  • fsio::directory - Directory specific utility functions.

Examples

use fsio::{directory, file, path};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::str;

fn main() {
    // file operations
    let mut result = file::ensure_exists("./target/__test/example/file_test/dir1/dir2/file.txt");
    assert!(result.is_ok());

    // create/append and read text files
    let mut file_path = "./target/__test/example/file_test/append_text_file/file.txt";
    result = file::write_text_file(file_path, "some content");
    assert!(result.is_ok());
    result = file::append_text_file(file_path, "\nmore content");
    assert!(result.is_ok());
    let mut text = file::read_text_file(file_path).unwrap();
    assert_eq!(text, "some content\nmore content");

    // create/append and read binary files
    file_path = "./target/__test/example/file_test/append_and_read_file_test/file.txt";
    result = file::write_file(file_path, "some content".as_bytes());
    assert!(result.is_ok());
    result = file::append_file(file_path, "\nmore content".as_bytes());
    assert!(result.is_ok());
    let data = file::read_file(file_path).unwrap();
    assert_eq!(str::from_utf8(&data).unwrap(), "some content\nmore content");

    // custom writing
    file_path = "./target/__test/file_test/modify_file/file.txt";
    result = file::modify_file(
        file_path,
        &move |file: &mut File| file.write_all("some content".as_bytes()),
        false,
    );
    assert!(result.is_ok());
    text = file::read_text_file(file_path).unwrap();
    assert_eq!(text, "some content");

    // delete file
    result = file::delete(file_path);
    assert!(result.is_ok());

    // directory operations
    result = directory::create("./target/__test/example/directory_test/dir1/dir2");
    assert!(result.is_ok());

    result = directory::create_parent("./target/__test/example/directory_test/dir1/files/file.txt");
    assert!(result.is_ok());

    // delete directory
    result = directory::delete("./target/__test/example/directory_test");
    assert!(result.is_ok());

    // basename and parent directory examples
    let basename = path::get_basename("./src/path/mod.rs");
    assert_eq!(basename.unwrap(), "mod.rs");

    let dirname = path::get_parent_directory("./src/path/mod.rs");
    assert_eq!(dirname.unwrap(), "./src/path");

    // canonicalize examples
    let path_obj = Path::new("./src/path/mod.rs");

    let path1 = path::canonicalize_as_string(&path_obj);
    let path2 = path::canonicalize_or("./src/path/mod.rs", "/src/path/mod.rs");

    assert_eq!(path1.unwrap(), path2);

    // get last modified time
    let time = path::get_last_modified_time("./src/path/mod.rs").unwrap();
    assert!(time > 0);
}

Installation

In order to use this library, just add it as a dependency:

[dependencies]
fsio = "^0.4.0"

If you need access to temporary file paths, enable the temp-path feature as follows:

[dependencies]
fsio = { version = "*", features = ["temp-path"] }

API Documentation

See full docs at: API Docs

Contributing

See contributing guide

Release History

See Changelog

License

Developed by Sagie Gur-Ari and licensed under the Apache 2 open source license.

Dependencies