]> src.bluestatic.org Git - ustaxlib.git/blob - src/core/Form.ts
Refine Trace to be able to output graphviz-compatible data.
[ustaxlib.git] / src / core / Form.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 Person from './Person';
7 import TaxReturn from './TaxReturn';
8 import Trace from './Trace';
9 import { Line } from './Line';
10 import { InconsistencyError, NotFoundError } from './Errors';
11
12 export default abstract class Form<L extends { [key: string]: Line<any> },
13 I = unknown> {
14 abstract readonly name: string;
15
16 protected abstract readonly _lines: L;
17
18 readonly supportsMultipleCopies: boolean = false;
19
20 private readonly _input?: I;
21
22 // Avoid using this; prefer the getLine() helpers declared below. This
23 // is only exposed for propagating line type information.
24 get lines(): L { return this._lines; }
25
26 constructor(input?: I) {
27 this._input = input;
28 }
29
30 init() {
31 for (const id in this._lines) {
32 let l = this._lines[id];
33 l._id = id;
34 l.form = this;
35 }
36 }
37
38 person(): Person | undefined {
39 return undefined;
40 }
41
42 getLine<K extends keyof L>(id: K): L[K] {
43 if (!(id in this._lines))
44 throw new NotFoundError(`Form ${this.name} does not have line ${id}`);
45 return this._lines[id];
46 }
47
48 getValue<K extends keyof L>(tr: TaxReturn, id: K): ReturnType<L[K]['value']> {
49 const line: L[K] = this.getLine(id);
50 return line.value(tr);
51 }
52
53 getInput<K extends keyof I>(name: K): I[K] {
54 if (!(name in this._input)) {
55 throw new NotFoundError(`No input with key ${name} on form ${this.name}`);
56 }
57 Trace.add(`${this.name} input: ${name}`);
58 return this._input[name];
59 }
60
61 hasInput<K extends keyof I>(name: K): boolean {
62 return this._input !== undefined && name in this._input;
63 }
64 };
65
66 export type FormClass<T extends Form<any>> = new (...args: any[]) => T;
67
68 export function isFormT<T extends Form<any>>(form: Form<any>,
69 formClass: FormClass<T>):
70 form is T {
71 return form.constructor === formClass;
72 }