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