fuchsia_pkg/
path_to_string.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2022 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.

use anyhow::{Context, Result};
use std::path::{Path, PathBuf};

/// Extension trait for getting a String from a Path.
pub trait PathToStringExt {
    /// Convert the path to a string if it is valid UTF-8, and return an error otherwise.
    fn path_to_string(&self) -> Result<String>;
}

impl PathToStringExt for Path {
    fn path_to_string(&self) -> Result<String> {
        self.to_str()
            .context(format!("Path is not valid UTF-8: {}", self.display()))
            .map(str::to_string)
    }
}

impl PathToStringExt for PathBuf {
    fn path_to_string(&self) -> Result<String> {
        self.to_str()
            .context(format!("Path is not valid UTF-8: {}", self.display()))
            .map(str::to_string)
    }
}

#[cfg(test)]
mod tests {
    use super::PathToStringExt;
    use std::ffi::OsString;
    use std::os::unix::ffi::OsStringExt;
    use std::path::PathBuf;

    #[test]
    fn path_to_string() {
        let path = PathBuf::from("/some/path/to/file.txt");
        let string = path.path_to_string().unwrap();
        assert_eq!(string, "/some/path/to/file.txt".to_string());
    }

    #[test]
    fn invalid_path_to_string() {
        let invalid_path = PathBuf::from(OsString::from_vec(b"invalid\xe7".to_vec()));
        assert!(invalid_path.path_to_string().is_err());
    }
}