1// generated by diplomat-tool 2import { DataError } from "./DataError.mjs" 3import { DataProvider } from "./DataProvider.mjs" 4import { Locale } from "./Locale.mjs" 5import { PluralCategories } from "./PluralCategories.mjs" 6import { PluralCategory } from "./PluralCategory.mjs" 7import { PluralOperands } from "./PluralOperands.mjs" 8import wasm from "./diplomat-wasm.mjs"; 9import * as diplomatRuntime from "./diplomat-runtime.mjs"; 10 11 12/** See the [Rust documentation for `PluralRules`](https://docs.rs/icu/latest/icu/plurals/struct.PluralRules.html) for more information. 13*/ 14const PluralRules_box_destroy_registry = new FinalizationRegistry((ptr) => { 15 wasm.icu4x_PluralRules_destroy_mv1(ptr); 16}); 17 18export class PluralRules { 19 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 33 this.#ptr = ptr; 34 this.#selfEdge = selfEdge; 35 36 // Are we being borrowed? If not, we can register. 37 if (this.#selfEdge.length === 0) { 38 PluralRules_box_destroy_registry.register(this, this.#ptr); 39 } 40 41 return this; 42 } 43 get ffiValue() { 44 return this.#ptr; 45 } 46 47 static createCardinal(locale) { 48 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 49 50 const result = wasm.icu4x_PluralRules_create_cardinal_mv1(diplomatReceive.buffer, locale.ffiValue); 51 52 try { 53 if (!diplomatReceive.resultFlag) { 54 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 55 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 56 } 57 return new PluralRules(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 58 } 59 60 finally { 61 diplomatReceive.free(); 62 } 63 } 64 65 static createCardinalWithProvider(provider, locale) { 66 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 67 68 const result = wasm.icu4x_PluralRules_create_cardinal_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue); 69 70 try { 71 if (!diplomatReceive.resultFlag) { 72 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 73 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 74 } 75 return new PluralRules(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 76 } 77 78 finally { 79 diplomatReceive.free(); 80 } 81 } 82 83 static createOrdinal(locale) { 84 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 85 86 const result = wasm.icu4x_PluralRules_create_ordinal_mv1(diplomatReceive.buffer, locale.ffiValue); 87 88 try { 89 if (!diplomatReceive.resultFlag) { 90 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 91 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 92 } 93 return new PluralRules(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 94 } 95 96 finally { 97 diplomatReceive.free(); 98 } 99 } 100 101 static createOrdinalWithProvider(provider, locale) { 102 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 103 104 const result = wasm.icu4x_PluralRules_create_ordinal_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, 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 categoryFor(op) { 120 const result = wasm.icu4x_PluralRules_category_for_mv1(this.ffiValue, op.ffiValue); 121 122 try { 123 return new PluralCategory(diplomatRuntime.internalConstructor, result); 124 } 125 126 finally {} 127 } 128 129 get categories() { 130 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 6, 1, false); 131 132 const result = wasm.icu4x_PluralRules_categories_mv1(diplomatReceive.buffer, this.ffiValue); 133 134 try { 135 return PluralCategories._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 136 } 137 138 finally { 139 diplomatReceive.free(); 140 } 141 } 142 143 constructor(symbol, ptr, selfEdge) { 144 return this.#internalConstructor(...arguments) 145 } 146}