• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { Calendar } from "./Calendar.mjs"
3import { CalendarParseError } from "./CalendarParseError.mjs"
4import { Date } from "./Date.mjs"
5import { Time } from "./Time.mjs"
6import wasm from "./diplomat-wasm.mjs";
7import * as diplomatRuntime from "./diplomat-runtime.mjs";
8
9
10/** An ICU4X DateTime object capable of containing a date and time for any calendar.
11*
12*See the [Rust documentation for `DateTime`](https://docs.rs/icu/latest/icu/time/struct.DateTime.html) for more information.
13*/
14
15
16export class DateTime {
17
18    #date;
19
20    get date()  {
21        return this.#date;
22    }
23
24    #time;
25
26    get time()  {
27        return this.#time;
28    }
29
30    #internalConstructor(structObj, internalConstructor) {
31        if (typeof structObj !== "object") {
32            throw new Error("DateTime's constructor takes an object of DateTime's fields.");
33        }
34
35        if (internalConstructor !== diplomatRuntime.internalConstructor) {
36            throw new Error("DateTime is an out struct and can only be created internally.");
37        }
38        if ("date" in structObj) {
39            this.#date = structObj.date;
40        } else {
41            throw new Error("Missing required field date.");
42        }
43
44        if ("time" in structObj) {
45            this.#time = structObj.time;
46        } else {
47            throw new Error("Missing required field time.");
48        }
49
50        return this;
51    }
52
53    // Return this struct in FFI function friendly format.
54    // Returns an array that can be expanded with spread syntax (...)
55
56    _intoFFI(
57        functionCleanupArena,
58        appendArrayMap
59    ) {
60        return [this.#date.ffiValue, this.#time.ffiValue]
61    }
62
63    static _fromSuppliedValue(internalConstructor, obj) {
64        if (internalConstructor !== diplomatRuntime.internalConstructor) {
65            throw new Error("_fromSuppliedValue cannot be called externally.");
66        }
67
68        if (obj instanceof DateTime) {
69            return obj;
70        }
71
72        return DateTime.fromFields(obj);
73    }
74
75    _writeToArrayBuffer(
76        arrayBuffer,
77        offset,
78        functionCleanupArena,
79        appendArrayMap
80    ) {
81        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, this.#date.ffiValue, Uint32Array);
82        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 4, this.#time.ffiValue, Uint32Array);
83    }
84
85    // This struct contains borrowed fields, so this takes in a list of
86    // "edges" corresponding to where each lifetime's data may have been borrowed from
87    // and passes it down to individual fields containing the borrow.
88    // This method does not attempt to handle any dependencies between lifetimes, the caller
89    // should handle this when constructing edge arrays.
90    static _fromFFI(internalConstructor, ptr) {
91        if (internalConstructor !== diplomatRuntime.internalConstructor) {
92            throw new Error("DateTime._fromFFI is not meant to be called externally. Please use the default constructor.");
93        }
94        let structObj = {};
95        const dateDeref = diplomatRuntime.ptrRead(wasm, ptr);
96        structObj.date = new Date(diplomatRuntime.internalConstructor, dateDeref, []);
97        const timeDeref = diplomatRuntime.ptrRead(wasm, ptr + 4);
98        structObj.time = new Time(diplomatRuntime.internalConstructor, timeDeref, []);
99
100        return new DateTime(structObj, internalConstructor);
101    }
102
103    static fromString(v, calendar) {
104        let functionCleanupArena = new diplomatRuntime.CleanupArena();
105
106        const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, v));
107
108        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 9, 4, true);
109
110        const result = wasm.icu4x_DateTime_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat(), calendar.ffiValue);
111
112        try {
113            if (!diplomatReceive.resultFlag) {
114                const cause = new CalendarParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
115                throw new globalThis.Error('CalendarParseError: ' + cause.value, { cause });
116            }
117            return DateTime._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
118        }
119
120        finally {
121            functionCleanupArena.free();
122
123            diplomatReceive.free();
124        }
125    }
126
127    constructor(structObj, internalConstructor) {
128        return this.#internalConstructor(...arguments)
129    }
130}