1
1
//! Dictionary object
2
- use super :: { BoxedObject , MethodIter , Object , RefValue , Str , Value } ;
2
+ use super :: { BoxedObject , MethodIter , Object , RefValue , Str } ;
3
3
use crate :: value;
4
4
use crate :: Error ;
5
5
use indexmap:: IndexMap ;
6
6
use tokay_macros:: tokay_method;
7
7
extern crate self as tokay;
8
- use num:: ToPrimitive ;
9
8
use std:: cmp:: Ordering ;
10
9
11
10
// Alias for the inner dict
@@ -103,6 +102,25 @@ impl Dict {
103
102
104
103
tokay_method ! ( "dict : @" , Ok ( RefValue :: from( Dict :: new( ) ) ) ) ;
105
104
105
+ tokay_method ! ( "dict_iter : @dict" , {
106
+ // If index is void, create an iterator on keys.
107
+ if dict. is( "dict" ) {
108
+ Ok ( RefValue :: from( MethodIter :: new_method_iter(
109
+ dict. clone( ) ,
110
+ "values" ,
111
+ None ,
112
+ "iinc" ,
113
+ ) ) )
114
+ } else {
115
+ Err ( Error :: from( format!(
116
+ "{} only accepts '{}' as parameter, not '{}'" ,
117
+ __function,
118
+ "dict" ,
119
+ dict. name( )
120
+ ) ) )
121
+ }
122
+ } ) ;
123
+
106
124
tokay_method ! ( "dict_len : @dict" , {
107
125
let dict = dict. borrow( ) ;
108
126
@@ -134,7 +152,7 @@ impl Dict {
134
152
} ) ;
135
153
136
154
// Method to retrieve or iterate the keys of a dict.
137
- tokay_method ! ( "dict_keys : @dict, index=void" , {
155
+ tokay_method ! ( "dict_keys : @dict, index=void, default=void " , {
138
156
// If index is void, create an iterator on keys.
139
157
if index. is_void( ) {
140
158
return Ok ( RefValue :: from( MethodIter :: new_method_iter(
@@ -151,7 +169,37 @@ impl Dict {
151
169
if let Some ( ( key, _) ) = dict. get_index( index. to_usize( ) ?) {
152
170
Ok ( key. clone( ) )
153
171
} else {
154
- Ok ( value!( void) )
172
+ Ok ( default )
173
+ }
174
+ } else {
175
+ Err ( Error :: from( format!(
176
+ "{} only accepts '{}' as parameter, not '{}'" ,
177
+ __function,
178
+ "dict" ,
179
+ dict. name( )
180
+ ) ) )
181
+ }
182
+ } ) ;
183
+
184
+ // Method to retrieve or iterate the values of a dict.
185
+ tokay_method ! ( "dict_values : @dict, index=void, default=void" , {
186
+ // If index is void, create an iterator on keys.
187
+ if index. is_void( ) {
188
+ return Ok ( RefValue :: from( MethodIter :: new_method_iter(
189
+ dict. clone( ) ,
190
+ "values" ,
191
+ None ,
192
+ "iinc" ,
193
+ ) ) ) ;
194
+ }
195
+
196
+ // Otherwise, borrow
197
+ let dict = dict. borrow( ) ;
198
+ if let Some ( dict) = dict. object:: <Dict >( ) {
199
+ if let Some ( ( _, value) ) = dict. get_index( index. to_usize( ) ?) {
200
+ Ok ( value. clone( ) )
201
+ } else {
202
+ Ok ( default )
155
203
}
156
204
} else {
157
205
Err ( Error :: from( format!(
@@ -164,7 +212,7 @@ impl Dict {
164
212
} ) ;
165
213
166
214
// Method to retrieve or iterate a list of [key, value] from a dict by index
167
- tokay_method ! ( "dict_items : @dict, index=void" , {
215
+ tokay_method ! ( "dict_items : @dict, index=void, default=void " , {
168
216
// If index is void, create an iterator on items.
169
217
if index. is_void( ) {
170
218
return Ok ( RefValue :: from( MethodIter :: new_method_iter(
@@ -181,7 +229,7 @@ impl Dict {
181
229
if let Some ( ( key, value) ) = dict. get_index( index. to_usize( ) ?) {
182
230
Ok ( value!( [ ( key. clone( ) ) , ( value. clone( ) ) ] ) )
183
231
} else {
184
- Ok ( value! ( void ) )
232
+ Ok ( default )
185
233
}
186
234
} else {
187
235
Err ( Error :: from( format!(
@@ -209,16 +257,6 @@ impl Dict {
209
257
if let Some ( item) = dict. get( & item) {
210
258
Ok ( item. clone( ) )
211
259
} else {
212
- // In case index is an int that can be turned into an usize,
213
- // try to obtain the dict item by its index
214
- if let Value :: Int ( index) = & * item. borrow( ) {
215
- if let Some ( index) = index. to_usize( ) {
216
- if let Some ( ( _, item) ) = dict. get_index( index) {
217
- return Ok ( item. clone( ) ) ;
218
- }
219
- }
220
- }
221
-
222
260
Ok ( default )
223
261
}
224
262
} else {
0 commit comments