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