@@ -20,6 +20,7 @@ use alloc::{
20
20
} ;
21
21
22
22
use core:: fmt:: { self , Display } ;
23
+ use core:: ops:: Deref ;
23
24
24
25
#[ cfg( feature = "serde" ) ]
25
26
use serde:: { Deserialize , Serialize } ;
@@ -993,7 +994,26 @@ impl fmt::Display for LambdaFunction {
993
994
994
995
/// Encapsulates the common pattern in SQL where either one unparenthesized item
995
996
/// such as an identifier or expression is permitted, or multiple of the same
996
- /// item in a parenthesized list.
997
+ /// item in a parenthesized list. For accessing items regardless of the form,
998
+ /// `OneOrManyWithParens` implements `Deref<Target = [T]>` and `IntoIterator`,
999
+ /// so you can call slice methods on it and iterate over items
1000
+ /// # Examples
1001
+ /// Acessing as a slice:
1002
+ /// ```
1003
+ /// # use sqlparser::ast::OneOrManyWithParens;
1004
+ /// let one = OneOrManyWithParens::One("a");
1005
+ ///
1006
+ /// assert_eq!(one[0], "a");
1007
+ /// assert_eq!(one.len(), 1);
1008
+ /// ```
1009
+ /// Iterating:
1010
+ /// ```
1011
+ /// # use sqlparser::ast::OneOrManyWithParens;
1012
+ /// let one = OneOrManyWithParens::One("a");
1013
+ /// let many = OneOrManyWithParens::Many(vec!["a", "b"]);
1014
+ ///
1015
+ /// assert_eq!(one.into_iter().chain(many).collect::<Vec<_>>(), vec!["a", "a", "b"] );
1016
+ /// ```
997
1017
#[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
998
1018
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
999
1019
#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
@@ -1004,6 +1024,125 @@ pub enum OneOrManyWithParens<T> {
1004
1024
Many ( Vec < T > ) ,
1005
1025
}
1006
1026
1027
+ impl < T > Deref for OneOrManyWithParens < T > {
1028
+ type Target = [ T ] ;
1029
+
1030
+ fn deref ( & self ) -> & [ T ] {
1031
+ match self {
1032
+ OneOrManyWithParens :: One ( one) => core:: slice:: from_ref ( one) ,
1033
+ OneOrManyWithParens :: Many ( many) => many,
1034
+ }
1035
+ }
1036
+ }
1037
+
1038
+ impl < T > AsRef < [ T ] > for OneOrManyWithParens < T > {
1039
+ fn as_ref ( & self ) -> & [ T ] {
1040
+ self
1041
+ }
1042
+ }
1043
+
1044
+ impl < ' a , T > IntoIterator for & ' a OneOrManyWithParens < T > {
1045
+ type Item = & ' a T ;
1046
+ type IntoIter = core:: slice:: Iter < ' a , T > ;
1047
+
1048
+ fn into_iter ( self ) -> Self :: IntoIter {
1049
+ self . iter ( )
1050
+ }
1051
+ }
1052
+
1053
+ /// Owned iterator implementation of `OneOrManyWithParens`
1054
+ #[ derive( Debug , Clone ) ]
1055
+ pub struct OneOrManyWithParensIntoIter < T > {
1056
+ inner : OneOrManyWithParensIntoIterInner < T > ,
1057
+ }
1058
+
1059
+ #[ derive( Debug , Clone ) ]
1060
+ enum OneOrManyWithParensIntoIterInner < T > {
1061
+ One ( core:: iter:: Once < T > ) ,
1062
+ Many ( <Vec < T > as IntoIterator >:: IntoIter ) ,
1063
+ }
1064
+
1065
+ impl < T > core:: iter:: FusedIterator for OneOrManyWithParensIntoIter < T >
1066
+ where
1067
+ core:: iter:: Once < T > : core:: iter:: FusedIterator ,
1068
+ <Vec < T > as IntoIterator >:: IntoIter : core:: iter:: FusedIterator ,
1069
+ {
1070
+ }
1071
+
1072
+ impl < T > core:: iter:: ExactSizeIterator for OneOrManyWithParensIntoIter < T >
1073
+ where
1074
+ core:: iter:: Once < T > : core:: iter:: ExactSizeIterator ,
1075
+ <Vec < T > as IntoIterator >:: IntoIter : core:: iter:: ExactSizeIterator ,
1076
+ {
1077
+ }
1078
+
1079
+ impl < T > core:: iter:: Iterator for OneOrManyWithParensIntoIter < T > {
1080
+ type Item = T ;
1081
+
1082
+ fn next ( & mut self ) -> Option < Self :: Item > {
1083
+ match & mut self . inner {
1084
+ OneOrManyWithParensIntoIterInner :: One ( one) => one. next ( ) ,
1085
+ OneOrManyWithParensIntoIterInner :: Many ( many) => many. next ( ) ,
1086
+ }
1087
+ }
1088
+
1089
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1090
+ match & self . inner {
1091
+ OneOrManyWithParensIntoIterInner :: One ( one) => one. size_hint ( ) ,
1092
+ OneOrManyWithParensIntoIterInner :: Many ( many) => many. size_hint ( ) ,
1093
+ }
1094
+ }
1095
+
1096
+ fn count ( self ) -> usize
1097
+ where
1098
+ Self : Sized ,
1099
+ {
1100
+ match self . inner {
1101
+ OneOrManyWithParensIntoIterInner :: One ( one) => one. count ( ) ,
1102
+ OneOrManyWithParensIntoIterInner :: Many ( many) => many. count ( ) ,
1103
+ }
1104
+ }
1105
+
1106
+ fn fold < B , F > ( mut self , init : B , f : F ) -> B
1107
+ where
1108
+ Self : Sized ,
1109
+ F : FnMut ( B , Self :: Item ) -> B ,
1110
+ {
1111
+ match & mut self . inner {
1112
+ OneOrManyWithParensIntoIterInner :: One ( one) => one. fold ( init, f) ,
1113
+ OneOrManyWithParensIntoIterInner :: Many ( many) => many. fold ( init, f) ,
1114
+ }
1115
+ }
1116
+ }
1117
+
1118
+ impl < T > core:: iter:: DoubleEndedIterator for OneOrManyWithParensIntoIter < T > {
1119
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
1120
+ match & mut self . inner {
1121
+ OneOrManyWithParensIntoIterInner :: One ( one) => one. next_back ( ) ,
1122
+ OneOrManyWithParensIntoIterInner :: Many ( many) => many. next_back ( ) ,
1123
+ }
1124
+ }
1125
+ }
1126
+
1127
+ impl < T > IntoIterator for OneOrManyWithParens < T > {
1128
+ type Item = T ;
1129
+
1130
+ type IntoIter = OneOrManyWithParensIntoIter < T > ;
1131
+
1132
+ fn into_iter ( self ) -> Self :: IntoIter {
1133
+ let inner = match self {
1134
+ OneOrManyWithParens :: One ( one) => {
1135
+ OneOrManyWithParensIntoIterInner :: One ( core:: iter:: once ( one) )
1136
+ }
1137
+ OneOrManyWithParens :: Many ( many) => {
1138
+ OneOrManyWithParensIntoIterInner :: Many ( many. into_iter ( ) )
1139
+ }
1140
+ } ;
1141
+
1142
+ OneOrManyWithParensIntoIter { inner }
1143
+ }
1144
+ }
1145
+
1007
1146
impl < T > fmt:: Display for OneOrManyWithParens < T >
1008
1147
where
1009
1148
T : fmt:: Display ,
@@ -6984,4 +7123,178 @@ mod tests {
6984
7123
} ) ;
6985
7124
assert_eq ! ( "INTERVAL '5' SECOND (1, 3)" , format!( "{interval}" ) ) ;
6986
7125
}
7126
+
7127
+ #[ test]
7128
+ fn test_one_or_many_with_parens_deref ( ) {
7129
+ use core:: ops:: Index ;
7130
+
7131
+ let one = OneOrManyWithParens :: One ( "a" ) ;
7132
+
7133
+ assert_eq ! ( one. deref( ) , & [ "a" ] ) ;
7134
+ assert_eq ! ( <OneOrManyWithParens <_> as Deref >:: deref( & one) , & [ "a" ] ) ;
7135
+
7136
+ assert_eq ! ( one[ 0 ] , "a" ) ;
7137
+ assert_eq ! ( one. index( 0 ) , & "a" ) ;
7138
+ assert_eq ! (
7139
+ <<OneOrManyWithParens <_> as Deref >:: Target as Index <usize >>:: index( & one, 0 ) ,
7140
+ & "a"
7141
+ ) ;
7142
+
7143
+ assert_eq ! ( one. len( ) , 1 ) ;
7144
+ assert_eq ! ( <OneOrManyWithParens <_> as Deref >:: Target :: len( & one) , 1 ) ;
7145
+
7146
+ let many1 = OneOrManyWithParens :: Many ( vec ! [ "b" ] ) ;
7147
+
7148
+ assert_eq ! ( many1. deref( ) , & [ "b" ] ) ;
7149
+ assert_eq ! ( <OneOrManyWithParens <_> as Deref >:: deref( & many1) , & [ "b" ] ) ;
7150
+
7151
+ assert_eq ! ( many1[ 0 ] , "b" ) ;
7152
+ assert_eq ! ( many1. index( 0 ) , & "b" ) ;
7153
+ assert_eq ! (
7154
+ <<OneOrManyWithParens <_> as Deref >:: Target as Index <usize >>:: index( & many1, 0 ) ,
7155
+ & "b"
7156
+ ) ;
7157
+
7158
+ assert_eq ! ( many1. len( ) , 1 ) ;
7159
+ assert_eq ! ( <OneOrManyWithParens <_> as Deref >:: Target :: len( & many1) , 1 ) ;
7160
+
7161
+ let many2 = OneOrManyWithParens :: Many ( vec ! [ "c" , "d" ] ) ;
7162
+
7163
+ assert_eq ! ( many2. deref( ) , & [ "c" , "d" ] ) ;
7164
+ assert_eq ! (
7165
+ <OneOrManyWithParens <_> as Deref >:: deref( & many2) ,
7166
+ & [ "c" , "d" ]
7167
+ ) ;
7168
+
7169
+ assert_eq ! ( many2[ 0 ] , "c" ) ;
7170
+ assert_eq ! ( many2. index( 0 ) , & "c" ) ;
7171
+ assert_eq ! (
7172
+ <<OneOrManyWithParens <_> as Deref >:: Target as Index <usize >>:: index( & many2, 0 ) ,
7173
+ & "c"
7174
+ ) ;
7175
+
7176
+ assert_eq ! ( many2[ 1 ] , "d" ) ;
7177
+ assert_eq ! ( many2. index( 1 ) , & "d" ) ;
7178
+ assert_eq ! (
7179
+ <<OneOrManyWithParens <_> as Deref >:: Target as Index <usize >>:: index( & many2, 1 ) ,
7180
+ & "d"
7181
+ ) ;
7182
+
7183
+ assert_eq ! ( many2. len( ) , 2 ) ;
7184
+ assert_eq ! ( <OneOrManyWithParens <_> as Deref >:: Target :: len( & many2) , 2 ) ;
7185
+ }
7186
+
7187
+ #[ test]
7188
+ fn test_one_or_many_with_parens_as_ref ( ) {
7189
+ let one = OneOrManyWithParens :: One ( "a" ) ;
7190
+
7191
+ assert_eq ! ( one. as_ref( ) , & [ "a" ] ) ;
7192
+ assert_eq ! ( <OneOrManyWithParens <_> as AsRef <_>>:: as_ref( & one) , & [ "a" ] ) ;
7193
+
7194
+ let many1 = OneOrManyWithParens :: Many ( vec ! [ "b" ] ) ;
7195
+
7196
+ assert_eq ! ( many1. as_ref( ) , & [ "b" ] ) ;
7197
+ assert_eq ! ( <OneOrManyWithParens <_> as AsRef <_>>:: as_ref( & many1) , & [ "b" ] ) ;
7198
+
7199
+ let many2 = OneOrManyWithParens :: Many ( vec ! [ "c" , "d" ] ) ;
7200
+
7201
+ assert_eq ! ( many2. as_ref( ) , & [ "c" , "d" ] ) ;
7202
+ assert_eq ! (
7203
+ <OneOrManyWithParens <_> as AsRef <_>>:: as_ref( & many2) ,
7204
+ & [ "c" , "d" ]
7205
+ ) ;
7206
+ }
7207
+
7208
+ #[ test]
7209
+ fn test_one_or_many_with_parens_ref_into_iter ( ) {
7210
+ let one = OneOrManyWithParens :: One ( "a" ) ;
7211
+
7212
+ assert_eq ! ( Vec :: from_iter( & one) , vec![ & "a" ] ) ;
7213
+
7214
+ let many1 = OneOrManyWithParens :: Many ( vec ! [ "b" ] ) ;
7215
+
7216
+ assert_eq ! ( Vec :: from_iter( & many1) , vec![ & "b" ] ) ;
7217
+
7218
+ let many2 = OneOrManyWithParens :: Many ( vec ! [ "c" , "d" ] ) ;
7219
+
7220
+ assert_eq ! ( Vec :: from_iter( & many2) , vec![ & "c" , & "d" ] ) ;
7221
+ }
7222
+
7223
+ #[ test]
7224
+ fn test_one_or_many_with_parens_value_into_iter ( ) {
7225
+ use core:: iter:: once;
7226
+
7227
+ //tests that our iterator implemented methods behaves exactly as it's inner iterator, at every step up to n calls to next/next_back
7228
+ fn test_steps < I > ( ours : OneOrManyWithParens < usize > , inner : I , n : usize )
7229
+ where
7230
+ I : IntoIterator < Item = usize , IntoIter : DoubleEndedIterator + Clone > + Clone ,
7231
+ {
7232
+ fn checks < I > ( ours : OneOrManyWithParensIntoIter < usize > , inner : I )
7233
+ where
7234
+ I : Iterator < Item = usize > + Clone + DoubleEndedIterator ,
7235
+ {
7236
+ assert_eq ! ( ours. size_hint( ) , inner. size_hint( ) ) ;
7237
+ assert_eq ! ( ours. clone( ) . count( ) , inner. clone( ) . count( ) ) ;
7238
+
7239
+ assert_eq ! (
7240
+ ours. clone( ) . fold( 1 , |a, v| a + v) ,
7241
+ inner. clone( ) . fold( 1 , |a, v| a + v)
7242
+ ) ;
7243
+
7244
+ assert_eq ! ( Vec :: from_iter( ours. clone( ) ) , Vec :: from_iter( inner. clone( ) ) ) ;
7245
+ assert_eq ! (
7246
+ Vec :: from_iter( ours. clone( ) . rev( ) ) ,
7247
+ Vec :: from_iter( inner. clone( ) . rev( ) )
7248
+ ) ;
7249
+ }
7250
+
7251
+ let mut ours_next = ours. clone ( ) . into_iter ( ) ;
7252
+ let mut inner_next = inner. clone ( ) . into_iter ( ) ;
7253
+
7254
+ for _ in 0 ..n {
7255
+ checks ( ours_next. clone ( ) , inner_next. clone ( ) ) ;
7256
+
7257
+ assert_eq ! ( ours_next. next( ) , inner_next. next( ) ) ;
7258
+ }
7259
+
7260
+ let mut ours_next_back = ours. clone ( ) . into_iter ( ) ;
7261
+ let mut inner_next_back = inner. clone ( ) . into_iter ( ) ;
7262
+
7263
+ for _ in 0 ..n {
7264
+ checks ( ours_next_back. clone ( ) , inner_next_back. clone ( ) ) ;
7265
+
7266
+ assert_eq ! ( ours_next_back. next_back( ) , inner_next_back. next_back( ) ) ;
7267
+ }
7268
+
7269
+ let mut ours_mixed = ours. clone ( ) . into_iter ( ) ;
7270
+ let mut inner_mixed = inner. clone ( ) . into_iter ( ) ;
7271
+
7272
+ for i in 0 ..n {
7273
+ checks ( ours_mixed. clone ( ) , inner_mixed. clone ( ) ) ;
7274
+
7275
+ if i % 2 == 0 {
7276
+ assert_eq ! ( ours_mixed. next_back( ) , inner_mixed. next_back( ) ) ;
7277
+ } else {
7278
+ assert_eq ! ( ours_mixed. next( ) , inner_mixed. next( ) ) ;
7279
+ }
7280
+ }
7281
+
7282
+ let mut ours_mixed2 = ours. into_iter ( ) ;
7283
+ let mut inner_mixed2 = inner. into_iter ( ) ;
7284
+
7285
+ for i in 0 ..n {
7286
+ checks ( ours_mixed2. clone ( ) , inner_mixed2. clone ( ) ) ;
7287
+
7288
+ if i % 2 == 0 {
7289
+ assert_eq ! ( ours_mixed2. next( ) , inner_mixed2. next( ) ) ;
7290
+ } else {
7291
+ assert_eq ! ( ours_mixed2. next_back( ) , inner_mixed2. next_back( ) ) ;
7292
+ }
7293
+ }
7294
+ }
7295
+
7296
+ test_steps ( OneOrManyWithParens :: One ( 1 ) , once ( 1 ) , 3 ) ;
7297
+ test_steps ( OneOrManyWithParens :: Many ( vec ! [ 2 ] ) , vec ! [ 2 ] , 3 ) ;
7298
+ test_steps ( OneOrManyWithParens :: Many ( vec ! [ 3 , 4 ] ) , vec ! [ 3 , 4 ] , 4 ) ;
7299
+ }
6987
7300
}
0 commit comments