diesel/query_source/aliasing/
field_alias_mapper.rs1use super::{Alias, AliasSource, AliasedField};
2
3use crate::expression;
4use crate::query_source::{Column, Table, TableNotEqual};
5
6pub trait FieldAliasMapper<S> {
18 type Out;
23
24 fn map(self, alias: &Alias<S>) -> Self::Out;
26}
27
28#[doc(hidden)]
29pub trait FieldAliasMapperAssociatedTypesDisjointnessTrick<CT, S, C> {
34 type Out;
35 fn map(column: C, alias: &Alias<S>) -> Self::Out;
36}
37impl<S, C> FieldAliasMapper<S> for C
38where
39 S: AliasSource,
40 C: Column,
41 S::Target: FieldAliasMapperAssociatedTypesDisjointnessTrick<C::Table, S, C>,
42{
43 type Out = <S::Target as FieldAliasMapperAssociatedTypesDisjointnessTrick<C::Table, S, C>>::Out;
44
45 fn map(self, alias: &Alias<S>) -> Self::Out {
46 <S::Target as FieldAliasMapperAssociatedTypesDisjointnessTrick<C::Table, S, C>>::map(
47 self, alias,
48 )
49 }
50}
51
52impl<TS, TC, S, C> FieldAliasMapperAssociatedTypesDisjointnessTrick<TC, S, C> for TS
53where
54 S: AliasSource<Target = TS>,
55 C: Column<Table = TC>,
56 TC: Table,
57 TS: TableNotEqual<TC>,
58{
59 type Out = C;
60
61 fn map(column: C, _alias: &Alias<S>) -> Self::Out {
62 column
64 }
65}
66
67macro_rules! field_alias_mapper {
68 ($(
69 $Tuple:tt {
70 $(($idx:tt) -> $T:ident, $ST:ident, $TT:ident,)+
71 }
72 )+) => {
73 $(
74 impl<_S, $($T,)*> FieldAliasMapper<_S> for ($($T,)*)
75 where
76 _S: AliasSource,
77 $($T: FieldAliasMapper<_S>,)*
78 {
79 type Out = ($(<$T as FieldAliasMapper<_S>>::Out,)*);
80
81 fn map(self, alias: &Alias<_S>) -> Self::Out {
82 (
83 $(self.$idx.map(alias),)*
84 )
85 }
86 }
87 )*
88 }
89}
90
91diesel_derives::__diesel_for_each_tuple!(field_alias_mapper);
92
93impl<SPrev, SNew, F> FieldAliasMapper<SNew> for AliasedField<SPrev, F>
96where
97 SNew: AliasSource,
98{
99 type Out = Self;
100
101 fn map(self, _alias: &Alias<SNew>) -> Self::Out {
102 self
104 }
105}
106
107impl<S, F> FieldAliasMapper<S> for expression::nullable::Nullable<F>
108where
109 F: FieldAliasMapper<S>,
110{
111 type Out = expression::nullable::Nullable<<F as FieldAliasMapper<S>>::Out>;
112
113 fn map(self, alias: &Alias<S>) -> Self::Out {
114 expression::nullable::Nullable::new(self.0.map(alias))
115 }
116}
117
118impl<S, F> FieldAliasMapper<S> for expression::grouped::Grouped<F>
119where
120 F: FieldAliasMapper<S>,
121{
122 type Out = expression::grouped::Grouped<<F as FieldAliasMapper<S>>::Out>;
123
124 fn map(self, alias: &Alias<S>) -> Self::Out {
125 expression::grouped::Grouped(self.0.map(alias))
126 }
127}