@@ -2,8 +2,7 @@ use std::collections::HashMap;
22
33use bcrypt:: DEFAULT_COST ;
44use rand:: distr:: { Alphanumeric , SampleString } ;
5- use serde:: de:: DeserializeOwned ;
6- use tera:: { Context , Function , Tera , Value } ;
5+ use tera:: { Context , Kwargs , State , Tera , TeraResult } ;
76
87use crate :: constants:: PASSWORD_LENGTH ;
98
@@ -14,47 +13,25 @@ use crate::constants::PASSWORD_LENGTH;
1413/// - `random_password`: Returns a random password with a relatively secure RNG.
1514/// See [`rand::rng`] for more information.
1615/// - `bcrypt`: Returns the bcyrpt hash of the provided `password` parameter.
17- pub fn render ( content : & str , parameters : & HashMap < String , String > ) -> Result < String , tera :: Error > {
16+ pub fn render ( content : & str , parameters : & HashMap < String , String > ) -> TeraResult < String > {
1817 // Create templating context
1918 let context = Context :: from_serialize ( parameters) ?;
2019
2120 // Create render engine
22- let mut tera = Tera :: default ( ) ;
23- tera. register_function ( "random_password" , random_password ( ) ) ;
24- tera. register_function ( "bcrypt" , bcrypt ( ) ) ;
21+ let mut tera = Tera :: new ( ) ;
22+ tera. register_function ( "random_password" , random_password) ;
23+ tera. register_function ( "bcrypt" , bcrypt) ;
2524
2625 // Render template
27- tera. render_str ( content, & context)
26+ tera. render ( content, & context)
2827}
2928
30- /// Internal helper function to retrieve value of type `T` from the `map` by
31- /// `key`. If the `key` is not present in the `map`, it returns an error.
32- fn get_from_map < T > ( map : & HashMap < String , Value > , key : & str ) -> tera:: Result < T >
33- where
34- T : DeserializeOwned ,
35- {
36- match map. get ( key) {
37- Some ( value) => match tera:: from_value ( value. to_owned ( ) ) {
38- Ok ( extracted) => Ok ( extracted) ,
39- Err ( _) => Err ( format ! ( "Unable to retrieve value of argument {key:?}" ) . into ( ) ) ,
40- } ,
41- None => Err ( format ! ( "Failed to retrieve missing parameter {key:?}" ) . into ( ) ) ,
42- }
29+ pub fn random_password ( _: Kwargs , _: & State ) -> TeraResult < String > {
30+ Ok ( Alphanumeric . sample_string ( & mut rand:: rng ( ) , PASSWORD_LENGTH ) )
4331}
4432
45- fn random_password ( ) -> impl Function {
46- |_args : & HashMap < String , Value > | -> tera:: Result < Value > {
47- let password = Alphanumeric . sample_string ( & mut rand:: rng ( ) , PASSWORD_LENGTH ) ;
48- Ok ( password. into ( ) )
49- }
50- }
51-
52- fn bcrypt ( ) -> impl Function {
53- |args : & HashMap < String , Value > | -> tera:: Result < Value > {
54- let password: String = get_from_map ( args, "password" ) ?;
55- let hash = bcrypt:: hash ( password, DEFAULT_COST )
56- . map_err ( |err| format ! ( "Failed to create bcrypt hash: {err}" ) ) ?;
57-
58- Ok ( hash. into ( ) )
59- }
33+ pub fn bcrypt ( kwargs : Kwargs , _: & State ) -> TeraResult < String > {
34+ let password = kwargs. must_get :: < String > ( "password" ) ?;
35+ bcrypt:: hash ( password, DEFAULT_COST )
36+ . map_err ( |err| tera:: Error :: message ( format ! ( "Failed to create bcrypt hash: {err}" ) ) )
6037}
0 commit comments