• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { DataProvider } from "./DataProvider.mjs"
3import { DateTimeFormatterLoadError } from "./DateTimeFormatterLoadError.mjs"
4import { DateTimeLength } from "./DateTimeLength.mjs"
5import { IsoDate } from "./IsoDate.mjs"
6import { Locale } from "./Locale.mjs"
7import wasm from "./diplomat-wasm.mjs";
8import * as diplomatRuntime from "./diplomat-runtime.mjs";
9
10
11/** An ICU4X TypedDateFormatter object capable of formatting an [`IsoDate`] and a [`Time`] as a string,
12*using the Gregorian Calendar.
13*
14*See the [Rust documentation for `FixedCalendarDateTimeFormatter`](https://docs.rs/icu/latest/icu/datetime/struct.FixedCalendarDateTimeFormatter.html) for more information.
15*
16*Additional information: [1](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.YMD.html)
17*/
18const GregorianDateFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => {
19    wasm.icu4x_GregorianDateFormatter_destroy_mv1(ptr);
20});
21
22export class GregorianDateFormatter {
23
24    // Internal ptr reference:
25    #ptr = null;
26
27    // Lifetimes are only to keep dependencies alive.
28    // Since JS won't garbage collect until there are no incoming edges.
29    #selfEdge = [];
30
31    #internalConstructor(symbol, ptr, selfEdge) {
32        if (symbol !== diplomatRuntime.internalConstructor) {
33            console.error("GregorianDateFormatter is an Opaque type. You cannot call its constructor.");
34            return;
35        }
36
37        this.#ptr = ptr;
38        this.#selfEdge = selfEdge;
39
40        // Are we being borrowed? If not, we can register.
41        if (this.#selfEdge.length === 0) {
42            GregorianDateFormatter_box_destroy_registry.register(this, this.#ptr);
43        }
44
45        return this;
46    }
47    get ffiValue() {
48        return this.#ptr;
49    }
50
51    static createWithLength(locale, length) {
52        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
53
54        const result = wasm.icu4x_GregorianDateFormatter_create_with_length_mv1(diplomatReceive.buffer, locale.ffiValue, length.ffiValue);
55
56        try {
57            if (!diplomatReceive.resultFlag) {
58                const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
59                throw new globalThis.Error('DateTimeFormatterLoadError: ' + cause.value, { cause });
60            }
61            return new GregorianDateFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
62        }
63
64        finally {
65            diplomatReceive.free();
66        }
67    }
68
69    static createWithLengthAndProvider(provider, locale, length) {
70        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
71
72        const result = wasm.icu4x_GregorianDateFormatter_create_with_length_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, length.ffiValue);
73
74        try {
75            if (!diplomatReceive.resultFlag) {
76                const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
77                throw new globalThis.Error('DateTimeFormatterLoadError: ' + cause.value, { cause });
78            }
79            return new GregorianDateFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
80        }
81
82        finally {
83            diplomatReceive.free();
84        }
85    }
86
87    formatIso(value) {
88        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
89        wasm.icu4x_GregorianDateFormatter_format_iso_mv1(this.ffiValue, value.ffiValue, write.buffer);
90
91        try {
92            return write.readString8();
93        }
94
95        finally {
96            write.free();
97        }
98    }
99
100    constructor(symbol, ptr, selfEdge) {
101        return this.#internalConstructor(...arguments)
102    }
103}