RegionDisplayNames.mjs (5951B)
1 // generated by diplomat-tool 2 import { DataError } from "./DataError.mjs" 3 import { DataProvider } from "./DataProvider.mjs" 4 import { DisplayNamesOptions } from "./DisplayNamesOptions.mjs" 5 import { Locale } from "./Locale.mjs" 6 import { LocaleParseError } from "./LocaleParseError.mjs" 7 import wasm from "./diplomat-wasm.mjs"; 8 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 9 10 11 /** 12 * See the [Rust documentation for `RegionDisplayNames`](https://docs.rs/icu/latest/icu/experimental/displaynames/struct.RegionDisplayNames.html) for more information. 13 */ 14 const RegionDisplayNames_box_destroy_registry = new FinalizationRegistry((ptr) => { 15 wasm.icu4x_RegionDisplayNames_destroy_mv1(ptr); 16 }); 17 18 export class RegionDisplayNames { 19 // Internal ptr reference: 20 #ptr = null; 21 22 // Lifetimes are only to keep dependencies alive. 23 // Since JS won't garbage collect until there are no incoming edges. 24 #selfEdge = []; 25 26 #internalConstructor(symbol, ptr, selfEdge) { 27 if (symbol !== diplomatRuntime.internalConstructor) { 28 console.error("RegionDisplayNames is an Opaque type. You cannot call its constructor."); 29 return; 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 RegionDisplayNames_box_destroy_registry.register(this, this.#ptr); 37 } 38 39 return this; 40 } 41 get ffiValue() { 42 return this.#ptr; 43 } 44 45 46 /** 47 * Creates a new `RegionDisplayNames` from locale data and an options bag using compiled data. 48 * 49 * See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/experimental/displaynames/struct.RegionDisplayNames.html#method.try_new) for more information. 50 */ 51 #defaultConstructor(locale, options) { 52 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 53 54 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 55 56 57 const result = wasm.icu4x_RegionDisplayNames_create_v1_mv1(diplomatReceive.buffer, locale.ffiValue, ...DisplayNamesOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {})); 58 59 try { 60 if (!diplomatReceive.resultFlag) { 61 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 62 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 63 } 64 return new RegionDisplayNames(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 65 } 66 67 finally { 68 functionCleanupArena.free(); 69 70 diplomatReceive.free(); 71 } 72 } 73 74 /** 75 * Creates a new `RegionDisplayNames` from locale data and an options bag using a particular data source. 76 * 77 * See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/experimental/displaynames/struct.RegionDisplayNames.html#method.try_new) for more information. 78 */ 79 static createWithProvider(provider, locale, options) { 80 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 81 82 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 83 84 85 const result = wasm.icu4x_RegionDisplayNames_create_v1_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, ...DisplayNamesOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {})); 86 87 try { 88 if (!diplomatReceive.resultFlag) { 89 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 90 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 91 } 92 return new RegionDisplayNames(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 93 } 94 95 finally { 96 functionCleanupArena.free(); 97 98 diplomatReceive.free(); 99 } 100 } 101 102 /** 103 * Returns the locale specific display name of a region. 104 * Note that the function returns an empty string in case the display name for a given 105 * region code is not found. 106 * 107 * See the [Rust documentation for `of`](https://docs.rs/icu/latest/icu/experimental/displaynames/struct.RegionDisplayNames.html#method.of) for more information. 108 */ 109 of(region) { 110 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 111 112 const regionSlice = diplomatRuntime.DiplomatBuf.str8(wasm, region); 113 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 114 115 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 116 117 118 const result = wasm.icu4x_RegionDisplayNames_of_mv1(diplomatReceive.buffer, this.ffiValue, ...regionSlice.splat(), write.buffer); 119 120 try { 121 if (!diplomatReceive.resultFlag) { 122 const cause = new LocaleParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 123 throw new globalThis.Error('LocaleParseError: ' + cause.value, { cause }); 124 } 125 return write.readString8(); 126 } 127 128 finally { 129 functionCleanupArena.free(); 130 131 diplomatReceive.free(); 132 write.free(); 133 } 134 } 135 136 constructor(locale, options) { 137 if (arguments[0] === diplomatRuntime.exposeConstructor) { 138 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 139 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 140 return this.#internalConstructor(...arguments); 141 } else { 142 return this.#defaultConstructor(...arguments); 143 } 144 } 145 }