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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use color::Color;
use colored::Colorize;

pub trait Decorized: std::fmt::Display {
    type Color: Color;
    fn decorized(&self) -> colored::ColoredString {
        self.to_string().color(Self::Color::color())
    }
    fn decorized_with_prefix(&self) -> colored::ColoredString {
        self.decorized()
    }
}

impl Decorized for crate::version::Version {
    type Color = color::Cyan;
    fn decorized_with_prefix(&self) -> colored::ColoredString {
        let with_prefix = format!("PHP {}", self);
        with_prefix.color(Self::Color::color())
    }
}
impl Decorized for crate::version::Local {
    type Color = color::Cyan;
    fn decorized_with_prefix(&self) -> colored::ColoredString {
        let with_prefix = format!("PHP {}", self);
        with_prefix.color(Self::Color::color())
    }
}
impl Decorized for crate::version::Alias {
    type Color = color::Cyan;
}
impl Decorized for std::path::Display<'_> {
    type Color = color::Yellow;
}

pub mod color {
    pub trait Color {
        fn color() -> colored::Color;
    }

    pub struct Red {}
    impl Color for Red {
        fn color() -> colored::Color {
            colored::Color::Red
        }
    }
    pub struct Green {}
    impl Color for Green {
        fn color() -> colored::Color {
            colored::Color::Green
        }
    }
    pub struct Yellow {}
    impl Color for Yellow {
        fn color() -> colored::Color {
            colored::Color::Yellow
        }
    }
    pub struct Cyan {}
    impl Color for Cyan {
        fn color() -> colored::Color {
            colored::Color::Cyan
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use derive_more::Display;

    #[derive(Debug, Display)]
    struct Phantom(String);
    impl Decorized for Phantom {
        type Color = color::Red;
    }

    #[test]
    fn test() {
        let phantom = Phantom("phantom data".to_owned());
        println!("normal: {}", phantom);
        println!("decorized: {}", phantom.decorized())
    }

    #[test]
    fn path() {
        let path = std::env::current_dir().unwrap();
        println!("{}", path.display().decorized());
    }
}