Time.mjs (7366B)
1 // generated by diplomat-tool 2 import { CalendarError } from "./CalendarError.mjs" 3 import { Rfc9557ParseError } from "./Rfc9557ParseError.mjs" 4 import wasm from "./diplomat-wasm.mjs"; 5 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 6 7 8 /** 9 * An ICU4X Time object representing a time in terms of hour, minute, second, nanosecond 10 * 11 * See the [Rust documentation for `Time`](https://docs.rs/icu/latest/icu/time/struct.Time.html) for more information. 12 */ 13 const Time_box_destroy_registry = new FinalizationRegistry((ptr) => { 14 wasm.icu4x_Time_destroy_mv1(ptr); 15 }); 16 17 export class Time { 18 // Internal ptr reference: 19 #ptr = null; 20 21 // Lifetimes are only to keep dependencies alive. 22 // Since JS won't garbage collect until there are no incoming edges. 23 #selfEdge = []; 24 25 #internalConstructor(symbol, ptr, selfEdge) { 26 if (symbol !== diplomatRuntime.internalConstructor) { 27 console.error("Time is an Opaque type. You cannot call its constructor."); 28 return; 29 } 30 this.#ptr = ptr; 31 this.#selfEdge = selfEdge; 32 33 // Are we being borrowed? If not, we can register. 34 if (this.#selfEdge.length === 0) { 35 Time_box_destroy_registry.register(this, this.#ptr); 36 } 37 38 return this; 39 } 40 get ffiValue() { 41 return this.#ptr; 42 } 43 44 45 /** 46 * Creates a new [`Time`] given field values 47 * 48 * See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/time/struct.Time.html#method.try_new) for more information. 49 */ 50 #defaultConstructor(hour, minute, second, subsecond) { 51 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 52 53 54 const result = wasm.icu4x_Time_create_mv1(diplomatReceive.buffer, hour, minute, second, subsecond); 55 56 try { 57 if (!diplomatReceive.resultFlag) { 58 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 59 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 60 } 61 return new Time(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 62 } 63 64 finally { 65 diplomatReceive.free(); 66 } 67 } 68 69 /** 70 * Creates a new [`Time`] from an IXDTF string. 71 * 72 * See the [Rust documentation for `try_from_str`](https://docs.rs/icu/latest/icu/time/struct.Time.html#method.try_from_str) for more information. 73 */ 74 static fromString(v) { 75 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 76 77 const vSlice = diplomatRuntime.DiplomatBuf.str8(wasm, v); 78 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 79 80 81 const result = wasm.icu4x_Time_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat()); 82 83 try { 84 if (!diplomatReceive.resultFlag) { 85 const cause = new Rfc9557ParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 86 throw new globalThis.Error('Rfc9557ParseError: ' + cause.value, { cause }); 87 } 88 return new Time(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 89 } 90 91 finally { 92 functionCleanupArena.free(); 93 94 diplomatReceive.free(); 95 } 96 } 97 98 /** 99 * Creates a new [`Time`] representing the start of the day (00:00:00.000). 100 * 101 * See the [Rust documentation for `start_of_day`](https://docs.rs/icu/latest/icu/time/struct.Time.html#method.start_of_day) for more information. 102 */ 103 static startOfDay() { 104 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 105 106 107 const result = wasm.icu4x_Time_start_of_day_mv1(diplomatReceive.buffer); 108 109 try { 110 if (!diplomatReceive.resultFlag) { 111 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 112 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 113 } 114 return new Time(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 115 } 116 117 finally { 118 diplomatReceive.free(); 119 } 120 } 121 122 /** 123 * Creates a new [`Time`] representing noon (12:00:00.000). 124 * 125 * See the [Rust documentation for `noon`](https://docs.rs/icu/latest/icu/time/struct.Time.html#method.noon) for more information. 126 */ 127 static noon() { 128 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 129 130 131 const result = wasm.icu4x_Time_noon_mv1(diplomatReceive.buffer); 132 133 try { 134 if (!diplomatReceive.resultFlag) { 135 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 136 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 137 } 138 return new Time(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 139 } 140 141 finally { 142 diplomatReceive.free(); 143 } 144 } 145 146 /** 147 * Returns the hour in this time 148 * 149 * See the [Rust documentation for `hour`](https://docs.rs/icu/latest/icu/time/struct.Time.html#structfield.hour) for more information. 150 */ 151 get hour() { 152 153 const result = wasm.icu4x_Time_hour_mv1(this.ffiValue); 154 155 try { 156 return result; 157 } 158 159 finally { 160 } 161 } 162 163 /** 164 * Returns the minute in this time 165 * 166 * See the [Rust documentation for `minute`](https://docs.rs/icu/latest/icu/time/struct.Time.html#structfield.minute) for more information. 167 */ 168 get minute() { 169 170 const result = wasm.icu4x_Time_minute_mv1(this.ffiValue); 171 172 try { 173 return result; 174 } 175 176 finally { 177 } 178 } 179 180 /** 181 * Returns the second in this time 182 * 183 * See the [Rust documentation for `second`](https://docs.rs/icu/latest/icu/time/struct.Time.html#structfield.second) for more information. 184 */ 185 get second() { 186 187 const result = wasm.icu4x_Time_second_mv1(this.ffiValue); 188 189 try { 190 return result; 191 } 192 193 finally { 194 } 195 } 196 197 /** 198 * Returns the subsecond in this time as nanoseconds 199 * 200 * See the [Rust documentation for `subsecond`](https://docs.rs/icu/latest/icu/time/struct.Time.html#structfield.subsecond) for more information. 201 */ 202 get subsecond() { 203 204 const result = wasm.icu4x_Time_subsecond_mv1(this.ffiValue); 205 206 try { 207 return result; 208 } 209 210 finally { 211 } 212 } 213 214 constructor(hour, minute, second, subsecond) { 215 if (arguments[0] === diplomatRuntime.exposeConstructor) { 216 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 217 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 218 return this.#internalConstructor(...arguments); 219 } else { 220 return this.#defaultConstructor(...arguments); 221 } 222 } 223 }