Giter VIP home page Giter VIP logo

rrrodzilla / pathtrim Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 22 KB

pathtrim - When all you need is the last few parts of a path in Rust, this crate implements the TrimmablePath trait on std::path::Path so you can easily obtain the last *n* parts of the path.

Rust 100.00%
file-system fs-utils path path-manipulation path-parser path-shortener rust rust-crate rust-library directory-path

pathtrim's Introduction

pathtrim

pathtrim is a simple, yet powerful Rust library that helps you obtain the last n parts of a filesystem path. It provides a seamless way to work with paths in a cross-platform manner, making it a great choice for projects that need to run on different systems like Unix, macOS, and Windows.

With pathtrim, you get a clean and intuitive API for handling path trimming, out-of-the-box support for different platforms, and the confidence that comes with strong safety guarantees, thanks to the use of Rust's features and ecosystem.

Crates.io Crates.io (Downloads) Continuous integration Docs.rs

๐Ÿ’ก Features

  • Obtain the last n components of a path with just a line of code.
  • Cross-platform: works seamlessly on Unix, macOS, and Windows.
  • Built upon the powerful std::path module.
  • Highly configurable, with a simple and easy-to-understand API.

๐Ÿš€ Getting Started

Add this to your Cargo.toml:

[dependencies]
pathtrim = "0.2.0"

And then import the trait in the file where you want to use it:

use std::path::Path;
use pathtrim::TrimmablePath;

Examples

Basic Usage

use std::path::Path;
use pathtrim::TrimmablePath;

let path = Path::new("/usr/local/bin/application");
let trimmed = path.trim_to_nth(2).unwrap();
assert_eq!(trimmed.to_str().unwrap(), "bin/application");

Different Platforms

Unix:

use std::path::Path;
use pathtrim::TrimmablePath;
let path = Path::new("/usr/local/bin/application");
let trimmed = path.trim_to_nth(3).unwrap();
assert_eq!(trimmed.to_str().unwrap(), "local/bin/application");

Windows:

use std::path::Path;
use pathtrim::TrimmablePath;
let path = Path::new(r"C:\Program Files\package\bin\application");
let trimmed = path.trim_to_nth(2).unwrap();
assert_eq!(trimmed.to_str().unwrap(), r"bin\application");

Edge Cases

use std::path::Path;
use pathtrim::TrimmablePath;
let path = Path::new("/");
let trimmed = path.trim_to_nth(1);
assert!(trimmed.is_none());

๐Ÿ“š Further Reading

To gain a deeper understanding of path components and various related APIs, refer to the official Rust documentation:

License

This project is licensed under the MIT License.

pathtrim's People

Contributors

rrrodzilla avatar

Stargazers

 avatar

Watchers

 avatar

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.