1 | use std::{ |
2 | ffi::OsStr, |
3 | fmt::{self, Write}, |
4 | path::Path, |
5 | }; |
6 | |
7 | pub(super) struct JoinOsStrs<'a, T> { |
8 | pub(super) slice: &'a [T], |
9 | pub(super) delimiter: char, |
10 | } |
11 | |
12 | impl<T> fmt::Display for JoinOsStrs<'_, T> |
13 | where |
14 | T: AsRef<OsStr>, |
15 | { |
16 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
17 | let len: usize = self.slice.len(); |
18 | for (index: usize, os_str: &T) in self.slice.iter().enumerate() { |
19 | // TODO: Use OsStr::display once it is stablised, |
20 | // Path and OsStr has the same `Display` impl |
21 | write!(f, " {}" , Path::new(os_str).display())?; |
22 | if index + 1 < len { |
23 | f.write_char(self.delimiter)?; |
24 | } |
25 | } |
26 | Ok(()) |
27 | } |
28 | } |
29 | |
30 | pub(super) struct OptionOsStrDisplay<T>(pub(super) Option<T>); |
31 | |
32 | impl<T> fmt::Display for OptionOsStrDisplay<T> |
33 | where |
34 | T: AsRef<OsStr>, |
35 | { |
36 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
37 | // TODO: Use OsStr::display once it is stablised |
38 | // Path and OsStr has the same `Display` impl |
39 | if let Some(os_str: &T) = self.0.as_ref() { |
40 | write!(f, "Some( {})" , Path::new(os_str).display()) |
41 | } else { |
42 | f.write_str(data:"None" ) |
43 | } |
44 | } |
45 | } |
46 | |