TimeFormatter.mjs (6783B)
1 // generated by diplomat-tool 2 import { DataProvider } from "./DataProvider.mjs" 3 import { DateTimeAlignment } from "./DateTimeAlignment.mjs" 4 import { DateTimeFormatterLoadError } from "./DateTimeFormatterLoadError.mjs" 5 import { DateTimeLength } from "./DateTimeLength.mjs" 6 import { Locale } from "./Locale.mjs" 7 import { Time } from "./Time.mjs" 8 import { TimePrecision } from "./TimePrecision.mjs" 9 import wasm from "./diplomat-wasm.mjs"; 10 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 11 12 13 /** 14 * See the [Rust documentation for `NoCalendarFormatter`](https://docs.rs/icu/latest/icu/datetime/type.NoCalendarFormatter.html) for more information. 15 */ 16 const TimeFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => { 17 wasm.icu4x_TimeFormatter_destroy_mv1(ptr); 18 }); 19 20 export class TimeFormatter { 21 // Internal ptr reference: 22 #ptr = null; 23 24 // Lifetimes are only to keep dependencies alive. 25 // Since JS won't garbage collect until there are no incoming edges. 26 #selfEdge = []; 27 28 #internalConstructor(symbol, ptr, selfEdge) { 29 if (symbol !== diplomatRuntime.internalConstructor) { 30 console.error("TimeFormatter is an Opaque type. You cannot call its constructor."); 31 return; 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 TimeFormatter_box_destroy_registry.register(this, this.#ptr); 39 } 40 41 return this; 42 } 43 get ffiValue() { 44 return this.#ptr; 45 } 46 47 48 /** 49 * See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/datetime/type.NoCalendarFormatter.html#method.try_new) for more information. 50 * 51 * See the [Rust documentation for `T`](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.T.html) for more information. 52 * 53 * Additional information: [1](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.T.html#method.with_time_precision), [2](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.T.html#method.with_alignment), [3](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.T.html#method.for_length) 54 */ 55 #defaultConstructor(locale, length, timePrecision, alignment) { 56 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 57 58 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 59 60 61 const result = wasm.icu4x_TimeFormatter_create_mv1(diplomatReceive.buffer, locale.ffiValue, ...diplomatRuntime.optionToArgsForCalling(length, 4, 4, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]), ...diplomatRuntime.optionToArgsForCalling(timePrecision, 4, 4, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]), ...diplomatRuntime.optionToArgsForCalling(alignment, 4, 4, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)])); 62 63 try { 64 if (!diplomatReceive.resultFlag) { 65 const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 66 throw new globalThis.Error('DateTimeFormatterLoadError: ' + cause.value, { cause }); 67 } 68 return new TimeFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 69 } 70 71 finally { 72 functionCleanupArena.free(); 73 74 diplomatReceive.free(); 75 } 76 } 77 78 /** 79 * See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/datetime/type.NoCalendarFormatter.html#method.try_new) for more information. 80 * 81 * See the [Rust documentation for `T`](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.T.html) for more information. 82 * 83 * Additional information: [1](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.T.html#method.with_time_precision), [2](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.T.html#method.with_alignment), [3](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.T.html#method.for_length) 84 */ 85 static createWithProvider(provider, locale, length, timePrecision, alignment) { 86 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 87 88 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 89 90 91 const result = wasm.icu4x_TimeFormatter_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, ...diplomatRuntime.optionToArgsForCalling(length, 4, 4, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]), ...diplomatRuntime.optionToArgsForCalling(timePrecision, 4, 4, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)]), ...diplomatRuntime.optionToArgsForCalling(alignment, 4, 4, (arrayBuffer, offset, jsValue) => [diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, jsValue.ffiValue, Int32Array)])); 92 93 try { 94 if (!diplomatReceive.resultFlag) { 95 const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 96 throw new globalThis.Error('DateTimeFormatterLoadError: ' + cause.value, { cause }); 97 } 98 return new TimeFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 99 } 100 101 finally { 102 functionCleanupArena.free(); 103 104 diplomatReceive.free(); 105 } 106 } 107 108 /** 109 * See the [Rust documentation for `format`](https://docs.rs/icu/latest/icu/datetime/type.NoCalendarFormatter.html#method.format) for more information. 110 */ 111 format(time) { 112 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 113 114 wasm.icu4x_TimeFormatter_format_mv1(this.ffiValue, time.ffiValue, write.buffer); 115 116 try { 117 return write.readString8(); 118 } 119 120 finally { 121 write.free(); 122 } 123 } 124 125 constructor(locale, length, timePrecision, alignment) { 126 if (arguments[0] === diplomatRuntime.exposeConstructor) { 127 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 128 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 129 return this.#internalConstructor(...arguments); 130 } else { 131 return this.#defaultConstructor(...arguments); 132 } 133 } 134 }