git_explore/
util.rs

1use crate::*;
2pub trait ToPath {}
3
4impl ToPath for String {}
5
6pub trait PathEx {
7    fn to_config_path(&self) -> PathBuf;
8    fn to_path(&self) -> PathBuf;
9    fn get_content(&self) -> String;
10}
11
12pub trait ToStringVec {
13    fn to_string_vec(&self) -> Vec<String>;
14}
15fn to_string_vec(path_vec: &[PathBuf]) -> Vec<String> {
16    path_vec
17        .iter()
18        .map(|e| e.to_str().unwrap().to_owned())
19        .collect::<_>()
20}
21
22impl ToStringVec for Vec<PathBuf> {
23    fn to_string_vec(&self) -> Vec<String> {
24        to_string_vec(self)
25    }
26}
27
28impl PathEx for &str {
29    fn to_config_path(&self) -> PathBuf {
30        Path::new(self).join(KEY_CONFIG_PATH)
31    }
32    fn to_path(&self) -> PathBuf {
33        Path::new(self)
34            .canonicalize()
35            .with_context(|| format!("{:?}", self))
36            .unwrap()
37    }
38    fn get_content(&self) -> String {
39        self.to_path().get_content()
40    }
41}
42impl PathEx for String {
43    fn to_config_path(&self) -> PathBuf {
44        self.as_str().to_config_path()
45    }
46    fn to_path(&self) -> PathBuf {
47        self.as_str().to_path()
48    }
49    fn get_content(&self) -> String {
50        self.to_path().get_content()
51    }
52}
53impl PathEx for PathBuf {
54    fn to_config_path(&self) -> PathBuf {
55        self.to_str().unwrap().to_config_path()
56    }
57    fn to_path(&self) -> PathBuf {
58        self.to_str().unwrap().to_path()
59    }
60    fn get_content(&self) -> String {
61        String::from_utf8(
62            std::fs::read(self)
63                .with_context(|| format!("{:?}", self.to_str()))
64                .unwrap(),
65        )
66        .unwrap()
67    }
68}