pub struct FileExt;Implementations§
source§impl FileExt
impl FileExt
sourcepub fn read_file_partially(
filepath: &str,
start: u64,
end: u64
) -> Result<Vec<u8>, String>
pub fn read_file_partially(
filepath: &str,
start: u64,
end: u64
) -> Result<Vec<u8>, String>
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);
}sourcepub fn read_file(filepath: &str) -> Result<Vec<u8>, String>
pub fn read_file(filepath: &str) -> Result<Vec<u8>, String>
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 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);
}sourcepub fn file_modified_utc(filepath: &str) -> Result<u128, String>
pub fn file_modified_utc(filepath: &str) -> Result<u128, String>
Returns file modification timestamp as nanoseconds in Unix epoch
sourcepub fn get_static_filepath(path: &str) -> Result<String, String>
pub fn get_static_filepath(path: &str) -> Result<String, String>
Will return absolute file path
sourcepub fn read_or_create_and_write(
path: &str,
content: &[u8]
) -> Result<Vec<u8>, String>
pub fn read_or_create_and_write(
path: &str,
content: &[u8]
) -> Result<Vec<u8>, String>
Will try to read from file. If file does not exist, will create and write to it given byte array
sourcepub fn does_file_exist(path: &str) -> bool
pub fn does_file_exist(path: &str) -> bool
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);
}sourcepub fn write_file(path: &str, file_content: &[u8]) -> Result<(), String>
pub fn write_file(path: &str, file_content: &[u8]) -> Result<(), String>
Will write given byte array to a file on the path
sourcepub fn is_symlink(path: &str) -> Result<bool, String>
pub fn is_symlink(path: &str) -> Result<bool, String>
Checks if the file is symlink
Examples
use file_ext::FileExt;
#[test]
fn link_points_to() {
let path = "test/index_rewrite";
let is_symlink = FileExt::is_symlink(path).unwrap();
assert!(is_symlink);
}sourcepub fn symlink_points_to(path: &str) -> Result<String, String>
pub fn symlink_points_to(path: &str) -> Result<String, String>
Returns path to a file, symlink points to
Examples
use file_ext::FileExt;
#[test]
fn link_points_to() {
let path = "test/index_rewrite";
let points_to = FileExt::symlink_points_to(path).unwrap();
assert_eq!("index.html", points_to);
}