IsoDateTime.mjs (4646B)
1 // generated by diplomat-tool 2 import { IsoDate } from "./IsoDate.mjs" 3 import { Rfc9557ParseError } from "./Rfc9557ParseError.mjs" 4 import { Time } from "./Time.mjs" 5 import wasm from "./diplomat-wasm.mjs"; 6 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 7 8 9 /** 10 * An ICU4X DateTime object capable of containing a ISO-8601 date and time. 11 * 12 * See the [Rust documentation for `DateTime`](https://docs.rs/icu/latest/icu/time/struct.DateTime.html) for more information. 13 */ 14 15 16 export class IsoDateTime { 17 #date; 18 get date() { 19 return this.#date; 20 } 21 #time; 22 get time() { 23 return this.#time; 24 } 25 #internalConstructor(structObj, internalConstructor) { 26 if (typeof structObj !== "object") { 27 throw new Error("IsoDateTime's constructor takes an object of IsoDateTime's fields."); 28 } 29 30 if (internalConstructor !== diplomatRuntime.internalConstructor) { 31 throw new Error("IsoDateTime is an out struct and can only be created internally."); 32 } 33 if ("date" in structObj) { 34 this.#date = structObj.date; 35 } else { 36 throw new Error("Missing required field date."); 37 } 38 39 if ("time" in structObj) { 40 this.#time = structObj.time; 41 } else { 42 throw new Error("Missing required field time."); 43 } 44 45 return this; 46 } 47 48 // Return this struct in FFI function friendly format. 49 // Returns an array that can be expanded with spread syntax (...) 50 _intoFFI( 51 functionCleanupArena, 52 appendArrayMap 53 ) { 54 return [this.#date.ffiValue, this.#time.ffiValue] 55 } 56 57 static _fromSuppliedValue(internalConstructor, obj) { 58 if (internalConstructor !== diplomatRuntime.internalConstructor) { 59 throw new Error("_fromSuppliedValue cannot be called externally."); 60 } 61 62 if (obj instanceof IsoDateTime) { 63 return obj; 64 } 65 66 return IsoDateTime.fromFields(obj); 67 } 68 69 _writeToArrayBuffer( 70 arrayBuffer, 71 offset, 72 functionCleanupArena, 73 appendArrayMap 74 ) { 75 diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, this.#date.ffiValue, Uint32Array); 76 diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 4, this.#time.ffiValue, Uint32Array); 77 } 78 79 // This struct contains borrowed fields, so this takes in a list of 80 // "edges" corresponding to where each lifetime's data may have been borrowed from 81 // and passes it down to individual fields containing the borrow. 82 // This method does not attempt to handle any dependencies between lifetimes, the caller 83 // should handle this when constructing edge arrays. 84 static _fromFFI(internalConstructor, ptr) { 85 if (internalConstructor !== diplomatRuntime.internalConstructor) { 86 throw new Error("IsoDateTime._fromFFI is not meant to be called externally. Please use the default constructor."); 87 } 88 let structObj = {}; 89 const dateDeref = diplomatRuntime.ptrRead(wasm, ptr); 90 structObj.date = new IsoDate(diplomatRuntime.internalConstructor, dateDeref, []); 91 const timeDeref = diplomatRuntime.ptrRead(wasm, ptr + 4); 92 structObj.time = new Time(diplomatRuntime.internalConstructor, timeDeref, []); 93 94 return new IsoDateTime(structObj, internalConstructor); 95 } 96 97 98 /** 99 * Creates a new [`IsoDateTime`] from an IXDTF string. 100 * 101 * See the [Rust documentation for `try_from_str`](https://docs.rs/icu/latest/icu/time/struct.DateTime.html#method.try_from_str) for more information. 102 */ 103 static fromString(v) { 104 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 105 106 const vSlice = diplomatRuntime.DiplomatBuf.str8(wasm, v); 107 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 9, 4, true); 108 109 110 const result = wasm.icu4x_IsoDateTime_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat()); 111 112 try { 113 if (!diplomatReceive.resultFlag) { 114 const cause = new Rfc9557ParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 115 throw new globalThis.Error('Rfc9557ParseError: ' + cause.value, { cause }); 116 } 117 return IsoDateTime._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 118 } 119 120 finally { 121 functionCleanupArena.free(); 122 123 diplomatReceive.free(); 124 } 125 } 126 127 constructor(structObj, internalConstructor) { 128 return this.#internalConstructor(...arguments) 129 } 130 }