Skip to main content

smart_format/combinator/
wrap.rs

1use core::fmt;
2
3/// Wraps a value with a prefix and suffix: `prefix + inner + suffix`.
4pub struct Wrap<T, P, S> {
5    inner: T,
6    prefix: P,
7    suffix: S,
8}
9
10impl<T, P, S> Wrap<T, P, S> {
11    pub(crate) fn new(inner: T, prefix: P, suffix: S) -> Self {
12        Wrap {
13            inner,
14            prefix,
15            suffix,
16        }
17    }
18}
19
20impl<T: fmt::Display, P: fmt::Display, S: fmt::Display> fmt::Display for Wrap<T, P, S> {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        self.prefix.fmt(f)?;
23        self.inner.fmt(f)?;
24        self.suffix.fmt(f)
25    }
26}
27
28/// Prepends a prefix to a value: `prefix + inner`.
29pub struct Prefix<T, P> {
30    inner: T,
31    prefix: P,
32}
33
34impl<T, P> Prefix<T, P> {
35    pub(crate) fn new(inner: T, prefix: P) -> Self {
36        Prefix { inner, prefix }
37    }
38}
39
40impl<T: fmt::Display, P: fmt::Display> fmt::Display for Prefix<T, P> {
41    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42        self.prefix.fmt(f)?;
43        self.inner.fmt(f)
44    }
45}
46
47/// Appends a suffix to a value: `inner + suffix`.
48pub struct Suffix<T, S> {
49    inner: T,
50    suffix: S,
51}
52
53impl<T, S> Suffix<T, S> {
54    pub(crate) fn new(inner: T, suffix: S) -> Self {
55        Suffix { inner, suffix }
56    }
57}
58
59impl<T: fmt::Display, S: fmt::Display> fmt::Display for Suffix<T, S> {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        self.inner.fmt(f)?;
62        self.suffix.fmt(f)
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use crate::combinator::SmartFormat;
69
70    #[test]
71    fn wrap_basic() {
72        assert_eq!("[hello]", "hello".display_wrap("[", "]").to_string());
73    }
74
75    #[test]
76    fn wrap_empty_inner() {
77        assert_eq!("[]", "".display_wrap("[", "]").to_string());
78    }
79
80    #[test]
81    fn wrap_empty_affixes() {
82        assert_eq!("hello", "hello".display_wrap("", "").to_string());
83    }
84
85    #[test]
86    fn wrap_unicode() {
87        assert_eq!("«Привіт»", "Привіт".display_wrap("«", "»").to_string());
88    }
89
90    #[test]
91    fn prefix_basic() {
92        assert_eq!("> hello", "hello".display_prefix("> ").to_string());
93    }
94
95    #[test]
96    fn prefix_empty() {
97        assert_eq!("hello", "hello".display_prefix("").to_string());
98    }
99
100    #[test]
101    fn suffix_basic() {
102        assert_eq!("hello!", "hello".display_suffix("!").to_string());
103    }
104
105    #[test]
106    fn suffix_empty() {
107        assert_eq!("hello", "hello".display_suffix("").to_string());
108    }
109
110    #[test]
111    fn chain_prefix_suffix() {
112        let result = "hello".display_prefix("> ").display_suffix("!").to_string();
113        assert_eq!("> hello!", result);
114    }
115
116    #[test]
117    fn wrap_with_display_type() {
118        let n: i32 = 42;
119        assert_eq!("[42]", n.display_wrap("[", "]").to_string());
120    }
121}