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