]> src.bluestatic.org Git - ustaxlib.git/blob - src/core/Line.ts
Remove the need to self-reference Form['lines'] in Form subclasses.
[ustaxlib.git] / src / core / Line.ts
1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
5
6 import TaxReturn from './TaxReturn';
7 import * as Trace from './Trace';
8 import Form, { FormClass } from './Form';
9
10 export abstract class Line<T> {
11 private _description?: string;
12
13 _id: string; // _id is set by Form.init().
14 form: Form; // Set by Form.init();
15
16 constructor(description?: string) {
17 this._description = description;
18 }
19
20 get id(): string {
21 return this._id;
22 }
23
24 get description(): string {
25 return this._description;
26 }
27
28 abstract value(tr: TaxReturn): T;
29 };
30
31 type ComputeFunc<T> = (tr: TaxReturn) => T;
32
33 export class ComputedLine<T> extends Line<T> {
34 private _compute: ComputeFunc<T>;
35
36 constructor(compute: ComputeFunc<T>, description?: string) {
37 super(description);
38 this._compute = compute;
39 }
40
41 value(tr: TaxReturn): T {
42 Trace.begin(this);
43 const value = this._compute(tr);
44 Trace.end();
45 return value;
46 }
47 };
48
49 export class ReferenceLine<F extends Form,
50 L extends keyof F['lines'],
51 T extends ReturnType<F['lines'][L]['value']>>
52 extends Line<T> {
53 private _form: FormClass<F>;
54 private _line: L;
55 private _fallback?: T;
56
57 // If creating a ReferenceLine and F is the same class as the
58 // the one the Line is in, erase |form|'s type with |as any| to
59 // keep TypeScript happy.
60 constructor(form: FormClass<F>, line: L, description?: string, fallback?: T) {
61 super(description || `Reference ${form.name}@${line}`);
62 this._form = form;
63 this._line = line;
64 this._fallback = fallback;
65 }
66
67 value(tr: TaxReturn): T {
68 Trace.begin(this);
69 const form: F = tr.findForm(this._form);
70 if (this._fallback !== undefined && !form) {
71 Trace.end();
72 return this._fallback;
73 }
74 const value: T = form.getValue(tr, this._line);
75 Trace.end();
76 return value;
77 }
78 };
79
80 export class InputLine<U = unknown, T extends keyof U = any> extends Line<U[T]> {
81 private _input: T;
82 private _fallback: U[T];
83
84 form: Form<U>;
85
86 constructor(input: T, description?: string, fallback?: U[T]) {
87 super(description || `Input from ${input}`);
88 this._input = input;
89 this._fallback = fallback;
90 }
91
92 value(tr: TaxReturn): U[T] {
93 Trace.begin(this);
94 if (!this.form.hasInput(this._input) && this._fallback !== undefined) {
95 Trace.end();
96 return this._fallback;
97 }
98 const value = this.form.getInput<T>(this._input);
99 Trace.end();
100 return value;
101 }
102 };
103
104 export class AccumulatorLine<F extends Form,
105 L extends keyof F['lines']>
106 extends Line<number> {
107 private _form: FormClass<F>;
108 private _line: L;
109
110 constructor(form: FormClass<F>, line: L, description?: string) {
111 super(description || `Accumulator ${form.name}@${line}`);
112 this._form = form;
113 this._line = line;
114 }
115
116 value(tr): number {
117 Trace.begin(this);
118 const forms: F[] = tr.findForms(this._form);
119 const value = sumLineOfForms(tr, forms, this._line);
120 Trace.end();
121 return value;
122 }
123 };
124
125 export class UnsupportedLine extends Line<number> {
126 constructor(description?: string) {
127 super(description || 'Unsupported');
128 }
129
130 value(tr): number {
131 // Unsupported lines are deliberately omitted from Trace.
132 return 0;
133 }
134 };
135
136 export function sumLineOfForms<F extends Form, L extends keyof F['lines']>(
137 tr: TaxReturn, forms: F[], line: L): number {
138 const reducer = (acc: number, curr: F) => acc + curr.getValue(tr, line);
139 return forms.reduce(reducer, 0);
140 }
141
142 export function sumFormLines<F extends Form, L extends keyof F['lines']>(
143 tr: TaxReturn, form: F, lines: L[]): number {
144 let value = 0;
145 for (const line of lines)
146 value += form.getValue(tr, line);
147 return value;
148 }