1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Utilities for manipulating paths.

use std::path::{Path, PathBuf};

pub trait PathBufExt {
    /// Joins `self` with `path`, returning `self` unmodified if `path` is empty.
    ///
    /// Takes ownership of `self`.
    fn attach<P: AsRef<Path>>(self, path: P) -> PathBuf;
}

impl PathBufExt for PathBuf {
    fn attach<P: AsRef<Path>>(mut self, path: P) -> PathBuf {
        let path: &Path = path.as_ref();
        if path.components().count() > 0 {
            self.push(path)
        }
        self
    }
}