@@ -4,7 +4,8 @@ use std::ops::Deref;
4
4
use std:: fmt;
5
5
6
6
use crate :: compiler:: tokens:: Span ;
7
- use crate :: value:: { Value , ValueMap , ValueRepr } ;
7
+ use crate :: key:: Key ;
8
+ use crate :: value:: { MapType , Value , ValueMap , ValueRepr } ;
8
9
9
10
/// Container for nodes with location info.
10
11
///
@@ -63,6 +64,9 @@ pub enum Stmt<'a> {
63
64
Include ( Spanned < Include < ' a > > ) ,
64
65
AutoEscape ( Spanned < AutoEscape < ' a > > ) ,
65
66
FilterBlock ( Spanned < FilterBlock < ' a > > ) ,
67
+ Macro ( Spanned < Macro < ' a > > ) ,
68
+ Import ( Spanned < Import < ' a > > ) ,
69
+ FromImport ( Spanned < FromImport < ' a > > ) ,
66
70
}
67
71
68
72
#[ cfg( feature = "internal_debug" ) ]
@@ -82,6 +86,9 @@ impl<'a> fmt::Debug for Stmt<'a> {
82
86
Stmt :: Include ( s) => fmt:: Debug :: fmt ( s, f) ,
83
87
Stmt :: AutoEscape ( s) => fmt:: Debug :: fmt ( s, f) ,
84
88
Stmt :: FilterBlock ( s) => fmt:: Debug :: fmt ( s, f) ,
89
+ Stmt :: Macro ( s) => fmt:: Debug :: fmt ( s, f) ,
90
+ Stmt :: Import ( s) => fmt:: Debug :: fmt ( s, f) ,
91
+ Stmt :: FromImport ( s) => fmt:: Debug :: fmt ( s, f) ,
85
92
}
86
93
}
87
94
}
@@ -102,6 +109,7 @@ pub enum Expr<'a> {
102
109
Call ( Spanned < Call < ' a > > ) ,
103
110
List ( Spanned < List < ' a > > ) ,
104
111
Map ( Spanned < Map < ' a > > ) ,
112
+ Kwargs ( Spanned < Kwargs < ' a > > ) ,
105
113
}
106
114
107
115
#[ cfg( feature = "internal_debug" ) ]
@@ -121,6 +129,7 @@ impl<'a> fmt::Debug for Expr<'a> {
121
129
Expr :: Call ( s) => fmt:: Debug :: fmt ( s, f) ,
122
130
Expr :: List ( s) => fmt:: Debug :: fmt ( s, f) ,
123
131
Expr :: Map ( s) => fmt:: Debug :: fmt ( s, f) ,
132
+ Expr :: Kwargs ( s) => fmt:: Debug :: fmt ( s, f) ,
124
133
}
125
134
}
126
135
}
@@ -206,6 +215,29 @@ pub struct FilterBlock<'a> {
206
215
pub body : Vec < Stmt < ' a > > ,
207
216
}
208
217
218
+ /// Declares a macro.
219
+ #[ cfg_attr( feature = "internal_debug" , derive( Debug ) ) ]
220
+ pub struct Macro < ' a > {
221
+ pub name : & ' a str ,
222
+ pub args : Vec < Expr < ' a > > ,
223
+ pub defaults : Vec < Expr < ' a > > ,
224
+ pub body : Vec < Stmt < ' a > > ,
225
+ }
226
+
227
+ /// A "from" import
228
+ #[ cfg_attr( feature = "internal_debug" , derive( Debug ) ) ]
229
+ pub struct FromImport < ' a > {
230
+ pub expr : Expr < ' a > ,
231
+ pub names : Vec < ( Expr < ' a > , Option < Expr < ' a > > ) > ,
232
+ }
233
+
234
+ /// A full module import
235
+ #[ cfg_attr( feature = "internal_debug" , derive( Debug ) ) ]
236
+ pub struct Import < ' a > {
237
+ pub expr : Expr < ' a > ,
238
+ pub name : Expr < ' a > ,
239
+ }
240
+
209
241
/// Outputs the expression.
210
242
#[ cfg_attr( feature = "internal_debug" , derive( Debug ) ) ]
211
243
pub struct EmitExpr < ' a > {
@@ -351,6 +383,29 @@ impl<'a> List<'a> {
351
383
}
352
384
}
353
385
386
+ /// Creates a map of kwargs
387
+ #[ cfg_attr( feature = "internal_debug" , derive( Debug ) ) ]
388
+ pub struct Kwargs < ' a > {
389
+ pub pairs : Vec < ( & ' a str , Expr < ' a > ) > ,
390
+ }
391
+
392
+ impl < ' a > Kwargs < ' a > {
393
+ pub fn as_const ( & self ) -> Option < Value > {
394
+ if !self . pairs . iter ( ) . all ( |x| matches ! ( x. 1 , Expr :: Const ( _) ) ) {
395
+ return None ;
396
+ }
397
+
398
+ let mut rv = ValueMap :: new ( ) ;
399
+ for ( key, value) in & self . pairs {
400
+ if let Expr :: Const ( value) = value {
401
+ rv. insert ( Key :: make_string_key ( key) , value. value . clone ( ) ) ;
402
+ }
403
+ }
404
+
405
+ Some ( Value ( ValueRepr :: Map ( rv. into ( ) , MapType :: Kwargs ) ) )
406
+ }
407
+ }
408
+
354
409
/// Creates a map of values.
355
410
#[ cfg_attr( feature = "internal_debug" , derive( Debug ) ) ]
356
411
pub struct Map < ' a > {
@@ -376,7 +431,7 @@ impl<'a> Map<'a> {
376
431
}
377
432
}
378
433
379
- Some ( Value ( ValueRepr :: Map ( rv. into ( ) ) ) )
434
+ Some ( Value ( ValueRepr :: Map ( rv. into ( ) , MapType :: Normal ) ) )
380
435
}
381
436
}
382
437
0 commit comments