8 个版本

0.4.0 2022 年 8 月 6 日
0.3.1 2022 年 5 月 24 日
0.3.0 2021 年 9 月 13 日
0.2.0 2021 年 3 月 8 日
0.1.2 2020 年 2 月 21 日

#121文件系统

Download history 21278/week @ 2024-03-14 21759/week @ 2024-03-21 18326/week @ 2024-03-28 21280/week @ 2024-04-04 20742/week @ 2024-04-11 19324/week @ 2024-04-18 18519/week @ 2024-04-25 24413/week @ 2024-05-02 21055/week @ 2024-05-09 21115/week @ 2024-05-16 20003/week @ 2024-05-23 21176/week @ 2024-05-30 20831/week @ 2024-06-06 21952/week @ 2024-06-13 20273/week @ 2024-06-20 16440/week @ 2024-06-27

83,407 每月下载量
用于 181 个 Crates (直接使用 7 个)

Apache-2.0

1MB
1K SLoC

包含 (WOFF 字体, 400KB) docs/api/NanumBarunGothic.ttf.woff2,(WOFF 字体, 135KB) docs/api/FiraSans-Medium.woff2,(WOFF 字体, 130KB) docs/api/FiraSans-Regular.woff2,(WOFF 字体, 82KB) docs/api/SourceSerif4-Bold.ttf.woff2,(WOFF 字体, 77KB) docs/api/SourceSerif4-Regular.ttf.woff2,(WOFF 字体, 45KB) docs/api/SourceCodePro-It.ttf.woff2 等 3 个更多

fsio

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

文件系统和路径实用函数。

概述

该 crate 包含用于路径、文件和目录处理的实用函数。

用法

fsio 有多个主要模块

  • fsio::path - 包含路径相关函数和特质。它们不直接修改文件系统。
  • fsio::file - 文件实用函数,如 read_file、write_file 等
  • fsio::directory - 特定于目录的实用函数。

示例

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);
}

安装

要使用此库,只需将其添加为依赖项

[dependencies]
fsio = "^0.4.0"

如果您需要访问临时文件路径,请启用以下 temp-path 功能

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

API 文档

请参阅完整文档: API 文档

贡献

请参阅 贡献指南

版本历史

请参阅 变更日志

许可证

由 Sagie Gur-Ari 开发并在 Apache 2 开源许可证下授权。

依赖项