path_tools/
lib.rs

1use std::ffi::{OsStr, OsString};
2use std::path::PathBuf;
3
4pub trait WithAdditionalExtension
5where
6    Self: Into<OsString>,
7{
8    fn with_additional_extension(&self, s: impl AsRef<OsStr>) -> Self;
9}
10
11impl WithAdditionalExtension for PathBuf {
12    fn with_additional_extension(&self, s: impl AsRef<OsStr>) -> Self {
13        // from: https://stackoverflow.com/questions/74322541/how-to-append-to-pathbuf
14        let mut p: OsString = self.into();
15        p.push(s);
16        p.into()
17    }
18}
19
20#[cfg(test)]
21mod tests {
22    use super::*;
23    use std::str::FromStr;
24
25    #[test]
26    fn can_append_extension() {
27        let initial = PathBuf::from_str("foo.bar").unwrap();
28        let updated = initial.with_additional_extension(".baz");
29        eprintln!("u : {:?}", updated);
30        assert_eq!(updated, PathBuf::from_str("foo.bar.baz").unwrap());
31    }
32}