• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { DataError } from "./DataError.mjs"
3import { DataProvider } from "./DataProvider.mjs"
4import { ListLength } from "./ListLength.mjs"
5import { Locale } from "./Locale.mjs"
6import wasm from "./diplomat-wasm.mjs";
7import * as diplomatRuntime from "./diplomat-runtime.mjs";
8
9
10/** See the [Rust documentation for `ListFormatter`](https://docs.rs/icu/latest/icu/list/struct.ListFormatter.html) for more information.
11*/
12const ListFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => {
13    wasm.icu4x_ListFormatter_destroy_mv1(ptr);
14});
15
16export class ListFormatter {
17
18    // Internal ptr reference:
19    #ptr = null;
20
21    // Lifetimes are only to keep dependencies alive.
22    // Since JS won't garbage collect until there are no incoming edges.
23    #selfEdge = [];
24
25    #internalConstructor(symbol, ptr, selfEdge) {
26        if (symbol !== diplomatRuntime.internalConstructor) {
27            console.error("ListFormatter is an Opaque type. You cannot call its constructor.");
28            return;
29        }
30
31        this.#ptr = ptr;
32        this.#selfEdge = selfEdge;
33
34        // Are we being borrowed? If not, we can register.
35        if (this.#selfEdge.length === 0) {
36            ListFormatter_box_destroy_registry.register(this, this.#ptr);
37        }
38
39        return this;
40    }
41    get ffiValue() {
42        return this.#ptr;
43    }
44
45    static createAndWithLength(locale, length) {
46        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
47
48        const result = wasm.icu4x_ListFormatter_create_and_with_length_mv1(diplomatReceive.buffer, locale.ffiValue, length.ffiValue);
49
50        try {
51            if (!diplomatReceive.resultFlag) {
52                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
53                throw new globalThis.Error('DataError: ' + cause.value, { cause });
54            }
55            return new ListFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
56        }
57
58        finally {
59            diplomatReceive.free();
60        }
61    }
62
63    static createAndWithLengthAndProvider(provider, locale, length) {
64        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
65
66        const result = wasm.icu4x_ListFormatter_create_and_with_length_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, length.ffiValue);
67
68        try {
69            if (!diplomatReceive.resultFlag) {
70                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
71                throw new globalThis.Error('DataError: ' + cause.value, { cause });
72            }
73            return new ListFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
74        }
75
76        finally {
77            diplomatReceive.free();
78        }
79    }
80
81    static createOrWithLength(locale, length) {
82        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
83
84        const result = wasm.icu4x_ListFormatter_create_or_with_length_mv1(diplomatReceive.buffer, locale.ffiValue, length.ffiValue);
85
86        try {
87            if (!diplomatReceive.resultFlag) {
88                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
89                throw new globalThis.Error('DataError: ' + cause.value, { cause });
90            }
91            return new ListFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
92        }
93
94        finally {
95            diplomatReceive.free();
96        }
97    }
98
99    static createOrWithLengthAndProvider(provider, locale, length) {
100        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
101
102        const result = wasm.icu4x_ListFormatter_create_or_with_length_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, length.ffiValue);
103
104        try {
105            if (!diplomatReceive.resultFlag) {
106                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
107                throw new globalThis.Error('DataError: ' + cause.value, { cause });
108            }
109            return new ListFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
110        }
111
112        finally {
113            diplomatReceive.free();
114        }
115    }
116
117    static createUnitWithLength(locale, length) {
118        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
119
120        const result = wasm.icu4x_ListFormatter_create_unit_with_length_mv1(diplomatReceive.buffer, locale.ffiValue, length.ffiValue);
121
122        try {
123            if (!diplomatReceive.resultFlag) {
124                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
125                throw new globalThis.Error('DataError: ' + cause.value, { cause });
126            }
127            return new ListFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
128        }
129
130        finally {
131            diplomatReceive.free();
132        }
133    }
134
135    static createUnitWithLengthAndProvider(provider, locale, length) {
136        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
137
138        const result = wasm.icu4x_ListFormatter_create_unit_with_length_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, length.ffiValue);
139
140        try {
141            if (!diplomatReceive.resultFlag) {
142                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
143                throw new globalThis.Error('DataError: ' + cause.value, { cause });
144            }
145            return new ListFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
146        }
147
148        finally {
149            diplomatReceive.free();
150        }
151    }
152
153    format(list) {
154        let functionCleanupArena = new diplomatRuntime.CleanupArena();
155
156        const listSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.strs(wasm, list, "string16"));
157
158        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
159        wasm.icu4x_ListFormatter_format_utf16_mv1(this.ffiValue, ...listSlice.splat(), write.buffer);
160
161        try {
162            return write.readString8();
163        }
164
165        finally {
166            functionCleanupArena.free();
167
168            write.free();
169        }
170    }
171
172    constructor(symbol, ptr, selfEdge) {
173        return this.#internalConstructor(...arguments)
174    }
175}