@reventlessdev/reventless-spec 3.0.0-alpha.89 → 3.0.0-alpha.90

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/CHANGELOG.md CHANGED
@@ -3,6 +3,13 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # 3.0.0-alpha.90 (2026-08-01)
7
+
8
+ ### Features
9
+
10
+ * **spec:** add DateRange semantic type ([d85b6cc](https://github.com/ReventlessDev/reventless-core/commit/d85b6cc18241644905241df2abd99949dd758059))
11
+
12
+
6
13
  # 3.0.0-alpha.89 (2026-07-31)
7
14
 
8
15
  ### Features
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reventlessdev/reventless-spec",
3
- "version": "3.0.0-alpha.89",
3
+ "version": "3.0.0-alpha.90",
4
4
  "description": "Specifications for Reventless",
5
5
  "license": "Apache-2.0",
6
6
  "bin": {
@@ -0,0 +1,148 @@
1
+ /**
2
+ A span of time: two ISO-8601 instants, a start and an end, as one value.
3
+
4
+ ## Why the pair is the value, not two fields beside each other
5
+
6
+ Before this type a span was a *guess*. A view that wanted a calendar, a timeline
7
+ or a gantt bar asked which fields were date-like and took the first named
8
+ `start…` and the first named `end…`, independently. Three things go wrong and
9
+ all three are silent: two intervals in one row mispair across each other (a bar
10
+ drawn from one interval's start to the other's end); an interval named anything
11
+ else — `checkIn`/`checkOut`, `from`/`to` — is invisible; and a lone `started…`
12
+ point pairs with whatever `end…` is nearby. Making the two instants one value
13
+ removes the pairing question: a consumer either holds the span or it does not.
14
+
15
+ The parts keep their own `dateTime` marker, so a walker that only understands
16
+ date-times still sees them, and the whole carries `dateRange` besides. That is
17
+ the same layering `Money` uses — the amount keeps being a number, the composite
18
+ adds the meaning the number cannot carry.
19
+
20
+ ## `[start, end)` — end exclusive
21
+
22
+ A range runs from `start` up to but not including `end`. `09:00–11:00` and
23
+ `11:00–13:00` are adjacent, not overlapping, and an all-day grid needs no
24
+ off-by-one-millisecond convention invented per consumer. This is a decision, not
25
+ a default: `overlaps` and `contains` are the only places it is written, and no
26
+ layout may re-decide it.
27
+
28
+ ## Why the ordering rule is not enforced at decode
29
+
30
+ `start <= end` relates two fields, so — unlike `Money`'s wholeness, which is a
31
+ property of one field and rides on that field's schema — it is a *record-level*
32
+ invariant. sury 11.0.0-alpha.4 miscompiles a refinement wrapping a record schema
33
+ (it hoists the result object above the field reads, so parse and serialize throw
34
+ `Cannot access 'v0' before initialization`), and the pin has not moved. So the
35
+ rule lives in `validate`/`make` and **the schema does not enforce it at decode**.
36
+
37
+ This is the first semantic type in this library whose invariant the boundary does
38
+ not check: `Money` rejects a fractional minor unit on the way in; `DateRange`
39
+ will accept a range that ends before it starts if one is ever written. A reader
40
+ who assumes parity with `Money` assumes wrong. When sury fixes the record
41
+ refinement the rule moves into the schema and `validate` stays as its single
42
+ definition — the relationship `Money.validateAmount` has with `amountSchema`.
43
+
44
+ Parsing is `Date.fromString` on each instant. A range whose strings do not parse
45
+ is a decode-time problem the `DateTime` marker does not currently catch either,
46
+ so there is no second validation layer here — a reversed *parseable* range is
47
+ what `validate` catches, and an unparseable one is out of both their scope.
48
+
49
+ ## How a field declares it
50
+
51
+ The field's declared type *is* `DateRange.t`, and sury-ppx resolves it to this
52
+ module's `schema`:
53
+
54
+ ```rescript
55
+ @schema type state = {
56
+ orderId: string,
57
+ deliveryWindow: option<Reventless.DateRange.t>,
58
+ }
59
+ ```
60
+
61
+ which serializes as `{"start": "2026-03-02T09:00:00Z", "end": "2026-03-02T11:00:00Z"}`.
62
+
63
+ **Introduced as a new optional field it is additive** — an absent optional
64
+ decodes to `None` for events written before it existed, so no upcaster and no
65
+ projection rebuild. It costs a log something only if it *collapses* an existing
66
+ `start*`/`end*` pair, which rewrites the wire shape the way `Money` rewrote
67
+ `price: float`. That collapse belongs to whoever builds the upcaster.
68
+ */
69
+
70
+ @schema
71
+ type t = {
72
+ /** The instant the range opens, inclusive. */
73
+ start: @s.matches(DateTime.string) string,
74
+ /** The instant the range closes, **exclusive** — the range does not contain
75
+ it. `@as("end")` puts `end` on the wire (where the UI's own `GanttChart`
76
+ already spells it that way); `end_` is the source spelling because `end`
77
+ is awkward as a bare ReScript field. */
78
+ @as("end") end_: @s.matches(DateTime.string) string,
79
+ }
80
+
81
+ /** The sury schema for a date-range field, carrying the `dateRange` semantic.
82
+
83
+ Shadows the schema sury-ppx derived from the type above: the derived one is
84
+ the shape, and this adds the marker the shape cannot carry. The ordering rule
85
+ is deliberately *not* refined in here — see the module doc. */
86
+ let schema: S.t<t> = schema->Semantic.mark(~id=Semantic.Id.dateRange)
87
+
88
+ /** An instant as milliseconds since the epoch — `NaN` if it does not parse. The
89
+ one place a range's strings become numbers, so end-exclusivity and the
90
+ ordering rule are all expressed against a single parse. */
91
+ let millis = (instant: string): float => instant->Date.fromString->Date.getTime
92
+
93
+ /**
94
+ Validate a range's ordering, saying why when it is reversed.
95
+
96
+ The single statement of the `start <= end` rule; `make` is derived from it, and
97
+ `schema` will be once sury's record refinement is fixed. An unparseable instant
98
+ is not caught here (see the module doc) — a reversed range means two instants
99
+ that both parse, the earlier one second.
100
+ */
101
+ let validate = (range: t): result<t, string> =>
102
+ millis(range.start) > millis(range.end_)
103
+ ? Error(
104
+ `a range ends before it starts: ${range.start} is after ${range.end_}. ` ++
105
+ `A range is [start, end) — the start is the earlier instant.`,
106
+ )
107
+ : Ok(range)
108
+
109
+ /** Build a validated range from its two instants. `end` is exclusive. */
110
+ let make = (~start: string, ~end_: string): result<t, string> => validate({start, end_})
111
+
112
+ /**
113
+ The range's length as a `Duration`, in whole seconds — the composite composing
114
+ with one of the branded scalars.
115
+
116
+ Total: a valid range has a non-negative length, and a zero-length range is
117
+ zero seconds. Truncated to whole seconds because that is what `Duration` is.
118
+ */
119
+ let duration = (range: t): Duration.t =>
120
+ Duration.unsafe(Math.trunc((millis(range.end_) -. millis(range.start)) /. 1000.0)->Float.toInt)
121
+
122
+ /**
123
+ Whether an instant falls within the range — at or after `start`, strictly before
124
+ `end`. End-exclusive, so the instant that opens the next adjacent range is *not*
125
+ contained by this one. One of the two places `[start, end)` is decided.
126
+ */
127
+ let contains = (range: t, instant: string): bool => {
128
+ let t = millis(instant)
129
+ t >= millis(range.start) && t < millis(range.end_)
130
+ }
131
+
132
+ /**
133
+ Whether two ranges share any instant. End-exclusive: `09:00–11:00` and
134
+ `11:00–13:00` are adjacent and do *not* overlap. The other place `[start, end)`
135
+ is decided — a layout that re-decides it will disagree with this.
136
+ */
137
+ let overlaps = (a: t, b: t): bool =>
138
+ millis(a.start) < millis(b.end_) && millis(b.start) < millis(a.end_)
139
+
140
+ /**
141
+ The range as text: the two instants with an en dash between them —
142
+ `"2026-03-02T09:00:00Z – 2026-03-02T11:00:00Z"`.
143
+
144
+ Locale-independent, matching the rest of the framework's formatters: the same
145
+ value reads the same in every log line and every test. A calendar-style
146
+ rendering is the presentation layer's job.
147
+ */
148
+ let format = (range: t): string => `${range.start} – ${range.end_}`
@@ -0,0 +1,74 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+
3
+ import * as S from "sury/src/S.res.mjs";
4
+ import * as DateTime$Reventless from "../types/DateTime.res.mjs";
5
+ import * as Semantic$Reventless from "./Semantic.res.mjs";
6
+
7
+ let schema = S.schema(s => ({
8
+ start: s.m(DateTime$Reventless.string),
9
+ end: s.m(DateTime$Reventless.string)
10
+ }));
11
+
12
+ let schema$1 = Semantic$Reventless.mark(schema, Semantic$Reventless.Id.dateRange, undefined);
13
+
14
+ function millis(instant) {
15
+ return new Date(instant).getTime();
16
+ }
17
+
18
+ function validate(range) {
19
+ if (new Date(range.start).getTime() > new Date(range.end).getTime()) {
20
+ return {
21
+ TAG: "Error",
22
+ _0: `a range ends before it starts: ` + range.start + ` is after ` + range.end + `. A range is [start, end) — the start is the earlier instant.`
23
+ };
24
+ } else {
25
+ return {
26
+ TAG: "Ok",
27
+ _0: range
28
+ };
29
+ }
30
+ }
31
+
32
+ function make(start, end_) {
33
+ return validate({
34
+ start: start,
35
+ end: end_
36
+ });
37
+ }
38
+
39
+ function duration(range) {
40
+ return Math.trunc((new Date(range.end).getTime() - new Date(range.start).getTime()) / 1000.0) | 0;
41
+ }
42
+
43
+ function contains(range, instant) {
44
+ let t = new Date(instant).getTime();
45
+ if (t >= new Date(range.start).getTime()) {
46
+ return t < new Date(range.end).getTime();
47
+ } else {
48
+ return false;
49
+ }
50
+ }
51
+
52
+ function overlaps(a, b) {
53
+ if (new Date(a.start).getTime() < new Date(b.end).getTime()) {
54
+ return new Date(b.start).getTime() < new Date(a.end).getTime();
55
+ } else {
56
+ return false;
57
+ }
58
+ }
59
+
60
+ function format(range) {
61
+ return range.start + ` – ` + range.end;
62
+ }
63
+
64
+ export {
65
+ schema$1 as schema,
66
+ millis,
67
+ validate,
68
+ make,
69
+ duration,
70
+ contains,
71
+ overlaps,
72
+ format,
73
+ }
74
+ /* schema Not a pure module */
@@ -63,6 +63,12 @@ module Id = {
63
63
  // this one changes a field's *shape* — a number becomes an object — so it is
64
64
  // a wire-breaking declaration rather than a refinement of one.
65
65
  let money = "money"
66
+
67
+ // The second composite. A pair of ISO-8601 instants as one value, replacing a
68
+ // span the UI used to guess from a `start*`/`end*` name pair. Like `money` it
69
+ // is an object on the wire; unlike it, adopting it as a *new* optional field
70
+ // is additive — an absent optional decodes to `None`.
71
+ let dateRange = "dateRange"
66
72
  }
67
73
 
68
74
  let semanticId: S.Metadata.Id.t<t> = S.Metadata.Id.make(~namespace="reventless", ~name="semantic")
@@ -13,7 +13,8 @@ let Id = {
13
13
  bytes: "bytes",
14
14
  duration: "duration",
15
15
  color: "color",
16
- money: "money"
16
+ money: "money",
17
+ dateRange: "dateRange"
17
18
  };
18
19
 
19
20
  let semanticId = S.Metadata.Id.make("reventless", "semantic");