Refactor register_lookup_fns and register_dynamic_builders for readability

This commit is contained in:
RunasSudo 2025-05-21 20:22:06 +10:00
parent db89814498
commit 71c3629898
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 116 additions and 85 deletions

View File

@ -24,31 +24,14 @@ use super::types::{
ReportingStep, ReportingStepArgs, ReportingStepDynamicBuilder, ReportingStepId, ReportingStep, ReportingStepArgs, ReportingStepDynamicBuilder, ReportingStepId,
}; };
/// Call [ReportingContext::register_dynamic_builder] for all dynamic builders provided by this module
pub fn register_dynamic_builders(context: &mut ReportingContext) { pub fn register_dynamic_builders(context: &mut ReportingContext) {
context.register_dynamic_builder(ReportingStepDynamicBuilder { GenerateBalances::register_dynamic_builder(context);
name: "GenerateBalances", UpdateBalancesBetween::register_dynamic_builder(context);
can_build: GenerateBalances::can_build, UpdateBalancesAt::register_dynamic_builder(context);
build: GenerateBalances::build,
});
context.register_dynamic_builder(ReportingStepDynamicBuilder { // This is the least efficient way of generating BalancesBetween so put at the end
name: "UpdateBalancesBetween", BalancesAtToBalancesBetween::register_dynamic_builder(context);
can_build: UpdateBalancesBetween::can_build,
build: UpdateBalancesBetween::build,
});
context.register_dynamic_builder(ReportingStepDynamicBuilder {
name: "UpdateBalancesAt",
can_build: UpdateBalancesAt::can_build,
build: UpdateBalancesAt::build,
});
// This is the least efficient way of generating BalancesBetween
context.register_dynamic_builder(ReportingStepDynamicBuilder {
name: "BalancesAtToBalancesBetween",
can_build: BalancesAtToBalancesBetween::can_build,
build: BalancesAtToBalancesBetween::build,
});
} }
#[derive(Debug)] #[derive(Debug)]
@ -60,6 +43,14 @@ pub struct BalancesAtToBalancesBetween {
impl BalancesAtToBalancesBetween { impl BalancesAtToBalancesBetween {
// Implements BalancesAt, BalancesAt -> BalancesBetween // Implements BalancesAt, BalancesAt -> BalancesBetween
fn register_dynamic_builder(context: &mut ReportingContext) {
context.register_dynamic_builder(ReportingStepDynamicBuilder {
name: "BalancesAtToBalancesBetween",
can_build: Self::can_build,
build: Self::build,
});
}
fn can_build( fn can_build(
name: &'static str, name: &'static str,
kind: ReportingProductKind, kind: ReportingProductKind,
@ -162,6 +153,14 @@ pub struct GenerateBalances {
impl GenerateBalances { impl GenerateBalances {
// Implements (() -> Transactions) -> BalancesAt // Implements (() -> Transactions) -> BalancesAt
fn register_dynamic_builder(context: &mut ReportingContext) {
context.register_dynamic_builder(ReportingStepDynamicBuilder {
name: "GenerateBalances",
can_build: Self::can_build,
build: Self::build,
});
}
fn can_build( fn can_build(
name: &'static str, name: &'static str,
kind: ReportingProductKind, kind: ReportingProductKind,
@ -250,6 +249,14 @@ pub struct UpdateBalancesAt {
impl UpdateBalancesAt { impl UpdateBalancesAt {
// Implements (BalancesAt -> Transactions) -> BalancesAt // Implements (BalancesAt -> Transactions) -> BalancesAt
fn register_dynamic_builder(context: &mut ReportingContext) {
context.register_dynamic_builder(ReportingStepDynamicBuilder {
name: "UpdateBalancesAt",
can_build: Self::can_build,
build: Self::build,
});
}
fn can_build( fn can_build(
name: &'static str, name: &'static str,
kind: ReportingProductKind, kind: ReportingProductKind,
@ -378,6 +385,14 @@ pub struct UpdateBalancesBetween {
impl UpdateBalancesBetween { impl UpdateBalancesBetween {
// Implements (BalancesBetween -> Transactions) -> BalancesBetween // Implements (BalancesBetween -> Transactions) -> BalancesBetween
fn register_dynamic_builder(context: &mut ReportingContext) {
context.register_dynamic_builder(ReportingStepDynamicBuilder {
name: "UpdateBalancesBetween",
can_build: Self::can_build,
build: Self::build,
});
}
fn can_build( fn can_build(
name: &'static str, name: &'static str,
kind: ReportingProductKind, kind: ReportingProductKind,

View File

@ -33,69 +33,15 @@ use super::types::{
ReportingStepArgs, ReportingStepId, VoidArgs, ReportingStepArgs, ReportingStepId, VoidArgs,
}; };
/// Call [ReportingContext::register_lookup_fn] for all steps provided by this module
pub fn register_lookup_fns(context: &mut ReportingContext) { pub fn register_lookup_fns(context: &mut ReportingContext) {
context.register_lookup_fn( AllTransactionsExceptRetainedEarnings::register_lookup_fn(context);
"AllTransactionsExceptRetainedEarnings", AllTransactionsIncludingRetainedEarnings::register_lookup_fn(context);
&[ReportingProductKind::BalancesAt], CalculateIncomeTax::register_lookup_fn(context);
AllTransactionsExceptRetainedEarnings::takes_args, CombineOrdinaryTransactions::register_lookup_fn(context);
|a| { DBBalances::register_lookup_fn(context);
AllTransactionsExceptRetainedEarnings::from_args(&[ReportingProductKind::BalancesAt], a) PostUnreconciledStatementLines::register_lookup_fn(context);
}, RetainedEarningsToEquity::register_lookup_fn(context);
);
context.register_lookup_fn(
"AllTransactionsExceptRetainedEarnings",
&[ReportingProductKind::BalancesBetween],
AllTransactionsExceptRetainedEarnings::takes_args,
|a| {
AllTransactionsExceptRetainedEarnings::from_args(
&[ReportingProductKind::BalancesBetween],
a,
)
},
);
context.register_lookup_fn(
"AllTransactionsIncludingRetainedEarnings",
&[ReportingProductKind::BalancesAt],
AllTransactionsIncludingRetainedEarnings::takes_args,
AllTransactionsIncludingRetainedEarnings::from_args,
);
context.register_lookup_fn(
"CalculateIncomeTax",
&[ReportingProductKind::Transactions],
CalculateIncomeTax::takes_args,
CalculateIncomeTax::from_args,
);
context.register_lookup_fn(
"CombineOrdinaryTransactions",
&[ReportingProductKind::BalancesAt],
CombineOrdinaryTransactions::takes_args,
CombineOrdinaryTransactions::from_args,
);
context.register_lookup_fn(
"DBBalances",
&[ReportingProductKind::BalancesAt],
DBBalances::takes_args,
DBBalances::from_args,
);
context.register_lookup_fn(
"PostUnreconciledStatementLines",
&[ReportingProductKind::Transactions],
PostUnreconciledStatementLines::takes_args,
PostUnreconciledStatementLines::from_args,
);
context.register_lookup_fn(
"RetainedEarningsToEquity",
&[ReportingProductKind::Transactions],
RetainedEarningsToEquity::takes_args,
RetainedEarningsToEquity::from_args,
);
} }
#[derive(Debug)] #[derive(Debug)]
@ -105,6 +51,22 @@ pub struct AllTransactionsExceptRetainedEarnings {
} }
impl AllTransactionsExceptRetainedEarnings { impl AllTransactionsExceptRetainedEarnings {
fn register_lookup_fn(context: &mut ReportingContext) {
context.register_lookup_fn(
"AllTransactionsExceptRetainedEarnings",
&[ReportingProductKind::BalancesAt],
Self::takes_args,
|a| Self::from_args(&[ReportingProductKind::BalancesAt], a),
);
context.register_lookup_fn(
"AllTransactionsExceptRetainedEarnings",
&[ReportingProductKind::BalancesBetween],
Self::takes_args,
|a| Self::from_args(&[ReportingProductKind::BalancesBetween], a),
);
}
fn takes_args(_args: &Box<dyn ReportingStepArgs>) -> bool { fn takes_args(_args: &Box<dyn ReportingStepArgs>) -> bool {
true true
} }
@ -142,6 +104,15 @@ pub struct AllTransactionsIncludingRetainedEarnings {
} }
impl AllTransactionsIncludingRetainedEarnings { impl AllTransactionsIncludingRetainedEarnings {
fn register_lookup_fn(context: &mut ReportingContext) {
context.register_lookup_fn(
"AllTransactionsIncludingRetainedEarnings",
&[ReportingProductKind::BalancesAt],
Self::takes_args,
Self::from_args,
);
}
fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool { fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool {
args.is::<DateArgs>() args.is::<DateArgs>()
} }
@ -190,6 +161,15 @@ impl ReportingStep for AllTransactionsIncludingRetainedEarnings {
pub struct CalculateIncomeTax {} pub struct CalculateIncomeTax {}
impl CalculateIncomeTax { impl CalculateIncomeTax {
fn register_lookup_fn(context: &mut ReportingContext) {
context.register_lookup_fn(
"CalculateIncomeTax",
&[ReportingProductKind::Transactions],
Self::takes_args,
Self::from_args,
);
}
fn takes_args(_args: &Box<dyn ReportingStepArgs>) -> bool { fn takes_args(_args: &Box<dyn ReportingStepArgs>) -> bool {
true true
} }
@ -254,6 +234,15 @@ pub struct CombineOrdinaryTransactions {
} }
impl CombineOrdinaryTransactions { impl CombineOrdinaryTransactions {
fn register_lookup_fn(context: &mut ReportingContext) {
context.register_lookup_fn(
"CombineOrdinaryTransactions",
&[ReportingProductKind::BalancesAt],
Self::takes_args,
Self::from_args,
);
}
fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool { fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool {
args.is::<DateArgs>() args.is::<DateArgs>()
} }
@ -304,6 +293,15 @@ pub struct DBBalances {
} }
impl DBBalances { impl DBBalances {
fn register_lookup_fn(context: &mut ReportingContext) {
context.register_lookup_fn(
"DBBalances",
&[ReportingProductKind::BalancesAt],
Self::takes_args,
Self::from_args,
);
}
fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool { fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool {
args.is::<DateArgs>() args.is::<DateArgs>()
} }
@ -360,6 +358,15 @@ pub struct PostUnreconciledStatementLines {
} }
impl PostUnreconciledStatementLines { impl PostUnreconciledStatementLines {
fn register_lookup_fn(context: &mut ReportingContext) {
context.register_lookup_fn(
"PostUnreconciledStatementLines",
&[ReportingProductKind::Transactions],
Self::takes_args,
Self::from_args,
);
}
fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool { fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool {
args.is::<DateArgs>() args.is::<DateArgs>()
} }
@ -393,6 +400,15 @@ pub struct RetainedEarningsToEquity {
} }
impl RetainedEarningsToEquity { impl RetainedEarningsToEquity {
fn register_lookup_fn(context: &mut ReportingContext) {
context.register_lookup_fn(
"RetainedEarningsToEquity",
&[ReportingProductKind::Transactions],
Self::takes_args,
Self::from_args,
);
}
fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool { fn takes_args(args: &Box<dyn ReportingStepArgs>) -> bool {
args.is::<DateArgs>() args.is::<DateArgs>()
} }