11#![ allow( missing_docs) ] //TODO: temporary allow
22
3+ use std:: borrow:: Cow ;
34use std:: marker:: PhantomData ;
45
56use itertools:: Itertools ;
@@ -50,7 +51,7 @@ pub enum Expr {
5051 WithOpacity ( WithOpacityExpr ) ,
5152
5253 #[ serde( untagged) ]
53- Literal ( ExprValue < String > ) ,
54+ Value ( ExprValue < ' static > ) ,
5455}
5556
5657#[ derive( Debug , Clone , PartialEq , Serialize ) ]
@@ -63,7 +64,7 @@ pub type BoolExpr = TypedExpr<bool>;
6364
6465impl < T > Default for TypedExpr < T > {
6566 fn default ( ) -> Self {
66- Self ( Expr :: Literal ( ExprValue :: Null ) , PhantomData )
67+ Self ( Expr :: Value ( ExprValue :: Null ) , PhantomData )
6768 }
6869}
6970
@@ -91,6 +92,15 @@ impl TypedExpr<bool> {
9192 }
9293}
9394
95+ impl TypedExpr < Vec < f64 > > {
96+ pub fn eval < ' a > ( & ' a self , f : & ' a impl ExprFeature , v : ExprView ) -> Option < Cow < ' a , [ f64 ] > > {
97+ match self . 0 . eval ( f, v) {
98+ ExprValue :: NumArray ( arr) => Some ( arr) ,
99+ _ => None ,
100+ }
101+ }
102+ }
103+
94104impl < Out > From < Expr > for TypedExpr < Out > {
95105 fn from ( value : Expr ) -> Self {
96106 Self :: new ( value)
@@ -99,37 +109,43 @@ impl<Out> From<Expr> for TypedExpr<Out> {
99109
100110impl From < Color > for TypedExpr < Color > {
101111 fn from ( value : Color ) -> Self {
102- Expr :: Literal ( value. into ( ) ) . into ( )
112+ Expr :: Value ( value. into ( ) ) . into ( )
103113 }
104114}
105115
106116impl From < f64 > for TypedExpr < f64 > {
107117 fn from ( value : f64 ) -> Self {
108- Expr :: Literal ( value. into ( ) ) . into ( )
118+ Expr :: Value ( value. into ( ) ) . into ( )
109119 }
110120}
111121
112122impl From < Color > for Expr {
113123 fn from ( value : Color ) -> Self {
114- Expr :: Literal ( value. into ( ) )
124+ Expr :: Value ( value. into ( ) )
115125 }
116126}
117127
118128impl From < f64 > for Expr {
119129 fn from ( value : f64 ) -> Self {
120- Expr :: Literal ( value. into ( ) )
130+ Expr :: Value ( value. into ( ) )
121131 }
122132}
123133
124134impl From < String > for Expr {
125135 fn from ( value : String ) -> Self {
126- Expr :: Literal ( value. into ( ) )
136+ Expr :: Value ( value. into ( ) )
127137 }
128138}
129139
130140impl From < bool > for Expr {
131141 fn from ( value : bool ) -> Self {
132- Expr :: Literal ( value. into ( ) )
142+ Expr :: Value ( value. into ( ) )
143+ }
144+ }
145+
146+ impl From < Vec < f64 > > for Expr {
147+ fn from ( value : Vec < f64 > ) -> Self {
148+ Expr :: Value ( value. into ( ) )
133149 }
134150}
135151
@@ -161,13 +177,13 @@ impl<'de, Out> Deserialize<'de> for TypedExpr<Out> {
161177}
162178
163179pub trait ExprFeature {
164- fn property ( & self , property_name : & str ) -> ExprValue < & str > ;
180+ fn property ( & self , property_name : & str ) -> ExprValue < ' _ > ;
165181 fn geom_type ( & self ) -> ExprGeometryType ;
166182}
167183
168184pub struct EmptyExprFeature ;
169185impl ExprFeature for EmptyExprFeature {
170- fn property ( & self , _property_name : & str ) -> ExprValue < & str > {
186+ fn property ( & self , _property_name : & str ) -> ExprValue < ' _ > {
171187 ExprValue :: Null
172188 }
173189
@@ -183,9 +199,9 @@ pub struct ExprView {
183199}
184200
185201impl Expr {
186- pub fn eval < ' a > ( & ' a self , f : & ' a impl ExprFeature , v : ExprView ) -> ExprValue < & ' a str > {
202+ pub fn eval < ' a > ( & ' a self , f : & ' a impl ExprFeature , v : ExprView ) -> ExprValue < ' a > {
187203 match self {
188- Expr :: Literal ( x) => x. borrowed ( ) ,
204+ Expr :: Value ( x) => x. borrowed ( ) ,
189205 Expr :: All ( exprs) => exprs. iter ( ) . all ( |expr| expr. eval ( f, v) . to_bool ( ) ) . into ( ) ,
190206 Expr :: Any ( exprs) => exprs. iter ( ) . any ( |expr| expr. eval ( f, v) . to_bool ( ) ) . into ( ) ,
191207 Expr :: Not ( expr) => ( !expr. eval ( f, v) . to_bool ( ) ) . into ( ) ,
@@ -218,9 +234,9 @@ impl Expr {
218234 }
219235}
220236
221- impl From < ExprValue < String > > for Expr {
222- fn from ( value : ExprValue < String > ) -> Self {
223- Self :: Literal ( value)
237+ impl From < ExprValue < ' static > > for Expr {
238+ fn from ( value : ExprValue < ' static > ) -> Self {
239+ Self :: Value ( value)
224240 }
225241}
226242
@@ -231,7 +247,7 @@ pub struct WithOpacityExpr {
231247}
232248
233249impl WithOpacityExpr {
234- pub fn eval < ' a > ( & ' a self , f : & ' a impl ExprFeature , v : ExprView ) -> ExprValue < & ' a str > {
250+ pub fn eval < ' a > ( & ' a self , f : & ' a impl ExprFeature , v : ExprView ) -> ExprValue < ' a > {
235251 let Some ( color) = self . color . eval ( f, v) . as_color ( ) else {
236252 return ExprValue :: Null ;
237253 } ;
@@ -258,7 +274,7 @@ mod tests {
258274 expr. 0 ,
259275 Expr :: Eq (
260276 Box :: new( Expr :: Get ( "kind" . to_string( ) ) ) ,
261- Box :: new( Expr :: Literal ( ExprValue :: String ( "road" . to_string( ) ) ) ) ,
277+ Box :: new( Expr :: Value ( "road" . to_string( ) . into ( ) ) ) ,
262278 )
263279 ) ;
264280 }
@@ -267,7 +283,7 @@ mod tests {
267283 fn deserialize_expr_from_num ( ) {
268284 let json = "42" ;
269285 let expr: NumExpr = serde_json:: from_str ( json) . unwrap ( ) ;
270- assert_eq ! ( expr. 0 , Expr :: Literal ( ExprValue :: Number ( 42.0 ) ) ) ;
286+ assert_eq ! ( expr. 0 , Expr :: Value ( ExprValue :: Number ( 42.0 ) ) ) ;
271287 }
272288
273289 #[ test]
0 commit comments