changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/util/src/path.rs

changeset 698: 96958d3eb5b0
parent: 1227f932b628
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 //! path module
2 //!
3 //! Helper functions for working with paths on filesystem
4 use std::{
5  io::Error,
6  path::{Component, Path, PathBuf},
7 };
8 
9 /// Given a path provided by the user, determines where generated files
10 /// related to that path should go.
11 // this may be for flate only- move there
12 pub fn local_relative_path<P: AsRef<Path>>(path: P) -> Result<PathBuf, Error> {
13  let mut rel_path = PathBuf::new();
14  let path = path.as_ref();
15  for component in path.components() {
16  match component {
17  Component::Prefix(_) | Component::RootDir | Component::CurDir => {}
18  Component::ParentDir => drop(rel_path.pop()), // noop if empty
19  Component::Normal(name) => rel_path.push(name),
20  }
21  }
22 
23  Ok(rel_path)
24 }