@@ -24,13 +24,14 @@ use std::fmt;
2424
2525use galileo:: expr:: {
2626 ControlPoint , CubicBezierInterpolation , ExponentialInterpolation , Expr , ExprGeometryType ,
27- ExprValue , LinearInterpolation ,
27+ ExprValue , LinearInterpolation , MatchCase , MatchExpr ,
2828} ;
2929use serde:: de:: { self , SeqAccess , Visitor } ;
3030use serde:: { Deserialize , Deserializer , Serialize } ;
3131use serde_json:: Value ;
3232
3333use crate :: layer:: { UNSUPPORTED , log_unsupported} ;
34+ use crate :: style:: color:: parse_css_color;
3435
3536/// The interpolation curve used by [`Expr::Interpolate`] and its color-space
3637/// variants.
@@ -250,7 +251,7 @@ pub enum MlExpr {
250251 /// Retrieves a feature property (or a property from `object_expr`).
251252 Get {
252253 /// The property name expression (typically a string literal).
253- property : Box < MlExpr > ,
254+ property : String ,
254255 /// Optional object to retrieve the property from.
255256 object : Option < Box < MlExpr > > ,
256257 } ,
@@ -809,11 +810,22 @@ impl MlExpr {
809810 } )
810811 }
811812
813+ fn literal ( v : & Value ) -> Option < ExprValue < String > > {
814+ match v {
815+ Value :: Bool ( v) => Some ( ExprValue :: Boolean ( * v) ) ,
816+ Value :: Number ( v) => Some ( ExprValue :: Number ( v. as_f64 ( ) ?) ) ,
817+ Value :: String ( v) => Some (
818+ parse_css_color ( v)
819+ . map ( ExprValue :: from)
820+ . unwrap_or_else ( || ExprValue :: String ( v. clone ( ) ) ) ,
821+ ) ,
822+ Value :: Null => Some ( ExprValue :: Null ) ,
823+ _ => None ,
824+ }
825+ }
826+
812827 Some ( match self {
813- MlExpr :: Literal ( Value :: Bool ( v) ) => ExprValue :: Boolean ( * v) . into ( ) ,
814- MlExpr :: Literal ( Value :: Number ( v) ) => ExprValue :: Number ( v. as_f64 ( ) ?) . into ( ) ,
815- MlExpr :: Literal ( Value :: String ( v) ) => ExprValue :: String ( v. clone ( ) ) . into ( ) ,
816- MlExpr :: Literal ( Value :: Null ) => ExprValue :: Null . into ( ) ,
828+ MlExpr :: Literal ( l) => literal ( l) ?. into ( ) ,
817829 MlExpr :: All ( parts) => Expr :: All (
818830 parts
819831 . iter ( )
@@ -842,6 +854,39 @@ impl MlExpr {
842854 stops,
843855 } => interpolation_to_galileo ( interpolation, input, stops) ?,
844856 MlExpr :: Zoom => Expr :: Zoom ,
857+ MlExpr :: Get { property, object } => {
858+ if object. is_some ( ) {
859+ log_unsupported ! ( format!( "get from an object" ) ) ;
860+ return None ;
861+ }
862+
863+ Expr :: Get ( property. clone ( ) )
864+ }
865+ MlExpr :: Match {
866+ input,
867+ branches,
868+ fallback,
869+ } => Expr :: Match ( Box :: new ( MatchExpr {
870+ input : input. to_galileo_expr ( ) ?,
871+ cases : branches
872+ . iter ( )
873+ . map ( |( values, out) | {
874+ let entries = match values {
875+ Value :: Array ( entries) => entries. clone ( ) ,
876+ v => vec ! [ v. clone( ) ] ,
877+ } ;
878+
879+ Some ( MatchCase {
880+ in_values : entries
881+ . into_iter ( )
882+ . map ( |v| literal ( & v) )
883+ . collect :: < Option < Vec < _ > > > ( ) ?,
884+ out : out. to_galileo_expr ( ) ?,
885+ } )
886+ } )
887+ . collect :: < Option < Vec < _ > > > ( ) ?,
888+ fallback : fallback. to_galileo_expr ( ) ?,
889+ } ) ) ,
845890 _ => {
846891 log:: debug!( "{UNSUPPORTED} Expression {self:?} is not supported yet" ) ;
847892 return None ;
@@ -1052,7 +1097,10 @@ fn parse_expr_array(mut arr: Vec<Value>) -> Result<MlExpr, String> {
10521097 }
10531098
10541099 "get" => {
1055- let property = box_expr ( take_arg ( & mut args, 1 , "get" ) ?) ?;
1100+ let property = take_arg ( & mut args, 1 , "get" ) ?
1101+ . as_str ( )
1102+ . ok_or_else ( || "Expected string value for get argument" . to_string ( ) ) ?
1103+ . to_owned ( ) ;
10561104 let object = if args. is_empty ( ) {
10571105 None
10581106 } else {
@@ -1510,7 +1558,7 @@ mod tests {
15101558 let e = parse ( json ! ( [ "get" , "name" ] ) ) ;
15111559 match e {
15121560 MlExpr :: Get { property, .. } => {
1513- assert ! ( matches! ( * property, MlExpr :: Literal ( Value :: String ( _ ) ) ) ) ;
1561+ assert_eq ! ( property, "name" ) ;
15141562 }
15151563 other => panic ! ( "expected Get, got {other:?}" ) ,
15161564 }
0 commit comments