TitlecaseMapper.mjs (5986B)
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 { TitlecaseOptions } from "./TitlecaseOptions.mjs" 6 import wasm from "./diplomat-wasm.mjs"; 7 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 8 9 10 /** 11 * See the [Rust documentation for `TitlecaseMapper`](https://docs.rs/icu/latest/icu/casemap/struct.TitlecaseMapper.html) for more information. 12 */ 13 const TitlecaseMapper_box_destroy_registry = new FinalizationRegistry((ptr) => { 14 wasm.icu4x_TitlecaseMapper_destroy_mv1(ptr); 15 }); 16 17 export class TitlecaseMapper { 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("TitlecaseMapper is an Opaque type. You cannot call its constructor."); 28 return; 29 } 30 this.#ptr = ptr; 31 this.#selfEdge = selfEdge; 32 33 // Are we being borrowed? If not, we can register. 34 if (this.#selfEdge.length === 0) { 35 TitlecaseMapper_box_destroy_registry.register(this, this.#ptr); 36 } 37 38 return this; 39 } 40 get ffiValue() { 41 return this.#ptr; 42 } 43 44 45 /** 46 * Construct a new `TitlecaseMapper` instance using compiled data. 47 * 48 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/casemap/struct.TitlecaseMapper.html#method.new) for more information. 49 */ 50 #defaultConstructor() { 51 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 52 53 54 const result = wasm.icu4x_TitlecaseMapper_create_mv1(diplomatReceive.buffer); 55 56 try { 57 if (!diplomatReceive.resultFlag) { 58 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 59 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 60 } 61 return new TitlecaseMapper(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 62 } 63 64 finally { 65 diplomatReceive.free(); 66 } 67 } 68 69 /** 70 * Construct a new `TitlecaseMapper` instance using a particular data source. 71 * 72 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/casemap/struct.TitlecaseMapper.html#method.new) for more information. 73 */ 74 static createWithProvider(provider) { 75 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 76 77 78 const result = wasm.icu4x_TitlecaseMapper_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 79 80 try { 81 if (!diplomatReceive.resultFlag) { 82 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 83 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 84 } 85 return new TitlecaseMapper(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 86 } 87 88 finally { 89 diplomatReceive.free(); 90 } 91 } 92 93 /** 94 * Returns the full titlecase mapping of the given string 95 * 96 * The `v1` refers to the version of the options struct, which may change as we add more options 97 * 98 * See the [Rust documentation for `titlecase_segment`](https://docs.rs/icu/latest/icu/casemap/struct.TitlecaseMapperBorrowed.html#method.titlecase_segment) for more information. 99 */ 100 titlecaseSegment(s, locale, options) { 101 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 102 103 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 104 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 105 106 wasm.icu4x_TitlecaseMapper_titlecase_segment_v1_mv1(this.ffiValue, ...sSlice.splat(), locale.ffiValue, ...TitlecaseOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}), write.buffer); 107 108 try { 109 return write.readString8(); 110 } 111 112 finally { 113 functionCleanupArena.free(); 114 115 write.free(); 116 } 117 } 118 119 /** 120 * Returns the full titlecase mapping of the given string, using compiled data (avoids having to allocate a TitlecaseMapper object) 121 * 122 * The `v1` refers to the version of the options struct, which may change as we add more options 123 * 124 * See the [Rust documentation for `titlecase_segment`](https://docs.rs/icu/latest/icu/casemap/struct.TitlecaseMapperBorrowed.html#method.titlecase_segment) for more information. 125 */ 126 static titlecaseSegmentWithCompiledData(s, locale, options) { 127 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 128 129 const sSlice = diplomatRuntime.DiplomatBuf.str8(wasm, s); 130 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 131 132 wasm.icu4x_TitlecaseMapper_titlecase_segment_with_compiled_data_v1_mv1(...sSlice.splat(), locale.ffiValue, ...TitlecaseOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}), write.buffer); 133 134 try { 135 return write.readString8(); 136 } 137 138 finally { 139 functionCleanupArena.free(); 140 141 write.free(); 142 } 143 } 144 145 constructor() { 146 if (arguments[0] === diplomatRuntime.exposeConstructor) { 147 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 148 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 149 return this.#internalConstructor(...arguments); 150 } else { 151 return this.#defaultConstructor(...arguments); 152 } 153 } 154 }