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
use anyhow::Context;
use std::borrow::Cow;
use std::ffi::OsStr;

const LONG_STRING_DISPLAY_LIMIT: usize = 80;

/// Extend [std::ffi::OsStr] with [anyhow] methods
pub trait OsStrAnyhow {
    /// Produce a utf8 `&str` or provide an error describing the string, truncated if long
    fn to_str_anyhow(&self) -> anyhow::Result<&str>;
}

impl OsStrAnyhow for OsStr {
    fn to_str_anyhow(&self) -> anyhow::Result<&str> {
        self.to_str()
            .ok_or_else(|| anyhow::Error::msg("not valid utf8"))
            .with_context(|| {
                format!(
                    "while processing os string {:?}",
                    truncate_long_strings(self.to_string_lossy())
                )
            })
    }
}

fn truncate_long_strings(s: Cow<'_, str>) -> Cow<'_, str> {
    let sref = s.as_ref();
    let charcnt = sref.chars().count();

    if charcnt <= LONG_STRING_DISPLAY_LIMIT {
        s
    } else {
        const HALF: usize = LONG_STRING_DISPLAY_LIMIT / 2;

        dbg!(Cow::from(format!(
            "{}\u{2772}\u{2026}\u{2773}{}",
            sref.chars().take(HALF).collect::<String>(),
            sref.chars().skip(charcnt - HALF + 3).collect::<String>(),
        )))
    }
}

#[cfg(test)]
mod tests;