byron / fs-utils-rs Goto Github PK
View Code? Open in Web Editor NEWUtilities to help working with the filesytem
Home Page: https://docs.rs/fs-utils
License: Apache License 2.0
Utilities to help working with the filesytem
Home Page: https://docs.rs/fs-utils
License: Apache License 2.0
I am currently decoupling my small FS utils from my current project and I am considering either to contribute them to an existing crate or to create my own crate. Would you be interested in receiving the following utils?
clean up a folder (drop all the files and directories from the target folder, while keeping the target folder):
pub fn cleanup_folder<P: AsRef<Path>>(folder_path: P) -> io::Result<()> {
for entry in fs::read_dir(&folder_path)? {
let path = entry?.path();
if path.is_dir() {
fs::remove_dir_all(&path)?;
} else {
fs::remove_file(&path)?;
}
}
if fs::read_dir(&folder_path)?.take(1).count() > 0 {
return Err(io::Error::new(io::ErrorKind::Other, "The folder is not clean"));
}
Ok(())
}
helper to read only first N bytes from a file (like head
util)
pub fn read_utf8_with_limit<P: AsRef<Path>>(path: P, limit: usize) -> io::Result<String> {
let truncation_message = "... (truncated output)";
let file_size = fs::metadata(&path)?.len() as usize;
let (read_buffer_size, read_limit, is_truncated) = if file_size <= limit {
(file_size, file_size, false)
} else {
(limit, limit - truncation_message.len(), true)
};
let mut read_buffer = Vec::with_capacity(read_buffer_size);
read_buffer.resize(read_buffer_size, 0);
fs::File::open(&path)?.read_exact(&mut read_buffer[..read_limit])?;
if is_truncated {
read_buffer[read_limit..].copy_from_slice(truncation_message.as_bytes());
}
Ok(String::from_utf8_lossy(&read_buffer).into_owned())
}
P.S. The presented snippets are just copied as is and I can improve them while preparing a PR.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.