Collator.mjs (6058B)
1 // generated by diplomat-tool 2 import { CollatorOptions } from "./CollatorOptions.mjs" 3 import { CollatorResolvedOptions } from "./CollatorResolvedOptions.mjs" 4 import { DataError } from "./DataError.mjs" 5 import { DataProvider } from "./DataProvider.mjs" 6 import { Locale } from "./Locale.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 `Collator`](https://docs.rs/icu/latest/icu/collator/struct.Collator.html) for more information. 13 */ 14 const Collator_box_destroy_registry = new FinalizationRegistry((ptr) => { 15 wasm.icu4x_Collator_destroy_mv1(ptr); 16 }); 17 18 export class Collator { 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("Collator 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 Collator_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 * Construct a new Collator instance using compiled data. 48 * 49 * See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/collator/struct.Collator.html#method.try_new) for more information. 50 */ 51 static create(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_Collator_create_v1_mv1(diplomatReceive.buffer, locale.ffiValue, ...CollatorOptions._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 Collator(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 65 } 66 67 finally { 68 functionCleanupArena.free(); 69 70 diplomatReceive.free(); 71 } 72 } 73 74 /** 75 * Construct a new Collator instance using a particular data source. 76 * 77 * See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/collator/struct.Collator.html#method.try_new) for more information. 78 */ 79 #defaultConstructor(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_Collator_create_v1_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, ...CollatorOptions._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 Collator(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 93 } 94 95 finally { 96 functionCleanupArena.free(); 97 98 diplomatReceive.free(); 99 } 100 } 101 102 /** 103 * Compare two strings. 104 * 105 * Ill-formed input is treated as if errors had been replaced with REPLACEMENT CHARACTERs according 106 * to the WHATWG Encoding Standard. 107 * 108 * See the [Rust documentation for `compare_utf16`](https://docs.rs/icu/latest/icu/collator/struct.CollatorBorrowed.html#method.compare_utf16) for more information. 109 */ 110 compare(left, right) { 111 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 112 113 const leftSlice = diplomatRuntime.DiplomatBuf.str16(wasm, left); 114 const rightSlice = diplomatRuntime.DiplomatBuf.str16(wasm, right); 115 116 const result = wasm.icu4x_Collator_compare_utf16_mv1(this.ffiValue, ...leftSlice.splat(), ...rightSlice.splat()); 117 118 try { 119 return result; 120 } 121 122 finally { 123 functionCleanupArena.free(); 124 125 } 126 } 127 128 /** 129 * The resolved options showing how the default options, the requested options, 130 * and the options from locale data were combined. None of the struct fields 131 * will have `Auto` as the value. 132 * 133 * See the [Rust documentation for `resolved_options`](https://docs.rs/icu/latest/icu/collator/struct.CollatorBorrowed.html#method.resolved_options) for more information. 134 */ 135 get resolvedOptions() { 136 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 24, 4, false); 137 138 139 const result = wasm.icu4x_Collator_resolved_options_v1_mv1(diplomatReceive.buffer, this.ffiValue); 140 141 try { 142 return CollatorResolvedOptions._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 143 } 144 145 finally { 146 diplomatReceive.free(); 147 } 148 } 149 150 constructor(provider, locale, options) { 151 if (arguments[0] === diplomatRuntime.exposeConstructor) { 152 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 153 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 154 return this.#internalConstructor(...arguments); 155 } else { 156 return this.#defaultConstructor(...arguments); 157 } 158 } 159 }