#file #create-file #string #delete-file #write-file #directory #access

file_access

A file_access wrapper-lib to make performing certain file manipulations more convenient

3 releases

0.1.9 Jun 3, 2023
0.1.8 Jun 3, 2023
0.1.7 Jun 3, 2023

#10 in #create-file

Download history 2/week @ 2024-02-14 24/week @ 2024-02-21 4/week @ 2024-02-28 10/week @ 2024-03-13 18/week @ 2024-03-27 34/week @ 2024-04-03

52 downloads per month

Apache-2.0

40KB
459 lines

Rust File Access Wrapper Lib

A file_access wrapper-lib containing the AsFile trait to make performing certain file manipulations more convenient.

let text = "strpath".as_file().read_string()?;
println!("{text}");

"file.1".as_file().copy_to(&"file.2")?;
"file.2".as_file().rename_to(&"file.3")?;

Exposed Actions

  • read_string: Returns String.
  • read_lines: Returns Vec<String>.
  • write_string: Takes a borrowed AsRef<str> such as String or &str. This function will create a file and its full directory path if they don't exist, and will entirely replace the contents.
  • write_lines: Takes a borrowed Vec<AsRef<str>> such as Vec<String> or Vec<&str>. This function will create a file and its full directory path if they don't exist, and will entirely replace the contents with the provided strings each on its own line.
  • append_string: Takes a borrowed AsRef<str> such as String or &str. This function will append the contents of a file, or write a new one and its full directory path if they don't exist yet.
  • append_lines: Takes a borrowed Vec<AsRef<str>> such as Vec<String> or Vec<&str>. This function will append the contents of a file, or write a new one and its full directory path if they don't exist yet.
  • delete: This function will delete a file, or a directory recursively.
  • copy/copy_to: This function will copy the contents of a file and write it to a destination. It will entirely replace the contents of the destination if it already exists.
  • rename/rename_to: This function will copy the contents of a file, write it to a destination and then delete the source. It will entirely replace the contents of the destination if it already exists.

Usages

There are 3 ways to use this library:

  • By calling methods directly: let result = file_access::METHOD_NAME(&file_path, &..)?
  • By using a FilePath handle: let file = FilePath::access(&file_path); let result = file.METHOD_NAME(&..)?
  • By using the AsFile trait: let file = "string_path".as_file(); let result = file.METHOD_NAME(&..)?

where file_path can be a borrowed String, &str, or file_access::FilePath.

Examples

  • Call read_string directly:
let text: String = file_access::read_string(&file_path)?;
println!("{text}");
  • Use a FilePath handle:
let file: FilePath = FilePath::access(&file_path);
let lines: Vec<&str> = vec!["hello", "world"];

file.write_lines(&lines)?;
file.append_lines(&lines)?;
file.copy_to(&another_path)?;
  • Use the AsFile trait:
// delete a file:
file_path.as_file().delete()?;

// rename a file:
"another_path".as_file().rename_to(&"a_new_file_path")?;

No runtime deps