TitlecaseOptions.mjs (5586B)
1 // generated by diplomat-tool 2 import { LeadingAdjustment } from "./LeadingAdjustment.mjs" 3 import { TrailingCase } from "./TrailingCase.mjs" 4 import wasm from "./diplomat-wasm.mjs"; 5 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 6 7 8 /** 9 * See the [Rust documentation for `TitlecaseOptions`](https://docs.rs/icu/latest/icu/casemap/options/struct.TitlecaseOptions.html) for more information. 10 */ 11 12 13 export class TitlecaseOptions { 14 #leadingAdjustment; 15 get leadingAdjustment() { 16 return this.#leadingAdjustment; 17 } 18 set leadingAdjustment(value){ 19 this.#leadingAdjustment = value; 20 } 21 #trailingCase; 22 get trailingCase() { 23 return this.#trailingCase; 24 } 25 set trailingCase(value){ 26 this.#trailingCase = value; 27 } 28 /** Create `TitlecaseOptions` from an object that contains all of `TitlecaseOptions`s fields. 29 * Optional fields do not need to be included in the provided object. 30 */ 31 static fromFields(structObj) { 32 return new TitlecaseOptions(diplomatRuntime.exposeConstructor, structObj); 33 } 34 35 #internalConstructor(structObj) { 36 if (typeof structObj !== "object") { 37 throw new Error("TitlecaseOptions's constructor takes an object of TitlecaseOptions's fields."); 38 } 39 40 if ("leadingAdjustment" in structObj) { 41 this.#leadingAdjustment = structObj.leadingAdjustment; 42 } else { 43 this.#leadingAdjustment = null; 44 } 45 46 if ("trailingCase" in structObj) { 47 this.#trailingCase = structObj.trailingCase; 48 } else { 49 this.#trailingCase = null; 50 } 51 52 return this; 53 } 54 55 // Return this struct in FFI function friendly format. 56 // Returns an array that can be expanded with spread syntax (...) 57 _intoFFI( 58 functionCleanupArena, 59 appendArrayMap 60 ) { 61 return [...diplomatRuntime.optionToArgsForCalling(this.#leadingAdjustment, 4, 4, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]), ...diplomatRuntime.optionToArgsForCalling(this.#trailingCase, 4, 4, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)])] 62 } 63 64 static _fromSuppliedValue(internalConstructor, obj) { 65 if (internalConstructor !== diplomatRuntime.internalConstructor) { 66 throw new Error("_fromSuppliedValue cannot be called externally."); 67 } 68 69 if (obj instanceof TitlecaseOptions) { 70 return obj; 71 } 72 73 return TitlecaseOptions.fromFields(obj); 74 } 75 76 _writeToArrayBuffer( 77 arrayBuffer, 78 offset, 79 functionCleanupArena, 80 appendArrayMap 81 ) { 82 diplomatRuntime.writeOptionToArrayBuffer(arrayBuffer, offset + 0, this.#leadingAdjustment, 4, 4, (arrayBuffer, offset, jsValue) => diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)); 83 diplomatRuntime.writeOptionToArrayBuffer(arrayBuffer, offset + 8, this.#trailingCase, 4, 4, (arrayBuffer, offset, jsValue) => diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)); 84 } 85 86 // This struct contains borrowed fields, so this takes in a list of 87 // "edges" corresponding to where each lifetime's data may have been borrowed from 88 // and passes it down to individual fields containing the borrow. 89 // This method does not attempt to handle any dependencies between lifetimes, the caller 90 // should handle this when constructing edge arrays. 91 static _fromFFI(internalConstructor, ptr) { 92 if (internalConstructor !== diplomatRuntime.internalConstructor) { 93 throw new Error("TitlecaseOptions._fromFFI is not meant to be called externally. Please use the default constructor."); 94 } 95 let structObj = {}; 96 const leadingAdjustmentDeref = ptr; 97 structObj.leadingAdjustment = diplomatRuntime.readOption(wasm, leadingAdjustmentDeref, 4, (wasm, offset) => { const deref = diplomatRuntime.enumDiscriminant(wasm, offset); return new LeadingAdjustment(diplomatRuntime.internalConstructor, deref) }); 98 const trailingCaseDeref = ptr + 8; 99 structObj.trailingCase = diplomatRuntime.readOption(wasm, trailingCaseDeref, 4, (wasm, offset) => { const deref = diplomatRuntime.enumDiscriminant(wasm, offset); return new TrailingCase(diplomatRuntime.internalConstructor, deref) }); 100 101 return new TitlecaseOptions(diplomatRuntime.exposeConstructor, structObj); 102 } 103 104 105 /** 106 * See the [Rust documentation for `default`](https://docs.rs/icu/latest/icu/casemap/options/struct.TitlecaseOptions.html#method.default) for more information. 107 */ 108 #defaultConstructor() { 109 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 16, 4, false); 110 111 112 const result = wasm.icu4x_TitlecaseOptionsV1_default_mv1(diplomatReceive.buffer); 113 114 try { 115 return TitlecaseOptions._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 116 } 117 118 finally { 119 diplomatReceive.free(); 120 } 121 } 122 123 constructor() { 124 if (arguments[0] === diplomatRuntime.exposeConstructor) { 125 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 126 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 127 return this.#internalConstructor(...arguments); 128 } else { 129 return this.#defaultConstructor(...arguments); 130 } 131 } 132 }