• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { WeekRelativeUnit } from "./WeekRelativeUnit.mjs"
3import wasm from "./diplomat-wasm.mjs";
4import * as diplomatRuntime from "./diplomat-runtime.mjs";
5
6
7/** See the [Rust documentation for `WeekOf`](https://docs.rs/icu/latest/icu/calendar/week/struct.WeekOf.html) for more information.
8*/
9
10
11export class WeekOf {
12
13    #week;
14
15    get week()  {
16        return this.#week;
17    }
18
19    #unit;
20
21    get unit()  {
22        return this.#unit;
23    }
24
25    #internalConstructor(structObj, internalConstructor) {
26        if (typeof structObj !== "object") {
27            throw new Error("WeekOf's constructor takes an object of WeekOf's fields.");
28        }
29
30        if (internalConstructor !== diplomatRuntime.internalConstructor) {
31            throw new Error("WeekOf is an out struct and can only be created internally.");
32        }
33        if ("week" in structObj) {
34            this.#week = structObj.week;
35        } else {
36            throw new Error("Missing required field week.");
37        }
38
39        if ("unit" in structObj) {
40            this.#unit = structObj.unit;
41        } else {
42            throw new Error("Missing required field unit.");
43        }
44
45        return this;
46    }
47
48    // Return this struct in FFI function friendly format.
49    // Returns an array that can be expanded with spread syntax (...)
50
51    // JS structs need to be generated with or without padding depending on whether they are being passed as aggregates or splatted out into fields.
52    // Most of the time this is known beforehand: large structs (>2 scalar fields) always get padding, and structs passed directly in parameters omit padding
53    // if they are small. However small structs within large structs also get padding, and we signal that by setting forcePadding.
54    _intoFFI(
55        functionCleanupArena,
56        appendArrayMap,
57        forcePadding
58    ) {
59        return [this.#week, ...diplomatRuntime.maybePaddingFields(forcePadding, 3 /* x i8 */), this.#unit.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 WeekOf) {
68            return obj;
69        }
70
71        return WeekOf.fromFields(obj);
72    }
73
74    _writeToArrayBuffer(
75        arrayBuffer,
76        offset,
77        functionCleanupArena,
78        appendArrayMap,
79        forcePadding
80    ) {
81        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, this.#week, Uint8Array);
82        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 4, this.#unit.ffiValue, Int32Array);
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("WeekOf._fromFFI is not meant to be called externally. Please use the default constructor.");
93        }
94        let structObj = {};
95        const weekDeref = (new Uint8Array(wasm.memory.buffer, ptr, 1))[0];
96        structObj.week = weekDeref;
97        const unitDeref = diplomatRuntime.enumDiscriminant(wasm, ptr + 4);
98        structObj.unit = new WeekRelativeUnit(diplomatRuntime.internalConstructor, unitDeref);
99
100        return new WeekOf(structObj, internalConstructor);
101    }
102
103    constructor(structObj, internalConstructor) {
104        return this.#internalConstructor(...arguments)
105    }
106}