@earnestjs/model 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/LICENSE.LESSER +165 -0
- package/README.md +198 -2
- package/dist/declarations.d.ts +151 -0
- package/dist/declarations.js +391 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +6 -0
- package/dist/m.d.ts +10 -0
- package/dist/m.js +3 -0
- package/dist/model.d.ts +164 -0
- package/dist/model.js +140 -0
- package/dist/replay.d.ts +35 -0
- package/dist/replay.js +97 -0
- package/dist/search.d.ts +121 -0
- package/dist/search.js +327 -0
- package/dist/values.d.ts +293 -0
- package/dist/values.js +684 -0
- package/package.json +51 -4
package/LICENSE.LESSER
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
CHANGED
|
@@ -1,3 +1,199 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @earnestjs/model
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The TypeScript library that models for the `earnest` model checker are written with. A model file
|
|
4
|
+
imports `m` from it and declares fields, actions, rules and ends. `earnest verify` reads the file
|
|
5
|
+
without running it; this library makes the same file type-check under strict `tsc`, with every
|
|
6
|
+
type inferred from the declarations, and run as ordinary code in Node.
|
|
7
|
+
|
|
8
|
+
The [Earnest documentation](https://earnest.cpeaustriajc.dev/reference/typescript/) (`apps/docs/src/content/docs/reference/`) is the contract: what a model file may contain, every primitive and its operations, and the JSON shapes used below.
|
|
9
|
+
|
|
10
|
+
## Installing
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install --save-dev @earnestjs/model
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
The published package holds compiled JavaScript and declarations in `dist/`, so it runs in Node
|
|
17
|
+
and type-checks under a strict `tsc` in a project that installs it. In this repository `exports`
|
|
18
|
+
points at `src/index.ts`, which Node runs by stripping types, and `publishConfig.exports` points
|
|
19
|
+
at `dist/` for the tarball that `bun pm pack` makes; `bun run build` emits it, and `prepack` runs
|
|
20
|
+
the build and swaps `publishConfig.exports` into `exports` for the tarball (`postpack` restores
|
|
21
|
+
`package.json`), since bun does not apply `publishConfig` itself. `test/installed.test.ts` packs the library, installs the tarball into a fresh project
|
|
22
|
+
and runs `explore`, `replay` and a strict type check there.
|
|
23
|
+
|
|
24
|
+
## Writing a model
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { m } from "@earnestjs/model";
|
|
28
|
+
|
|
29
|
+
export const Turnstile = m.model("Turnstile", {
|
|
30
|
+
gate: m.enum(["locked", "open"]).initial("locked"),
|
|
31
|
+
paid: m.boolean().initial(false),
|
|
32
|
+
visitors: m.int(0, 3).initial(0),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export const pay = m.action(Turnstile, "the visitor pays", {
|
|
36
|
+
when: (s) => !s.paid,
|
|
37
|
+
changes: () => ({ paid: true }),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
export const push = m.action(Turnstile, "the visitor pushes", {
|
|
41
|
+
when: (s) => s.paid && s.visitors < 3,
|
|
42
|
+
changes: (s) => ({ gate: "open", visitors: s.visitors + 1 }),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export const staysLocked = m.rule(
|
|
46
|
+
Turnstile,
|
|
47
|
+
"The gate stays locked until the visitor pays",
|
|
48
|
+
(s) => s.gate === "locked" || s.paid,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
export const full = m.end(Turnstile, "Three visitors went through", (s) => s.visitors === 3);
|
|
52
|
+
|
|
53
|
+
export type State = m.Infer<typeof Turnstile>;
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
- Nothing is annotated. `s` is the state type the fields declare, an action's second parameter is
|
|
57
|
+
typed from its `input`, and `changes` gives back only the fields that change. A wrong literal
|
|
58
|
+
(`gate: "ajar"`), a field the model does not have, or a value of the wrong kind in `changes` is
|
|
59
|
+
a compile error. In an action, `input`, `when` and `changes` may be written in any order.
|
|
60
|
+
- `m.action`, `m.rule`, `m.end` and `m.view` take the model as their first argument. A rule or an
|
|
61
|
+
end then has its sentence and a condition. An action has its sentence and an object with
|
|
62
|
+
`input`, `when` and `changes`. A view has an object.
|
|
63
|
+
- Declarations are plain values. `.optional()` and `.initial(value)` give back a new declaration,
|
|
64
|
+
so one `const Press = m.enum([...])` can be a field in two models and an input of an action.
|
|
65
|
+
- State is read-only, and every operation on a set, map, sequence, queue, lock, timer, request or
|
|
66
|
+
retry gives back a new value.
|
|
67
|
+
- `m.Infer<typeof Model>` is the state type, and `m.Infer<typeof Declaration>` the type of a
|
|
68
|
+
declaration's values. `m` is a module namespace (`export * as m`), so `m.Infer` is an ordinary
|
|
69
|
+
exported type and nothing in the library needs more than type stripping to run.
|
|
70
|
+
|
|
71
|
+
## The builders
|
|
72
|
+
|
|
73
|
+
| Builder | Values | Starts at, without `.initial` |
|
|
74
|
+
| --------------------------- | ------------------------------------------------ | ----------------------------- |
|
|
75
|
+
| `m.boolean()` | `false`, `true` | every value |
|
|
76
|
+
| `m.int(lo, hi)` | whole numbers from `lo` to `hi` | every value |
|
|
77
|
+
| `m.enum([...])` | the listed texts | every value |
|
|
78
|
+
| `m.oneOf([...])` | the listed texts, numbers or booleans | every value |
|
|
79
|
+
| `.optional()` | `null`, then the values of the declaration | every value |
|
|
80
|
+
| `m.record({...})` | an object with exactly those fields | every value |
|
|
81
|
+
| `m.set(T)` | `SetValue<T>` | the empty set |
|
|
82
|
+
| `m.map(K, V)` | `MapValue<K, V>` | the empty map |
|
|
83
|
+
| `m.seq(T, { max })` | `SeqValue<T>` of at most `max` items | the empty sequence |
|
|
84
|
+
| `m.queue(T, { capacity })` | `QueueValue<T>` of at most `capacity` items | the empty queue |
|
|
85
|
+
| `m.lock()`, `m.lock(Owner)` | `LockValue`, `OwnedLockValue<Owner>` | free |
|
|
86
|
+
| `m.timer()` | `TimerValue`: idle, running, fired | idle |
|
|
87
|
+
| `m.request()` | `RequestValue`: idle, pending, succeeded, failed | idle |
|
|
88
|
+
| `m.retry({ max })` | `RetryValue` from zero to `max` attempts | zero |
|
|
89
|
+
|
|
90
|
+
A declaration that makes no sense throws when it is written: bounds the wrong way round, a
|
|
91
|
+
value listed twice in `m.enum` or `m.oneOf`, an optional of an optional, and a lock with an
|
|
92
|
+
owner as a map value or a sequence or queue item, whose free state `null` could not be told from
|
|
93
|
+
a missing entry. `earnest verify` refuses the same.
|
|
94
|
+
|
|
95
|
+
`m.rem(a, b)` is the remainder of whole numbers: `%` is not allowed in a model file, and `m.rem` of a
|
|
96
|
+
negative `a` or a `b` below 1 is a fault that `explore` and `replay` report as `earnest verify`
|
|
97
|
+
does. `m.div(a, b)` is the whole number of times `b` goes into `a`, as Rust's `/` on integers
|
|
98
|
+
(`m.div(7, 2)` is 3): `/` is not allowed in a model file, since JavaScript's gives a fraction, and
|
|
99
|
+
`m.div` of a negative `a` or a `b` below 1 is the same kind of fault, "takes a division with a
|
|
100
|
+
negative number or a divisor below 1". `m.some(value)` is the value inside an optional: `%`'s sibling for `!`, and for `as`, `satisfies` and `<Type>value`, which a model
|
|
101
|
+
file may not write (only `as const` is read). `m.some(null)` is a fault that `explore` and `replay` report as `earnest
|
|
102
|
+
verify` does ("reads a value that is null"), where JavaScript would go on with `null` as 0, so
|
|
103
|
+
a number read from an optional goes through it. `m.setOf(a, b)`, `m.seqOf(a, b)` and `m.mapOf([k, v])` write collection values; a queue's
|
|
104
|
+
`.initial` takes `m.seqOf(...)`. `m.forall(T, f)` and `m.exists(T, f)` range over a declaration
|
|
105
|
+
or a collection. Every declaration has `values()`, which lists what it declares in the order the
|
|
106
|
+
checker tries them, `has(value)`, `starts()`, `count` and `fromJson(json)`.
|
|
107
|
+
|
|
108
|
+
## Running a model in Node
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
import { explore, replay } from "@earnestjs/model";
|
|
112
|
+
import * as turnstile from "./turnstile.model.ts";
|
|
113
|
+
|
|
114
|
+
const found = explore(turnstile.Turnstile, { exports: turnstile });
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`explore` searches breadth first and gives
|
|
118
|
+
`{ status, states, rules: [{ id, says, verdict, path }], findings: [{ kind, message, path }], reason }`
|
|
119
|
+
with the words and the path shape of `earnest verify --json`: `pass`, `fail` or `unfinished`;
|
|
120
|
+
`holds`, `broken` or `unknown`; `stuck`, `out-of-range` or `fault`. Each broken rule has the
|
|
121
|
+
shortest path to a state that breaks it. A rule is `unknown` when it was not seen broken but
|
|
122
|
+
the search is unfinished or the rule could not be evaluated in a state. `findings` has at most
|
|
123
|
+
one of each kind. `reason` is `null`, or why the search is unfinished.
|
|
124
|
+
|
|
125
|
+
- The id of an action, a rule or an end is the name it is exported under, and a running program
|
|
126
|
+
can't see that name, so pass the model file's exports as `exports`. A part that is not among
|
|
127
|
+
them has its sentence as its id.
|
|
128
|
+
- `maxStates` stops the search before it holds more distinct states than that; the default is
|
|
129
|
+
5,000,000. Every state that is counted has its rules judged, also the states the limit kept
|
|
130
|
+
the search from going on from, so a rule one of them breaks is `broken` with its path. No
|
|
131
|
+
action is tried from those states.
|
|
132
|
+
- The search follows the checker's order, so the two results can be compared: start states in
|
|
133
|
+
field order with the first field varying slowest, actions in declaration order, inputs with
|
|
134
|
+
the first input varying fastest, and states judged in the order they were found.
|
|
135
|
+
- Anything the model asks that can't be evaluated in a state is a `fault` there: a rule, the
|
|
136
|
+
condition or the change of an action, or the `when` of an end. A state is `stuck` only when the
|
|
137
|
+
condition of every action is false and every end is false, so a state where a condition or an end
|
|
138
|
+
faults is not stuck. An end is asked only in a state where no action can happen.
|
|
139
|
+
- A state that is out of range is counted, is not checked against the rules and is not searched
|
|
140
|
+
from.
|
|
141
|
+
|
|
142
|
+
`replay(Model, path, { exports })` applies a `{ start, steps }` path one action at a time and
|
|
143
|
+
gives every state on the way, the start state first, as objects in JSON form. It throws an error
|
|
144
|
+
that names the step when the path does not begin at a start state, a step names no action, an
|
|
145
|
+
input is missing or undeclared, the action's `when` is false, the action can't be evaluated, or
|
|
146
|
+
the state after the action differs from the step's `state`. Without `exports` a step is matched
|
|
147
|
+
by its `label`, the action's sentence.
|
|
148
|
+
|
|
149
|
+
## Where Node and the checker differ
|
|
150
|
+
|
|
151
|
+
- Arithmetic past 2^53 - 1 is a fault in the checker, "computes a number too large to hold",
|
|
152
|
+
wherever a model computes it. In Node the library sees only the numbers it is given back: a
|
|
153
|
+
next state that holds one is the same fault in the change of its action. A number computed
|
|
154
|
+
inside a rule, an end or a `when`, or by a helper on the way to a value that is not a number,
|
|
155
|
+
is the rounded value JavaScript computes, so `s.n + 9007199254740992 - 9007199254740992 ===
|
|
156
|
+
s.n` is judged by what JavaScript makes of it. `m.int` throws on a bound outside the safe
|
|
157
|
+
integers, and `m.oneOf` on a number outside them or one that is not whole, as the checker
|
|
158
|
+
refuses the same file.
|
|
159
|
+
- `===` on two records compares the objects in Node. Compare records field by field. Sets, maps,
|
|
160
|
+
sequences, queues, locks, timers, requests and retries are shared by content, so `===` on them
|
|
161
|
+
compares content.
|
|
162
|
+
- Set members and map keys are walked and printed in one fixed order, the checker's and the Rust
|
|
163
|
+
library's: `null`, booleans, numbers, text by code point (`compareText`, which puts a character
|
|
164
|
+
past U+FFFF after one from U+E000 to U+FFFF, where `<` does not), records field by field in the
|
|
165
|
+
order of the field names, collections item by item. A quantifier over a set stops at the first
|
|
166
|
+
item in that order. `replay` still accepts the members of a set in any order. See the
|
|
167
|
+
[primitives reference](https://earnest.cpeaustriajc.dev/reference/primitives/#order-of-sets-maps-and-records) for the rule for each kind.
|
|
168
|
+
- A fault is a `TypeError` thrown by the model, `m.rem` of a negative number, or `m.some(null)`:
|
|
169
|
+
reading a field of `null` or `m.some(null)` reports "reads a value that is null", any other
|
|
170
|
+
`TypeError` reports "uses a value of the wrong kind". Where a model file reads a number from
|
|
171
|
+
an optional it must use `m.some`, because JavaScript would give `null < 3` as true and `null +
|
|
172
|
+
1` as 1; the checker refuses the postfix `!` that would hide it.
|
|
173
|
+
- The library does not check that a file keeps to what a model file may contain. `earnest verify`
|
|
174
|
+
does.
|
|
175
|
+
|
|
176
|
+
## Development
|
|
177
|
+
|
|
178
|
+
From this folder, after `bun install --frozen-lockfile` at the repository root:
|
|
179
|
+
|
|
180
|
+
```sh
|
|
181
|
+
bun run typecheck # tsrs --noEmit, over src/, scripts/ and test/
|
|
182
|
+
bun run format # oxfmt --check
|
|
183
|
+
bun run format:fix # oxfmt, rewriting the files in place
|
|
184
|
+
bun run test # vitest run
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
- The package has no runtime dependencies and no build step. Node 22.18 or newer runs the `.ts`
|
|
188
|
+
files by stripping types, so the source uses only erasable syntax.
|
|
189
|
+
- The devDependencies are pinned to exact versions: `tsrs` (the Rust port of the TypeScript
|
|
190
|
+
checker), `oxfmt` (the formatter) and `vitest`, with `@types/node` for the tests. The lockfile
|
|
191
|
+
is `bun.lock` at the repository root.
|
|
192
|
+
- `typecheck` is one project: `tsconfig.json` covers `src/`, `scripts/` and `test/` and excludes
|
|
193
|
+
`test/rejected/`. It sets `types` to `node` so the tests can import `node:` modules.
|
|
194
|
+
- `test/types.ts` only has to compile: it holds the type-level proofs, such as what `m.Infer`
|
|
195
|
+
gives and which values `changes` rejects.
|
|
196
|
+
- `test/rejected/` holds one program for each mistake the compiler must catch, and `accepted.ts`,
|
|
197
|
+
which it must not complain about. `test/rejected.test.ts` runs `tsrs` on the folder and checks
|
|
198
|
+
that each bad program gets exactly one error, on the expected line. The folder is excluded from
|
|
199
|
+
the package's own `tsconfig.json`.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { type Json, LockValue, MapValue, OwnedLockValue, QueueValue, RequestValue, RetryValue, SeqValue, SetValue, TimerValue } from "./values.ts";
|
|
2
|
+
/** The most values `Declaration.values` will list before it throws instead. */
|
|
3
|
+
export declare const MOST_VALUES_LISTED = 1000000;
|
|
4
|
+
/**
|
|
5
|
+
* What one kind of declaration knows about its values. `rest` is where a field of this kind
|
|
6
|
+
* starts when it has no `.initial`; `null` means it starts at every value. `adopt` turns what
|
|
7
|
+
* `.initial` accepts into a value, and `decode` reads a value back from its JSON form, throwing
|
|
8
|
+
* when the JSON is of another kind.
|
|
9
|
+
*/
|
|
10
|
+
export interface Shape<T, In = T> {
|
|
11
|
+
readonly kind: string;
|
|
12
|
+
readonly count: number;
|
|
13
|
+
readonly rest: {
|
|
14
|
+
readonly value: T;
|
|
15
|
+
} | null;
|
|
16
|
+
values(): readonly T[];
|
|
17
|
+
has(value: unknown): value is T;
|
|
18
|
+
adopt(value: In): T;
|
|
19
|
+
decode(json: Json): unknown;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The finite set of values a field, an input or a collection member can take. Build one with
|
|
23
|
+
* `m.boolean()`, `m.int(lo, hi)` and the other builders; it is a plain value, so one declaration
|
|
24
|
+
* can be shared by several fields and models. `.optional()` and `.initial(value)` give back a
|
|
25
|
+
* new declaration and leave this one as it was.
|
|
26
|
+
*/
|
|
27
|
+
export declare class Declaration<T, In = T> {
|
|
28
|
+
#private;
|
|
29
|
+
/** Model files use the `m` builders instead of calling this. */
|
|
30
|
+
constructor(shape: Shape<T, In>, initial?: {
|
|
31
|
+
readonly value: T;
|
|
32
|
+
} | null);
|
|
33
|
+
/** What was declared, in words: `"enum"`, `"number"`, `"set"` and so on. */
|
|
34
|
+
get kind(): string;
|
|
35
|
+
/** How many values the declaration has. Very large counts are approximate. */
|
|
36
|
+
get count(): number;
|
|
37
|
+
/**
|
|
38
|
+
* Every value, in the order the checker tries them: `false` before `true`, numbers upward,
|
|
39
|
+
* enum values as listed, `null` before the values of an optional, a record's fields in the
|
|
40
|
+
* order declared with the first one changing slowest. Throws when there are more than
|
|
41
|
+
* `MOST_VALUES_LISTED`.
|
|
42
|
+
*/
|
|
43
|
+
values(): readonly T[];
|
|
44
|
+
/** Whether `value` is one of the declared values. Any value may be asked about. */
|
|
45
|
+
has(value: unknown): value is T;
|
|
46
|
+
/**
|
|
47
|
+
* The values a field declared this way starts at: the `.initial` value when there is one;
|
|
48
|
+
* otherwise the empty, free, idle or zero value of a collection, lock, timer, request or
|
|
49
|
+
* retry; otherwise every value.
|
|
50
|
+
*/
|
|
51
|
+
starts(): readonly T[];
|
|
52
|
+
/**
|
|
53
|
+
* Reads a value from the form `earnest verify --json` prints. The result may lie outside the
|
|
54
|
+
* declaration, as a value on an out-of-range path does, so check it with `has`. Throws when
|
|
55
|
+
* the JSON is of another kind altogether, such as text where a set is declared.
|
|
56
|
+
*/
|
|
57
|
+
fromJson(json: Json): unknown;
|
|
58
|
+
/** The same declaration, starting at `value`. Throws when `value` is not a declared value. */
|
|
59
|
+
initial(value: In): Declaration<T, In>;
|
|
60
|
+
/**
|
|
61
|
+
* The same declaration with `null` as one more value. Without `.initial` it then starts at
|
|
62
|
+
* every value, `null` included. Throws for a declaration that is already optional and for a
|
|
63
|
+
* lock with an owner, whose free state is already `null`.
|
|
64
|
+
*/
|
|
65
|
+
optional(): Declaration<T | null, In | null>;
|
|
66
|
+
}
|
|
67
|
+
/** The fields of a model or a record, or the inputs of an action: a declaration for each name. */
|
|
68
|
+
export type Fields = Readonly<Record<string, Declaration<unknown, never>>>;
|
|
69
|
+
/** The type of the values a declaration has. */
|
|
70
|
+
export type ValueOf<D> = D extends Declaration<infer T, infer _In> ? T : never;
|
|
71
|
+
/** The object type with one read-only property per declared name. */
|
|
72
|
+
export type StateOf<F extends Fields> = {
|
|
73
|
+
readonly [Name in keyof F]: ValueOf<F[Name]>;
|
|
74
|
+
};
|
|
75
|
+
/** Declares a field that is `true` or `false`. */
|
|
76
|
+
export declare const booleanOf: () => Declaration<boolean>;
|
|
77
|
+
/**
|
|
78
|
+
* Declares a whole number from `lo` to `hi`, both included. Arithmetic that leaves the range is
|
|
79
|
+
* not an error in the action: the checker reports the state as out of range. Throws when a bound
|
|
80
|
+
* is not a whole number or `lo` is above `hi`.
|
|
81
|
+
*/
|
|
82
|
+
export declare const int: (lo: number, hi: number) => Declaration<number>;
|
|
83
|
+
/** Declares a field that is one of the listed texts. Its type is the union of those literals. */
|
|
84
|
+
export declare const enumOf: <const T extends string>(options: readonly T[]) => Declaration<T>;
|
|
85
|
+
/**
|
|
86
|
+
* Declares a field that is one of the listed values, which are all text, all numbers or all
|
|
87
|
+
* booleans. Its type is the union of those literals.
|
|
88
|
+
*/
|
|
89
|
+
export declare const oneOf: <const T extends string | number | boolean>(options: readonly T[]) => Declaration<T>;
|
|
90
|
+
/**
|
|
91
|
+
* Declares a record: an object with exactly the given fields. Read a field with `r.field` and
|
|
92
|
+
* change one with `{ ...r, field: value }`. Compare records field by field: `===` on two records
|
|
93
|
+
* compares the objects, not their content.
|
|
94
|
+
*/
|
|
95
|
+
export declare const record: <F extends Fields>(fields: F) => Declaration<StateOf<F>>;
|
|
96
|
+
/** Declares a set of values of `item`. Without `.initial` it starts empty. */
|
|
97
|
+
export declare const set: <T>(item: Declaration<T, never>) => Declaration<SetValue<T>>;
|
|
98
|
+
/**
|
|
99
|
+
* Declares a map from values of `key` to values of `value`. Without `.initial` it starts empty.
|
|
100
|
+
* Throws when `value` is a lock with an owner.
|
|
101
|
+
*/
|
|
102
|
+
export declare const map: <K, V>(key: Declaration<K, never>, valueDeclared: Declaration<V, never>) => Declaration<MapValue<K, V>>;
|
|
103
|
+
/**
|
|
104
|
+
* Declares a sequence of at most `max` values of `item`. Without `.initial` it starts empty.
|
|
105
|
+
* Pushing past `max` is not an error in the action: the checker reports the state as out of
|
|
106
|
+
* range. Throws when `item` is a lock with an owner.
|
|
107
|
+
*/
|
|
108
|
+
export declare const seq: <T>(itemDeclared: Declaration<T, never>, limit: {
|
|
109
|
+
readonly max: number;
|
|
110
|
+
}) => Declaration<SeqValue<T>>;
|
|
111
|
+
/**
|
|
112
|
+
* Declares a first-in, first-out queue of at most `capacity` values of `item`. Without
|
|
113
|
+
* `.initial` it starts empty; `.initial(m.seqOf(a, b))` starts it with `a` at the head. Sending
|
|
114
|
+
* to a full queue is not an error in the action: the checker reports the state as out of range.
|
|
115
|
+
* Throws when `item` is a lock with an owner.
|
|
116
|
+
*/
|
|
117
|
+
export declare const queue: <T>(itemDeclared: Declaration<T, never>, limit: {
|
|
118
|
+
readonly capacity: number;
|
|
119
|
+
}) => Declaration<QueueValue<T>, QueueValue<T> | SeqValue<T>>;
|
|
120
|
+
/**
|
|
121
|
+
* Declares a lock. `m.lock()` is free or held; `m.lock(Owner)` also remembers which value of
|
|
122
|
+
* `Owner` holds it. Without `.initial` it starts free.
|
|
123
|
+
*/
|
|
124
|
+
export declare function lock(): Declaration<LockValue>;
|
|
125
|
+
export declare function lock<Owner>(owner: Declaration<Owner, never>): Declaration<OwnedLockValue<Owner>>;
|
|
126
|
+
/** Declares a timer that is idle, running or fired. Without `.initial` it starts idle. */
|
|
127
|
+
export declare const timer: () => Declaration<TimerValue>;
|
|
128
|
+
/**
|
|
129
|
+
* Declares an async request that is idle, pending, succeeded or failed. Without `.initial` it
|
|
130
|
+
* starts idle.
|
|
131
|
+
*/
|
|
132
|
+
export declare const request: () => Declaration<RequestValue>;
|
|
133
|
+
/**
|
|
134
|
+
* Declares a count of attempts from zero to `max`. Without `.initial` it starts at zero.
|
|
135
|
+
* Counting past `max` is not an error in the action: the checker reports the state as out of
|
|
136
|
+
* range, so guard `again()` with `canRetry`.
|
|
137
|
+
*/
|
|
138
|
+
export declare const retry: (limit: {
|
|
139
|
+
readonly max: number;
|
|
140
|
+
}) => Declaration<RetryValue>;
|
|
141
|
+
/**
|
|
142
|
+
* Whether `test` is true for every value of a declaration, or for every member of a set,
|
|
143
|
+
* sequence or queue. Over a map it is given `[key, value]` pairs. True when there is nothing to
|
|
144
|
+
* test.
|
|
145
|
+
*/
|
|
146
|
+
export declare const forall: <T>(over: Declaration<T, never> | Iterable<T>, test: (item: T) => boolean) => boolean;
|
|
147
|
+
/**
|
|
148
|
+
* Whether `test` is true for some value of a declaration, or for some member of a set, sequence
|
|
149
|
+
* or queue. Over a map it is given `[key, value]` pairs. False when there is nothing to test.
|
|
150
|
+
*/
|
|
151
|
+
export declare const exists: <T>(over: Declaration<T, never> | Iterable<T>, test: (item: T) => boolean) => boolean;
|