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