GithubHelp home page GithubHelp logo

relative-path's Introduction

relative-path

Portable, relative paths for Rust.

Build Status Build status

Documentation

Relative Paths

This library provides two types: RelativePath and RelativePathBuf. These are analogous to the Path and PathBuf structures provided through standard Rust.

While Path provides an API that adapts to a given platform, RelativePath does not. Instead RelativePath uses a fixed, slash-separated representation regardless of platform.

Windows permits using drive volumes as a prefix (e.g. "c:\") and backslash (\) as a separator. Storing paths like this would build and run on one platform, but not others. Another hairy issue is that data serialized for one platform through serde might not be usable on another:

#[derive(Serialize, Deserialize)]
struct Message {
    path: PathBuf,
}

Since RelativePath only uses / as a separator it avoids this issue. Anything non-slash else will be considered part of a distinc component.

Conversion to Path may only happen if it is known which path it is relative to through the to_path function. This is where the relative part of the name comes from.

let relative_path = RelativePath::new("foo/bar");
let path = Path::new("C:\\");
let full_path = relative_path.to_path(path);

This would permit relative paths to portably be used in project manifests or configurations. Where files are referenced from some specific, well-known point in the filesystem.

The following is an example configuration for a made up project:

lib = "src/lib.rs"
bin = "src/bin/hello.rs"

The corresponding Rust struct to deserialize it would look like this:

#[derive(Deserialize)]
pub struct Manifest {
    lib: RelativePathBuf,
    bin: RelativePathBuf,
}

Portability Note

RelativePath, similarly to Path, makes no guarantees that the components represented in them makes up legal file names.

NUL is not permitted on unix platforms - this is a terminator in C-based filesystem APIs. Slash (/) is also used as a path separator.

Windows has a number of reserved characters.

TODO

  • Verify that relative paths are - indeed - portable.
  • Better function documentation with examples.
  • Support more Path-like functions.

relative-path's People

Contributors

udoprog avatar

Watchers

James Cloos avatar  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.