@@ -63,7 +63,7 @@ impl<'a> BuiltinFn<'a> {
6363 /// The default set of builtin functions
6464 ///
6565 /// This also defines the lookup index for builtins during compilation
66- pub const DEFAULT_BUILTINS : [ BuiltinFn < ' a > ; 17 ] = [
66+ pub const DEFAULT_BUILTINS : [ BuiltinFn < ' a > ; 19 ] = [
6767 BuiltinFn :: ID ,
6868 BuiltinFn :: NOOP ,
6969 BuiltinFn :: IS_EMPTY ,
@@ -81,6 +81,8 @@ impl<'a> BuiltinFn<'a> {
8181 BuiltinFn :: TYPE ,
8282 BuiltinFn :: EQ ,
8383 BuiltinFn :: NOT ,
84+ BuiltinFn :: BASE64_ENCODE ,
85+ BuiltinFn :: BASE64_DECODE ,
8486 ] ;
8587
8688 // Builtin Definitions
@@ -546,6 +548,62 @@ impl<'a> BuiltinFn<'a> {
546548
547549 Ok ( Value :: Bool ( !value) )
548550 }
551+
552+ /// Return a base64 encoded the [`Value`] passed in
553+ ///
554+ /// `(base64_encode ?string_prompt)`
555+ pub const BASE64_ENCODE : BuiltinFn < ' static > = BuiltinFn {
556+ name : "b64encode" ,
557+ args : & [ FnArg {
558+ name : "value" ,
559+ ty : Type :: Value ,
560+ variadic : false ,
561+ } ] ,
562+ return_type : Type :: Value ,
563+ func : Self :: base64_encode,
564+ } ;
565+
566+ fn base64_encode ( args : Vec < Value > ) -> ExprResult < Value > {
567+ let string_arg = args
568+ . first ( )
569+ . expect ( "should have string expression passed" )
570+ . get_string ( ) ?;
571+
572+ use base64:: prelude:: * ;
573+
574+ let encoded = BASE64_STANDARD . encode ( string_arg) ;
575+
576+ Ok ( Value :: String ( encoded) )
577+ }
578+
579+ /// Return a base64 decoded of the [`Value`] passed in
580+ ///
581+ /// `(base64_encode ?string_prompt)`
582+ pub const BASE64_DECODE : BuiltinFn < ' static > = BuiltinFn {
583+ name : "b64decode" ,
584+ args : & [ FnArg {
585+ name : "value" ,
586+ ty : Type :: Value ,
587+ variadic : false ,
588+ } ] ,
589+ return_type : Type :: Value ,
590+ func : Self :: base64_decode,
591+ } ;
592+
593+ fn base64_decode ( args : Vec < Value > ) -> ExprResult < Value > {
594+ let string_arg = args
595+ . first ( )
596+ . expect ( "should have string expression passed" )
597+ . get_string ( ) ?;
598+
599+ use base64:: prelude:: * ;
600+
601+ let decoded = BASE64_STANDARD
602+ . decode ( string_arg)
603+ . expect ( "unable to decode" ) ;
604+
605+ Ok ( Value :: String ( String :: from_utf8 ( decoded) . unwrap ( ) ) )
606+ }
549607}
550608
551609impl < ' a > PartialEq for BuiltinFn < ' a > {
0 commit comments