• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { DataError } from "./DataError.mjs"
3import { DataProvider } from "./DataProvider.mjs"
4import { TimeZone } from "./TimeZone.mjs"
5import { TimeZoneIterator } from "./TimeZoneIterator.mjs"
6import wasm from "./diplomat-wasm.mjs";
7import * as diplomatRuntime from "./diplomat-runtime.mjs";
8
9
10/** A mapper between IANA time zone identifiers and BCP-47 time zone identifiers.
11*
12*This mapper supports two-way mapping, but it is optimized for the case of IANA to BCP-47.
13*It also supports normalizing and canonicalizing the IANA strings.
14*
15*See the [Rust documentation for `IanaParser`](https://docs.rs/icu/latest/icu/time/zone/iana/struct.IanaParser.html) for more information.
16*/
17const IanaParser_box_destroy_registry = new FinalizationRegistry((ptr) => {
18    wasm.icu4x_IanaParser_destroy_mv1(ptr);
19});
20
21export class IanaParser {
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("IanaParser 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            IanaParser_box_destroy_registry.register(this, this.#ptr);
42        }
43
44        return this;
45    }
46    get ffiValue() {
47        return this.#ptr;
48    }
49
50    #defaultConstructor() {
51        const result = wasm.icu4x_IanaParser_create_mv1();
52
53        try {
54            return new IanaParser(diplomatRuntime.internalConstructor, result, []);
55        }
56
57        finally {}
58    }
59
60    static createWithProvider(provider) {
61        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
62
63        const result = wasm.icu4x_IanaParser_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue);
64
65        try {
66            if (!diplomatReceive.resultFlag) {
67                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
68                throw new globalThis.Error('DataError: ' + cause.value, { cause });
69            }
70            return new IanaParser(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
71        }
72
73        finally {
74            diplomatReceive.free();
75        }
76    }
77
78    parse(value) {
79        let functionCleanupArena = new diplomatRuntime.CleanupArena();
80
81        const valueSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, value));
82
83        const result = wasm.icu4x_IanaParser_parse_mv1(this.ffiValue, ...valueSlice.splat());
84
85        try {
86            return new TimeZone(diplomatRuntime.internalConstructor, result, []);
87        }
88
89        finally {
90            functionCleanupArena.free();
91        }
92    }
93
94    iter() {
95        // This lifetime edge depends on lifetimes 'a
96        let aEdges = [this];
97
98        const result = wasm.icu4x_IanaParser_iter_mv1(this.ffiValue);
99
100        try {
101            return new TimeZoneIterator(diplomatRuntime.internalConstructor, result, [], aEdges);
102        }
103
104        finally {}
105    }
106
107    constructor() {
108        if (arguments[0] === diplomatRuntime.exposeConstructor) {
109            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
110        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
111            return this.#internalConstructor(...arguments);
112        } else {
113            return this.#defaultConstructor(...arguments);
114        }
115    }
116}