classnames/classes/
duo_class.rs1use crate::classes::OptionClass;
2use crate::Class;
3use ::std::borrow::Cow;
4use ::std::convert::From;
5use ::std::fmt;
6use ::std::ops::Add;
7
8#[derive(Clone, PartialEq, Debug)]
9pub struct DuoClass<L, R> {
10    left: L,
11    right: R,
12}
13
14impl<L, R> Copy for DuoClass<L, R>
15where
16    L: Copy,
17    R: Copy,
18{
19}
20
21impl<L, R> Class for DuoClass<L, R>
22where
23    L: fmt::Display + Sized + PartialEq + Clone,
24    R: fmt::Display + Sized + PartialEq + Clone,
25{
26}
27
28impl<L, R> DuoClass<L, R>
29where
30    L: fmt::Display + Sized,
31    R: fmt::Display + Sized,
32{
33    pub(crate) fn new(left: L, right: R) -> Self {
34        Self { left, right }
35    }
36}
37
38impl<'s, L, R> Add<&'s str> for DuoClass<L, R>
39where
40    L: fmt::Display + Sized,
41    R: fmt::Display + Sized,
42{
43    type Output = DuoClass<Self, &'s str>;
44
45    fn add(self, other: &'s str) -> Self::Output {
46        DuoClass::new(self, other)
47    }
48}
49
50impl<L, R, O> Add<O> for DuoClass<L, R>
51where
52    L: fmt::Display + Sized,
53    R: fmt::Display + Sized,
54    O: Class,
55{
56    type Output = DuoClass<Self, O>;
57
58    fn add(self, other: O) -> Self::Output {
59        DuoClass::new(self, other)
60    }
61}
62
63impl<L, R, O> Add<Option<O>> for DuoClass<L, R>
64where
65    L: fmt::Display + Sized,
66    R: fmt::Display + Sized,
67    O: Class,
68{
69    type Output = DuoClass<Self, OptionClass<O>>;
70
71    fn add(self, other: Option<O>) -> Self::Output {
72        DuoClass::new(self, OptionClass::new(other))
73    }
74}
75
76impl<L: fmt::Display, R: fmt::Display> fmt::Display for DuoClass<L, R> {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        write!(f, "{} {}", self.left, self.right)
79    }
80}
81
82impl<'a, L: fmt::Display, R: fmt::Display> From<DuoClass<L, R>> for Cow<'a, str> {
83    fn from(class: DuoClass<L, R>) -> Self {
84        class.to_string().into()
85    }
86}
87
88impl<'a, L: fmt::Display, R: fmt::Display> From<DuoClass<L, R>> for String {
89    fn from(class: DuoClass<L, R>) -> Self {
90        class.to_string()
91    }
92}