• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { Calendar } from "./Calendar.mjs"
3import { CalendarError } from "./CalendarError.mjs"
4import { CalendarParseError } from "./CalendarParseError.mjs"
5import { Date } from "./Date.mjs"
6import { WeekCalculator } from "./WeekCalculator.mjs"
7import { WeekOf } from "./WeekOf.mjs"
8import { Weekday } from "./Weekday.mjs"
9import wasm from "./diplomat-wasm.mjs";
10import * as diplomatRuntime from "./diplomat-runtime.mjs";
11
12
13/** An ICU4X Date object capable of containing a ISO-8601 date
14*
15*See the [Rust documentation for `Date`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html) for more information.
16*/
17const IsoDate_box_destroy_registry = new FinalizationRegistry((ptr) => {
18    wasm.icu4x_IsoDate_destroy_mv1(ptr);
19});
20
21export class IsoDate {
22
23    // Internal ptr reference:
24    #ptr = null;
25
26    // Lifetimes are only to keep dependencies alive.
27    // Since JS won't garbage collect until there are no incoming edges.
28    #selfEdge = [];
29
30    #internalConstructor(symbol, ptr, selfEdge) {
31        if (symbol !== diplomatRuntime.internalConstructor) {
32            console.error("IsoDate is an Opaque type. You cannot call its constructor.");
33            return;
34        }
35
36        this.#ptr = ptr;
37        this.#selfEdge = selfEdge;
38
39        // Are we being borrowed? If not, we can register.
40        if (this.#selfEdge.length === 0) {
41            IsoDate_box_destroy_registry.register(this, this.#ptr);
42        }
43
44        return this;
45    }
46    get ffiValue() {
47        return this.#ptr;
48    }
49
50    #defaultConstructor(year, month, day) {
51        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
52
53        const result = wasm.icu4x_IsoDate_create_mv1(diplomatReceive.buffer, year, month, day);
54
55        try {
56            if (!diplomatReceive.resultFlag) {
57                const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
58                throw new globalThis.Error('CalendarError: ' + cause.value, { cause });
59            }
60            return new IsoDate(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
61        }
62
63        finally {
64            diplomatReceive.free();
65        }
66    }
67
68    static fromString(v) {
69        let functionCleanupArena = new diplomatRuntime.CleanupArena();
70
71        const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, v));
72
73        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
74
75        const result = wasm.icu4x_IsoDate_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat());
76
77        try {
78            if (!diplomatReceive.resultFlag) {
79                const cause = new CalendarParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
80                throw new globalThis.Error('CalendarParseError: ' + cause.value, { cause });
81            }
82            return new IsoDate(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
83        }
84
85        finally {
86            functionCleanupArena.free();
87
88            diplomatReceive.free();
89        }
90    }
91
92    toCalendar(calendar) {
93        const result = wasm.icu4x_IsoDate_to_calendar_mv1(this.ffiValue, calendar.ffiValue);
94
95        try {
96            return new Date(diplomatRuntime.internalConstructor, result, []);
97        }
98
99        finally {}
100    }
101
102    toAny() {
103        const result = wasm.icu4x_IsoDate_to_any_mv1(this.ffiValue);
104
105        try {
106            return new Date(diplomatRuntime.internalConstructor, result, []);
107        }
108
109        finally {}
110    }
111
112    get dayOfYear() {
113        const result = wasm.icu4x_IsoDate_day_of_year_mv1(this.ffiValue);
114
115        try {
116            return result;
117        }
118
119        finally {}
120    }
121
122    get dayOfMonth() {
123        const result = wasm.icu4x_IsoDate_day_of_month_mv1(this.ffiValue);
124
125        try {
126            return result;
127        }
128
129        finally {}
130    }
131
132    get dayOfWeek() {
133        const result = wasm.icu4x_IsoDate_day_of_week_mv1(this.ffiValue);
134
135        try {
136            return new Weekday(diplomatRuntime.internalConstructor, result);
137        }
138
139        finally {}
140    }
141
142    weekOfMonth(firstWeekday) {
143        const result = wasm.icu4x_IsoDate_week_of_month_mv1(this.ffiValue, firstWeekday.ffiValue);
144
145        try {
146            return result;
147        }
148
149        finally {}
150    }
151
152    weekOfYear(calculator) {
153        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 8, 4, false);
154
155        const result = wasm.icu4x_IsoDate_week_of_year_mv1(diplomatReceive.buffer, this.ffiValue, calculator.ffiValue);
156
157        try {
158            return WeekOf._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
159        }
160
161        finally {
162            diplomatReceive.free();
163        }
164    }
165
166    get month() {
167        const result = wasm.icu4x_IsoDate_month_mv1(this.ffiValue);
168
169        try {
170            return result;
171        }
172
173        finally {}
174    }
175
176    get year() {
177        const result = wasm.icu4x_IsoDate_year_mv1(this.ffiValue);
178
179        try {
180            return result;
181        }
182
183        finally {}
184    }
185
186    get isInLeapYear() {
187        const result = wasm.icu4x_IsoDate_is_in_leap_year_mv1(this.ffiValue);
188
189        try {
190            return result;
191        }
192
193        finally {}
194    }
195
196    get monthsInYear() {
197        const result = wasm.icu4x_IsoDate_months_in_year_mv1(this.ffiValue);
198
199        try {
200            return result;
201        }
202
203        finally {}
204    }
205
206    get daysInMonth() {
207        const result = wasm.icu4x_IsoDate_days_in_month_mv1(this.ffiValue);
208
209        try {
210            return result;
211        }
212
213        finally {}
214    }
215
216    get daysInYear() {
217        const result = wasm.icu4x_IsoDate_days_in_year_mv1(this.ffiValue);
218
219        try {
220            return result;
221        }
222
223        finally {}
224    }
225
226    constructor(year, month, day) {
227        if (arguments[0] === diplomatRuntime.exposeConstructor) {
228            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
229        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
230            return this.#internalConstructor(...arguments);
231        } else {
232            return this.#defaultConstructor(...arguments);
233        }
234    }
235}