@@ -129,14 +129,17 @@ impl<T: Any> AsAny for T {
129129
130130/// Represents the set of application-defined callback functions that the FAPI invokes.
131131///
132+ /// An implementation is **not** required to override *all* callback functions, as the trait provides “blank” default implementations.
133+ ///
134+ /// Generally, an application overrides only the callback functions that it actually needs.
135+ ///
132136/// Implementations of this trait are registered with a FAPI context via the [**`FapiContext::set_callbacks()`**](crate::FapiContext::set_callbacks) function.
133137///
134138/// ### Example
135139///
136140/// Applications shall implement this trait as follows:
137141///
138142/// ```
139- /// #[derive(Debug)]
140143/// pub struct MyCallbacks;
141144///
142145/// impl FapiCallbacks for MyCallbacks {
@@ -157,7 +160,7 @@ impl<T: Any> AsAny for T {
157160/// }
158161/// }
159162/// ```
160- pub trait FapiCallbacks : AsAny + Send + Debug + ' static {
163+ pub trait FapiCallbacks : AsAny + Send + ' static {
161164 /// A callback function that allows the FAPI to request authorization values.
162165 ///
163166 /// The default implementation of this function returns `None`. Please override the function as needed!
@@ -201,11 +204,96 @@ pub trait FapiCallbacks: AsAny + Send + Debug + 'static {
201204 }
202205}
203206
207+ // ==========================================================================
208+ // Callbacks implementation
209+ // ==========================================================================
210+
211+ /// Provides a simple implementation of the [`FapiCallbacks`](crate::FapiCallbacks) trait.
212+ #[ allow( clippy:: type_complexity) ]
213+ pub struct Callbacks {
214+ auth_fn : Box < dyn Fn ( AuthCbParam ) -> Option < Cow < ' static , str > > + Send > ,
215+ sign_fn : Box < dyn Fn ( SignCbParam ) -> Option < Vec < u8 > > + Send > ,
216+ branch_fn : Box < dyn Fn ( BranchCbParam ) -> Option < usize > + Send > ,
217+ policy_action_fn : Box < dyn Fn ( PolicyActionCbParam ) -> bool + Send > ,
218+ }
219+
220+ impl Callbacks {
221+ /// Creates a new `Callbacks` instance with application-defined callback functions.
222+ ///
223+ /// Instances of this struct are registered with a FAPI context via the [**`FapiContext::set_callbacks()`**](crate::FapiContext::set_callbacks) function.
224+ pub fn new < AuthFn , SignFn , BranchFn , PolicyActionFn > ( auth_fn : AuthFn , sign_fn : SignFn , branch_fn : BranchFn , policy_action_fn : PolicyActionFn ) -> Self
225+ where
226+ AuthFn : Fn ( AuthCbParam ) -> Option < Cow < ' static , str > > + Send + ' static ,
227+ SignFn : Fn ( SignCbParam ) -> Option < Vec < u8 > > + Send + ' static ,
228+ BranchFn : Fn ( BranchCbParam ) -> Option < usize > + Send + ' static ,
229+ PolicyActionFn : Fn ( PolicyActionCbParam ) -> bool + Send + ' static ,
230+ {
231+ Self { auth_fn : Box :: new ( auth_fn) , sign_fn : Box :: new ( sign_fn) , branch_fn : Box :: new ( branch_fn) , policy_action_fn : Box :: new ( policy_action_fn) }
232+ }
233+
234+ /// Creates a `Callbacks` instance with an application-defined [`auth_cb`](crate::FapiCallbacks::auth_cb) callback function.
235+ ///
236+ /// All other callback functions will use the default implementation.
237+ pub fn with_auth < F > ( auth_fn : F ) -> Self
238+ where
239+ F : Fn ( AuthCbParam ) -> Option < Cow < ' static , str > > + Send + ' static ,
240+ {
241+ Self :: new ( auth_fn, |_| None , |_| None , |_| false )
242+ }
243+
244+ /// Creates a `Callbacks` instance with an application-defined [`sign_cb`](crate::FapiCallbacks::sign_cb) callback function.
245+ ///
246+ /// All other callback functions will use the default implementation.
247+ pub fn with_sign < F > ( sign_fn : F ) -> Self
248+ where
249+ F : Fn ( SignCbParam ) -> Option < Vec < u8 > > + Send + ' static ,
250+ {
251+ Self :: new ( |_| None , sign_fn, |_| None , |_| false )
252+ }
253+
254+ /// Creates a `Callbacks` instance with an application-defined [`branch_cb`](crate::FapiCallbacks::branch_cb) callback function.
255+ ///
256+ /// All other callback functions will use the default implementation.
257+ pub fn with_branch < F > ( branch_fn : F ) -> Self
258+ where
259+ F : Fn ( BranchCbParam ) -> Option < usize > + Send + ' static ,
260+ {
261+ Self :: new ( |_| None , |_| None , branch_fn, |_| false )
262+ }
263+
264+ /// Creates a `Callbacks` instance with an application-defined [`policy_action_cb`](crate::FapiCallbacks::policy_action_cb) callback function.
265+ ///
266+ /// All other callback functions will use the default implementation.
267+ pub fn with_policy_action < F > ( policy_action_fn : F ) -> Self
268+ where
269+ F : Fn ( PolicyActionCbParam ) -> bool + Send + ' static ,
270+ {
271+ Self :: new ( |_| None , |_| None , |_| None , policy_action_fn)
272+ }
273+ }
274+
275+ impl FapiCallbacks for Callbacks {
276+ fn auth_cb ( & self , param : AuthCbParam ) -> Option < Cow < ' static , str > > {
277+ ( self . auth_fn ) ( param)
278+ }
279+
280+ fn sign_cb ( & self , param : SignCbParam ) -> Option < Vec < u8 > > {
281+ ( self . sign_fn ) ( param)
282+ }
283+
284+ fn branch_cb ( & self , param : BranchCbParam ) -> Option < usize > {
285+ ( self . branch_fn ) ( param)
286+ }
287+
288+ fn policy_action_cb ( & self , param : PolicyActionCbParam ) -> bool {
289+ ( self . policy_action_fn ) ( param)
290+ }
291+ }
292+
204293// ==========================================================================
205294// Callbacks manager
206295// ==========================================================================
207296
208- #[ derive( Debug ) ]
209297pub struct CallbackManager {
210298 inner : Box < dyn FapiCallbacks > ,
211299 auth_value : Option < CStringHolder > ,
@@ -283,6 +371,16 @@ impl CallbackManager {
283371 }
284372}
285373
374+ impl Debug for CallbackManager {
375+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
376+ f. debug_struct ( "CallbackManager" )
377+ . field ( "inner" , & self . inner . as_any ( ) . type_id ( ) )
378+ . field ( "auth_value" , & self . auth_value )
379+ . field ( "sign_data" , & self . sign_data )
380+ . finish ( )
381+ }
382+ }
383+
286384// ==========================================================================
287385// Unit tests
288386// ==========================================================================
0 commit comments