33 * Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
44 */
55
6- use crate :: { DescriptionValue , EvalFn , Identifiable , NumericalValue } ;
6+ use crate :: { AssumptionError , DescriptionValue , Identifiable , NumericalValue , PropagatingEffect } ;
77
88/// The Assumable trait defines the interface for objects that represent
99/// assumptions that can be tested and verified. Assumable types must also
@@ -25,10 +25,9 @@ use crate::{DescriptionValue, EvalFn, Identifiable, NumericalValue};
2525///
2626pub trait Assumable : Identifiable {
2727 fn description ( & self ) -> DescriptionValue ;
28- fn assumption_fn ( & self ) -> EvalFn ;
2928 fn assumption_tested ( & self ) -> bool ;
3029 fn assumption_valid ( & self ) -> bool ;
31- fn verify_assumption ( & self , data : & [ NumericalValue ] ) -> bool ;
30+ fn verify_assumption ( & self , data : & [ PropagatingEffect ] ) -> Result < bool , AssumptionError > ;
3231}
3332
3433/// The AssumableReasoning trait provides default implementations for common
@@ -119,10 +118,19 @@ where
119118 /// (from `number_assumption_valid()`) by the total number of assumptions
120119 /// (from `len()`) and multiplying by 100.
121120 ///
122- /// Returns the percentage as a NumericalValue.
121+ /// # Errors
123122 ///
124- fn percent_assumption_valid ( & self ) -> NumericalValue {
125- ( self . number_assumption_valid ( ) / self . len ( ) as NumericalValue ) * 100.0
123+ /// Returns `AssumptionError::EvaluationFailed` if the number of assumptions is zero,
124+ /// as percentage calculation would lead to a division by zero.
125+ ///
126+ fn percent_assumption_valid ( & self ) -> Result < NumericalValue , AssumptionError > {
127+ if self . is_empty ( ) {
128+ return Err ( AssumptionError :: EvaluationFailed (
129+ "Cannot calculate percentage with zero assumptions" . to_string ( ) ,
130+ ) ) ;
131+ }
132+ let percentage = ( self . number_assumption_valid ( ) / self . len ( ) as NumericalValue ) * 100.0 ;
133+ Ok ( percentage)
126134 }
127135
128136 /// Verifies all assumptions in the collection against the provided data.
@@ -133,10 +141,17 @@ where
133141 /// This will test each assumption against the data and update the
134142 /// `assumption_valid` and `assumption_tested` flags accordingly.
135143 ///
136- fn verify_all_assumptions ( & self , data : & [ NumericalValue ] ) {
144+ /// # Errors
145+ ///
146+ /// Returns an `AssumptionError` if any of the assumption functions fail during execution.
147+ ///
148+ fn verify_all_assumptions ( & self , data : & [ PropagatingEffect ] ) -> Result < ( ) , AssumptionError > {
137149 for a in self . get_all_items ( ) {
138- a. verify_assumption ( data) ;
150+ // We are interested in the side effect of updating the assumption state,
151+ // but we must handle the potential error.
152+ let _ = a. verify_assumption ( data) ?;
139153 }
154+ Ok ( ( ) )
140155 }
141156
142157 /// Returns a vector containing references to all invalid assumptions.
0 commit comments