IanaParser.mjs (4627B)
1 // generated by diplomat-tool 2 import { DataError } from "./DataError.mjs" 3 import { DataProvider } from "./DataProvider.mjs" 4 import { TimeZone } from "./TimeZone.mjs" 5 import { TimeZoneIterator } from "./TimeZoneIterator.mjs" 6 import wasm from "./diplomat-wasm.mjs"; 7 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 8 9 10 /** 11 * A mapper between IANA time zone identifiers and BCP-47 time zone identifiers. 12 * 13 * This mapper supports two-way mapping, but it is optimized for the case of IANA to BCP-47. 14 * It also supports normalizing and canonicalizing the IANA strings. 15 * 16 * See the [Rust documentation for `IanaParser`](https://docs.rs/icu/latest/icu/time/zone/iana/struct.IanaParser.html) for more information. 17 */ 18 const IanaParser_box_destroy_registry = new FinalizationRegistry((ptr) => { 19 wasm.icu4x_IanaParser_destroy_mv1(ptr); 20 }); 21 22 export class IanaParser { 23 // Internal ptr reference: 24 #ptr = null; 25 26 // Lifetimes are only to keep dependencies alive. 27 // Since JS won't garbage collect until there are no incoming edges. 28 #selfEdge = []; 29 30 #internalConstructor(symbol, ptr, selfEdge) { 31 if (symbol !== diplomatRuntime.internalConstructor) { 32 console.error("IanaParser is an Opaque type. You cannot call its constructor."); 33 return; 34 } 35 this.#ptr = ptr; 36 this.#selfEdge = selfEdge; 37 38 // Are we being borrowed? If not, we can register. 39 if (this.#selfEdge.length === 0) { 40 IanaParser_box_destroy_registry.register(this, this.#ptr); 41 } 42 43 return this; 44 } 45 get ffiValue() { 46 return this.#ptr; 47 } 48 49 50 /** 51 * Create a new [`IanaParser`] using compiled data 52 * 53 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/time/zone/iana/struct.IanaParser.html#method.new) for more information. 54 */ 55 #defaultConstructor() { 56 57 const result = wasm.icu4x_IanaParser_create_mv1(); 58 59 try { 60 return new IanaParser(diplomatRuntime.internalConstructor, result, []); 61 } 62 63 finally { 64 } 65 } 66 67 /** 68 * Create a new [`IanaParser`] using a particular data source 69 * 70 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/time/zone/iana/struct.IanaParser.html#method.new) for more information. 71 */ 72 static createWithProvider(provider) { 73 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 74 75 76 const result = wasm.icu4x_IanaParser_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 77 78 try { 79 if (!diplomatReceive.resultFlag) { 80 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 81 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 82 } 83 return new IanaParser(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 84 } 85 86 finally { 87 diplomatReceive.free(); 88 } 89 } 90 91 /** 92 * See the [Rust documentation for `parse`](https://docs.rs/icu/latest/icu/time/zone/iana/struct.IanaParserBorrowed.html#method.parse) for more information. 93 */ 94 parse(value) { 95 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 96 97 const valueSlice = diplomatRuntime.DiplomatBuf.str8(wasm, value); 98 99 const result = wasm.icu4x_IanaParser_parse_mv1(this.ffiValue, ...valueSlice.splat()); 100 101 try { 102 return new TimeZone(diplomatRuntime.internalConstructor, result, []); 103 } 104 105 finally { 106 functionCleanupArena.free(); 107 108 } 109 } 110 111 /** 112 * See the [Rust documentation for `iter`](https://docs.rs/icu/latest/icu/time/zone/iana/struct.IanaParserBorrowed.html#method.iter) for more information. 113 */ 114 iter() { 115 // This lifetime edge depends on lifetimes 'a 116 let aEdges = [this]; 117 118 119 const result = wasm.icu4x_IanaParser_iter_mv1(this.ffiValue); 120 121 try { 122 return new TimeZoneIterator(diplomatRuntime.internalConstructor, result, [], aEdges); 123 } 124 125 finally { 126 } 127 } 128 129 constructor() { 130 if (arguments[0] === diplomatRuntime.exposeConstructor) { 131 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 132 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 133 return this.#internalConstructor(...arguments); 134 } else { 135 return this.#defaultConstructor(...arguments); 136 } 137 } 138 }