• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { Calendar } from "./Calendar.mjs"
3import { DataProvider } from "./DataProvider.mjs"
4import { Date } from "./Date.mjs"
5import { DateTimeFormatError } from "./DateTimeFormatError.mjs"
6import { DateTimeFormatterLoadError } from "./DateTimeFormatterLoadError.mjs"
7import { DateTimeLength } from "./DateTimeLength.mjs"
8import { IsoDate } from "./IsoDate.mjs"
9import { Locale } from "./Locale.mjs"
10import wasm from "./diplomat-wasm.mjs";
11import * as diplomatRuntime from "./diplomat-runtime.mjs";
12
13
14/** An ICU4X DateFormatter object capable of formatting a [`Date`] as a string,
15*using some calendar specified at runtime in the locale.
16*
17*See the [Rust documentation for `DateTimeFormatter`](https://docs.rs/icu/latest/icu/datetime/struct.DateTimeFormatter.html) for more information.
18*
19*Additional information: [1](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.YMD.html)
20*/
21const DateFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => {
22    wasm.icu4x_DateFormatter_destroy_mv1(ptr);
23});
24
25export class DateFormatter {
26
27    // Internal ptr reference:
28    #ptr = null;
29
30    // Lifetimes are only to keep dependencies alive.
31    // Since JS won't garbage collect until there are no incoming edges.
32    #selfEdge = [];
33
34    #internalConstructor(symbol, ptr, selfEdge) {
35        if (symbol !== diplomatRuntime.internalConstructor) {
36            console.error("DateFormatter is an Opaque type. You cannot call its constructor.");
37            return;
38        }
39
40        this.#ptr = ptr;
41        this.#selfEdge = selfEdge;
42
43        // Are we being borrowed? If not, we can register.
44        if (this.#selfEdge.length === 0) {
45            DateFormatter_box_destroy_registry.register(this, this.#ptr);
46        }
47
48        return this;
49    }
50    get ffiValue() {
51        return this.#ptr;
52    }
53
54    static createWithLength(locale, length) {
55        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
56
57        const result = wasm.icu4x_DateFormatter_create_with_length_mv1(diplomatReceive.buffer, locale.ffiValue, length.ffiValue);
58
59        try {
60            if (!diplomatReceive.resultFlag) {
61                const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
62                throw new globalThis.Error('DateTimeFormatterLoadError: ' + cause.value, { cause });
63            }
64            return new DateFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
65        }
66
67        finally {
68            diplomatReceive.free();
69        }
70    }
71
72    static createWithLengthAndProvider(provider, locale, length) {
73        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
74
75        const result = wasm.icu4x_DateFormatter_create_with_length_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, length.ffiValue);
76
77        try {
78            if (!diplomatReceive.resultFlag) {
79                const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
80                throw new globalThis.Error('DateTimeFormatterLoadError: ' + cause.value, { cause });
81            }
82            return new DateFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
83        }
84
85        finally {
86            diplomatReceive.free();
87        }
88    }
89
90    format(value) {
91        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
92
93        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
94
95        const result = wasm.icu4x_DateFormatter_format_mv1(diplomatReceive.buffer, this.ffiValue, value.ffiValue, write.buffer);
96
97        try {
98            if (!diplomatReceive.resultFlag) {
99                const cause = new DateTimeFormatError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
100                throw new globalThis.Error('DateTimeFormatError: ' + cause.value, { cause });
101            }
102            return write.readString8();
103        }
104
105        finally {
106            diplomatReceive.free();
107
108            write.free();
109        }
110    }
111
112    formatIso(value) {
113        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
114
115        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
116
117        const result = wasm.icu4x_DateFormatter_format_iso_mv1(diplomatReceive.buffer, this.ffiValue, value.ffiValue, write.buffer);
118
119        try {
120            if (!diplomatReceive.resultFlag) {
121                const cause = new DateTimeFormatError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
122                throw new globalThis.Error('DateTimeFormatError: ' + cause.value, { cause });
123            }
124            return write.readString8();
125        }
126
127        finally {
128            diplomatReceive.free();
129
130            write.free();
131        }
132    }
133
134    calendar() {
135        const result = wasm.icu4x_DateFormatter_calendar_mv1(this.ffiValue);
136
137        try {
138            return new Calendar(diplomatRuntime.internalConstructor, result, []);
139        }
140
141        finally {}
142    }
143
144    constructor(symbol, ptr, selfEdge) {
145        return this.#internalConstructor(...arguments)
146    }
147}