@@ -82,7 +82,7 @@ fn baml_value_to_term<'a>(env: Env<'a>, value: &BamlValue, client: &Client) -> N
8282 . collect ( ) ;
8383 Ok ( terms?. encode ( env) )
8484 }
85- BamlValue :: Map ( map) | BamlValue :: Class ( _ , map ) => {
85+ BamlValue :: Map ( map) => {
8686 let mut result_map = Term :: map_new ( env) ;
8787 for ( key, value) in map. iter ( ) {
8888 let value_term = baml_value_to_term ( env, value, client) ?;
@@ -92,18 +92,39 @@ fn baml_value_to_term<'a>(env: Env<'a>, value: &BamlValue, client: &Client) -> N
9292 }
9393 Ok ( result_map)
9494 }
95+ BamlValue :: Class ( class_name, map) => {
96+ let mut result_map = Term :: map_new ( env) ;
97+ let class_atom = rustler:: Atom :: from_str ( env, "__baml_class__" )
98+ . map_err ( |_| Error :: Term ( Box :: new ( "Failed to create atom" ) ) ) ?;
99+ result_map = result_map
100+ . map_put ( class_atom. encode ( env) , class_name. encode ( env) )
101+ . map_err ( |_| Error :: Term ( Box :: new ( "Failed to add class name" ) ) ) ?;
102+ for ( key, value) in map. iter ( ) {
103+ let key_atom = rustler:: Atom :: from_str ( env, key)
104+ . map_err ( |_| Error :: Term ( Box :: new ( "Failed to create key atom" ) ) ) ?;
105+ let value_term = baml_value_to_term ( env, value, client) ?;
106+ result_map = result_map
107+ . map_put ( key_atom. encode ( env) , value_term)
108+ . map_err ( |_| Error :: Term ( Box :: new ( "Failed to add key to map" ) ) ) ?;
109+ }
110+ Ok ( result_map)
111+ }
95112 BamlValue :: Media ( _media) => {
96113 // For now, return an error since we need to check the actual BamlMedia structure
97114 Err ( Error :: Term ( Box :: new ( "Media type not yet supported" ) ) )
98115 }
99116 BamlValue :: Enum ( enum_type, variant) => {
100- // Convert enum to a map with type and variant
117+ // Convert enum to a map with __baml_enum__ and value
101118 let mut result_map = Term :: map_new ( env) ;
119+ let enum_atom = rustler:: Atom :: from_str ( env, "__baml_enum__" )
120+ . map_err ( |_| Error :: Term ( Box :: new ( "Failed to create enum atom" ) ) ) ?;
121+ let value_atom = rustler:: Atom :: from_str ( env, "value" )
122+ . map_err ( |_| Error :: Term ( Box :: new ( "Failed to create value atom" ) ) ) ?;
102123 result_map = result_map
103- . map_put ( "type" . encode ( env) , enum_type. encode ( env) )
124+ . map_put ( enum_atom . encode ( env) , enum_type. encode ( env) )
104125 . map_err ( |_| Error :: Term ( Box :: new ( "Failed to add enum type" ) ) ) ?;
105126 result_map = result_map
106- . map_put ( "variant" . encode ( env) , variant. encode ( env) )
127+ . map_put ( value_atom . encode ( env) , variant. encode ( env) )
107128 . map_err ( |_| Error :: Term ( Box :: new ( "Failed to add enum variant" ) ) ) ?;
108129 Ok ( result_map)
109130 }
0 commit comments