Giter VIP home page Giter VIP logo

fs-utils-rs's Introduction

fs-utils-rs's People

Contributors

byron avatar frol avatar white-oak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

fs-utils-rs's Issues

Extra functionality

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.