From c422b53f160566e5ddb27fe193a07194448cb41f Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Sun, 1 Jun 2025 00:53:45 +1000 Subject: [PATCH] Pass step name and context to reporting step Preparation for plugins --- libdrcr/src/reporting/builders.rs | 4 +- libdrcr/src/reporting/calculator.rs | 4 +- libdrcr/src/reporting/steps.rs | 154 +++++++++++++++++++++++----- libdrcr/src/reporting/types.rs | 9 +- 4 files changed, 138 insertions(+), 33 deletions(-) diff --git a/libdrcr/src/reporting/builders.rs b/libdrcr/src/reporting/builders.rs index 8638fe8..1b1c572 100644 --- a/libdrcr/src/reporting/builders.rs +++ b/libdrcr/src/reporting/builders.rs @@ -260,7 +260,7 @@ impl GenerateBalances { } HasStepOrCanBuild::CanLookup(lookup_fn) => { // Check for () -> Transactions - let step = lookup_fn(args.clone()); + let step = lookup_fn(name, args.clone(), context); if step.requires(context).len() == 0 { return true; } @@ -287,7 +287,7 @@ impl GenerateBalances { } HasStepOrCanBuild::CanLookup(lookup_fn) => { // Check for () -> Transactions - let step = lookup_fn(args.clone()); + let step = lookup_fn(name, args.clone(), context); if step.requires(context).len() == 0 { return true; } diff --git a/libdrcr/src/reporting/calculator.rs b/libdrcr/src/reporting/calculator.rs index 4d1c87c..fe73e29 100644 --- a/libdrcr/src/reporting/calculator.rs +++ b/libdrcr/src/reporting/calculator.rs @@ -96,7 +96,7 @@ pub fn has_step_or_can_build<'a, 'b>( .find(|(name, kinds)| *name == product.name && kinds.contains(&product.kind)) { let (takes_args_fn, from_args_fn) = context.step_lookup_fn.get(lookup_key).unwrap(); - if takes_args_fn(&product.args) { + if takes_args_fn(&product.name, &product.args, context) { return HasStepOrCanBuild::CanLookup(*from_args_fn); } } @@ -133,7 +133,7 @@ fn build_step_for_product( panic!("Attempted to call build_step_for_product for already existing step") } HasStepOrCanBuild::CanLookup(from_args_fn) => { - new_step = from_args_fn(product.args.clone()); + new_step = from_args_fn(&product.name, product.args.clone(), context); // Check new step meets the dependency if new_step.id().name != product.name { diff --git a/libdrcr/src/reporting/steps.rs b/libdrcr/src/reporting/steps.rs index 9a28cb0..347936b 100644 --- a/libdrcr/src/reporting/steps.rs +++ b/libdrcr/src/reporting/steps.rs @@ -79,11 +79,19 @@ impl AllTransactionsExceptEarningsToEquity { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(AllTransactionsExceptEarningsToEquity { args: *args.downcast().unwrap(), }) @@ -143,18 +151,22 @@ impl AllTransactionsExceptEarningsToEquityBalances { "AllTransactionsExceptEarningsToEquity".to_string(), vec![ReportingProductKind::BalancesAt], Self::takes_args, - |a| Self::from_args(ReportingProductKind::BalancesAt, a), + |_name, args, _ctx| Self::from_args(ReportingProductKind::BalancesAt, args), ); context.register_lookup_fn( "AllTransactionsExceptEarningsToEquity".to_string(), vec![ReportingProductKind::BalancesBetween], Self::takes_args, - |a| Self::from_args(ReportingProductKind::BalancesBetween, a), + |_name, args, _ctx| Self::from_args(ReportingProductKind::BalancesBetween, args), ); } - fn takes_args(_args: &Box) -> bool { + fn takes_args( + _name: &str, + _args: &Box, + _context: &ReportingContext, + ) -> bool { true } @@ -249,11 +261,19 @@ impl AllTransactionsIncludingEarningsToEquity { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(AllTransactionsIncludingEarningsToEquity { args: *args.downcast().unwrap(), }) @@ -381,11 +401,19 @@ impl BalanceSheet { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(BalanceSheet { args: *args.downcast().unwrap(), }) @@ -555,11 +583,19 @@ impl CombineOrdinaryTransactions { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(CombineOrdinaryTransactions { args: *args.downcast().unwrap(), }) @@ -628,11 +664,19 @@ impl CombineOrdinaryTransactionsBalances { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(CombineOrdinaryTransactionsBalances { args: *args.downcast().unwrap(), }) @@ -729,11 +773,19 @@ impl CurrentYearEarningsToEquity { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(CurrentYearEarningsToEquity { args: *args.downcast().unwrap(), }) @@ -866,11 +918,19 @@ impl DBBalances { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(DBBalances { args: *args.downcast().unwrap(), }) @@ -933,11 +993,19 @@ impl DBTransactions { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(_args: Box) -> Box { + fn from_args( + _name: &str, + _args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(DBTransactions {}) } } @@ -1000,11 +1068,19 @@ impl IncomeStatement { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(IncomeStatement { args: *args.downcast().unwrap(), }) @@ -1171,11 +1247,19 @@ impl PostUnreconciledStatementLines { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(_args: Box) -> Box { + fn from_args( + _name: &str, + _args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(PostUnreconciledStatementLines {}) } } @@ -1278,11 +1362,19 @@ impl RetainedEarningsToEquity { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(RetainedEarningsToEquity { args: *args.downcast().unwrap(), }) @@ -1418,11 +1510,19 @@ impl TrialBalance { ); } - fn takes_args(args: &Box) -> bool { + fn takes_args( + _name: &str, + args: &Box, + _context: &ReportingContext, + ) -> bool { args.is::() } - fn from_args(args: Box) -> Box { + fn from_args( + _name: &str, + args: Box, + _context: &ReportingContext, + ) -> Box { Box::new(TrialBalance { args: *args.downcast().unwrap(), }) diff --git a/libdrcr/src/reporting/types.rs b/libdrcr/src/reporting/types.rs index 07e3936..d1319e0 100644 --- a/libdrcr/src/reporting/types.rs +++ b/libdrcr/src/reporting/types.rs @@ -102,12 +102,17 @@ impl ReportingContext { /// Function which determines whether the [ReportingStepArgs] are valid arguments for a given [ReportingStep] /// /// See [ReportingContext::register_lookup_fn]. -pub type ReportingStepTakesArgsFn = fn(args: &Box) -> bool; +pub type ReportingStepTakesArgsFn = + fn(name: &str, args: &Box, context: &ReportingContext) -> bool; /// Function which builds a concrete [ReportingStep] from the given [ReportingStepArgs] /// /// See [ReportingContext::register_lookup_fn]. -pub type ReportingStepFromArgsFn = fn(args: Box) -> Box; +pub type ReportingStepFromArgsFn = fn( + name: &str, + args: Box, + context: &ReportingContext, +) -> Box; // ------------------------------- // REPORTING STEP DYNAMIC BUILDERS