PluralRules.mjs (7083B)
1 // generated by diplomat-tool 2 import { DataError } from "./DataError.mjs" 3 import { DataProvider } from "./DataProvider.mjs" 4 import { Locale } from "./Locale.mjs" 5 import { PluralCategories } from "./PluralCategories.mjs" 6 import { PluralCategory } from "./PluralCategory.mjs" 7 import { PluralOperands } from "./PluralOperands.mjs" 8 import wasm from "./diplomat-wasm.mjs"; 9 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 10 11 12 /** 13 * See the [Rust documentation for `PluralRules`](https://docs.rs/icu/latest/icu/plurals/struct.PluralRules.html) for more information. 14 */ 15 const PluralRules_box_destroy_registry = new FinalizationRegistry((ptr) => { 16 wasm.icu4x_PluralRules_destroy_mv1(ptr); 17 }); 18 19 export class PluralRules { 20 // Internal ptr reference: 21 #ptr = null; 22 23 // Lifetimes are only to keep dependencies alive. 24 // Since JS won't garbage collect until there are no incoming edges. 25 #selfEdge = []; 26 27 #internalConstructor(symbol, ptr, selfEdge) { 28 if (symbol !== diplomatRuntime.internalConstructor) { 29 console.error("PluralRules is an Opaque type. You cannot call its constructor."); 30 return; 31 } 32 this.#ptr = ptr; 33 this.#selfEdge = selfEdge; 34 35 // Are we being borrowed? If not, we can register. 36 if (this.#selfEdge.length === 0) { 37 PluralRules_box_destroy_registry.register(this, this.#ptr); 38 } 39 40 return this; 41 } 42 get ffiValue() { 43 return this.#ptr; 44 } 45 46 47 /** 48 * Construct an [`PluralRules`] for the given locale, for cardinal numbers, using compiled data. 49 * 50 * See the [Rust documentation for `try_new_cardinal`](https://docs.rs/icu/latest/icu/plurals/struct.PluralRules.html#method.try_new_cardinal) for more information. 51 */ 52 static createCardinal(locale) { 53 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 54 55 56 const result = wasm.icu4x_PluralRules_create_cardinal_mv1(diplomatReceive.buffer, locale.ffiValue); 57 58 try { 59 if (!diplomatReceive.resultFlag) { 60 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 61 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 62 } 63 return new PluralRules(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 64 } 65 66 finally { 67 diplomatReceive.free(); 68 } 69 } 70 71 /** 72 * Construct an [`PluralRules`] for the given locale, for cardinal numbers, using a particular data source. 73 * 74 * See the [Rust documentation for `try_new_cardinal`](https://docs.rs/icu/latest/icu/plurals/struct.PluralRules.html#method.try_new_cardinal) for more information. 75 */ 76 static createCardinalWithProvider(provider, locale) { 77 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 78 79 80 const result = wasm.icu4x_PluralRules_create_cardinal_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue); 81 82 try { 83 if (!diplomatReceive.resultFlag) { 84 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 85 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 86 } 87 return new PluralRules(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 88 } 89 90 finally { 91 diplomatReceive.free(); 92 } 93 } 94 95 /** 96 * Construct an [`PluralRules`] for the given locale, for ordinal numbers, using compiled data. 97 * 98 * See the [Rust documentation for `try_new_ordinal`](https://docs.rs/icu/latest/icu/plurals/struct.PluralRules.html#method.try_new_ordinal) for more information. 99 */ 100 static createOrdinal(locale) { 101 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 102 103 104 const result = wasm.icu4x_PluralRules_create_ordinal_mv1(diplomatReceive.buffer, locale.ffiValue); 105 106 try { 107 if (!diplomatReceive.resultFlag) { 108 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 109 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 110 } 111 return new PluralRules(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 112 } 113 114 finally { 115 diplomatReceive.free(); 116 } 117 } 118 119 /** 120 * Construct an [`PluralRules`] for the given locale, for ordinal numbers, using a particular data source. 121 * 122 * See the [Rust documentation for `try_new_ordinal`](https://docs.rs/icu/latest/icu/plurals/struct.PluralRules.html#method.try_new_ordinal) for more information. 123 */ 124 static createOrdinalWithProvider(provider, locale) { 125 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 126 127 128 const result = wasm.icu4x_PluralRules_create_ordinal_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue); 129 130 try { 131 if (!diplomatReceive.resultFlag) { 132 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 133 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 134 } 135 return new PluralRules(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 136 } 137 138 finally { 139 diplomatReceive.free(); 140 } 141 } 142 143 /** 144 * Get the category for a given number represented as operands 145 * 146 * See the [Rust documentation for `category_for`](https://docs.rs/icu/latest/icu/plurals/struct.PluralRules.html#method.category_for) for more information. 147 */ 148 categoryFor(op) { 149 150 const result = wasm.icu4x_PluralRules_category_for_mv1(this.ffiValue, op.ffiValue); 151 152 try { 153 return new PluralCategory(diplomatRuntime.internalConstructor, result); 154 } 155 156 finally { 157 } 158 } 159 160 /** 161 * Get all of the categories needed in the current locale 162 * 163 * See the [Rust documentation for `categories`](https://docs.rs/icu/latest/icu/plurals/struct.PluralRules.html#method.categories) for more information. 164 */ 165 get categories() { 166 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 6, 1, false); 167 168 169 const result = wasm.icu4x_PluralRules_categories_mv1(diplomatReceive.buffer, this.ffiValue); 170 171 try { 172 return PluralCategories._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 173 } 174 175 finally { 176 diplomatReceive.free(); 177 } 178 } 179 180 constructor(symbol, ptr, selfEdge) { 181 return this.#internalConstructor(...arguments) 182 } 183 }