#case-insensitive #file #search-file #open-file #search-path #file-io #search

yanked qfile

Crate for accessing a file by path, case insensitive. Automatic detection, create a path with a new file or open an existing file

8 stable releases

3.0.1 Feb 21, 2023
3.0.0 Feb 19, 2023
2.2.3 Feb 2, 2023
2.2.2 Jan 21, 2023
0.1.2 Nov 28, 2022

#13 in #search-path

Download history 75/week @ 2024-03-30 16/week @ 2024-04-06 2/week @ 2024-06-29 89/week @ 2024-07-06

91 downloads per month

MIT license

74KB
989 lines

Crate Docs Changelog

Qfile

The Qfile crate provides functionality for accessing a file by path, case-insensitive, including automatic detection, creation of a path with a new file or opening an existing file. It includes several submodules to handle different aspects of file handling, such as initialization, reading, and writing. The crate also defines some custom errors related to file operations.

qfile is not the rust version of qfile from the QT framework

Paths syntax

Unix format

let path1 = "File.txt";
let path2 = "./File.txt";
let path3 = "../../File.txt";
let path4 = String::from("Folder/Folder/File.txt");

Windows format

let path1 = "File.txt";
let path2 = ".\\File.txt";
let path3 = "..\\..\\File.txt";
let path4 = "D:\\Folder\\file.txt";
let path5 = r"D:\Folder\file.txt";
let path6 = String::from("D:\\Folder\\file.txt");

Paths finder

This is the method that kicks off the directory search. It takes in the search location, names of files to search for, excluded directories, whether or not to follow symbolic links, and a channel to send the results back on.

It uses the rayon crate to parallelize the search over multiple threads for better performance.

The algorithm first filters out the excluded directories, and then iterates through the remaining directories to find all the files that match the specified search criteria. If a match is found, the path of the file is sent to the Sender object.

use qfile::{QFilePath,Directory};
use std::sync::mpsc;

let (tx, rx) = mpsc::channel();
QFilePath::find_paths(
    // specifies the directories to search from, where the search should start.
    Directory::ThisPlace(vec!["src", "another_folder", "/home/user/my_project"]),
    // names of items to search in the file system
    vec!["main.rs", "lib.rs", "photo-1-2.jpg"],
    // folders to exclude search
    Some(vec!["src/tests", "another_folder/tmp"]),
    // follow links
    false,
    // Sender channel
    tx,
)?;
for path in rx {
    println!("{}", path.display().to_string());
}

Writing to a file

The method for writing to a file depends on the current context, case insensitive

  • If the file exists - adds new content to the file
  • If file does not exist - creates files and, if necessary, all parent folders specified in the path. After that writes the new content

Example (Sync Code)

 use qfile::{QFilePath, QTraitSync};

 // real path : myFolder/file.txt
 let mut file = QFilePath::add_path("MyFolder/file.TXT")?;
 file.auto_write("text1 text1 text1")?;
 file.auto_write("text2 text2 text2")?;

Example (Async code)

use qfile::{QFilePath, QTraitAsync};
// real path : myFolder/file.txt
let mut file = QFilePath::async_add_path("MyFolder/file.TXT").await?;
file.lock().await.auto_write("text1 text1 text1").await?;
file.lock().await.auto_write("text2 text2 text2").await?;
  • If the path exists, we work with the file
Unix format Windows format
The path we specified: folder1/FolDER2/file.TXT folder1\FolDER2\file.TXT
Real path : ./Folder1/Folder2/file.txt .\Folder1\Folder2\file.txt
Result : ./Folder1/Folder2/file.txt .\Folder1\Folder2\file.txt
  • If the file/path is not found, creates a new path with the file
Unix format Windows format
The path we specified: ./main_folder/folder_new/file.txt .\main_folder\folder_new\file.txt
Real path : ./Main_Folder .\Main_Folder
Result : ./Main_Folder/folder_new/file.txt .\Main_Folder\folder_new\file.txt
  • The Windows file system treats file and directory names as case insensitive. file.txt and FILE.txt will be treated as equivalent files (Although the path is case insensitive in windows (..\FOLDER\file.txt), you can return a case-sensitive path with : get_path_string() or get_path_buf()).

Reading a file

Method for reading the contents of a file (String), case insensitive

Example (Sync code)

use qfile::{QFilePath, QTraitSync};
    
// real path : myFolder/file.txt
let mut file = QFilePath::add_path("MyFolder/file.TXT")?;
let text = file.read()?;
println!("content: {}", text);

Example (Aync code)

use qfile::{QFilePath, QTraitAsync};

// real path : myFolder/file.txt
let mut file = QFilePath::async_add_path("MyFolder/file.TXT").await?;
let text = file.lock().await.async_read().await?;
println!("content: {}", text);

Changelog

List

License

MIT

Dependencies

~5–14MB
~153K SLoC