MeasureUnitParser.mjs (4191B)
1 // generated by diplomat-tool 2 import { DataError } from "./DataError.mjs" 3 import { DataProvider } from "./DataProvider.mjs" 4 import { MeasureUnit } from "./MeasureUnit.mjs" 5 import wasm from "./diplomat-wasm.mjs"; 6 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 7 8 9 /** 10 * An ICU4X Measure Unit Parser object, capable of parsing the CLDR unit identifier (e.g. `meter-per-square-second`) and get the [`MeasureUnit`]. 11 * 12 * See the [Rust documentation for `MeasureUnitParser`](https://docs.rs/icu/latest/icu/experimental/measure/parser/struct.MeasureUnitParser.html) for more information. 13 */ 14 const MeasureUnitParser_box_destroy_registry = new FinalizationRegistry((ptr) => { 15 wasm.icu4x_MeasureUnitParser_destroy_mv1(ptr); 16 }); 17 18 export class MeasureUnitParser { 19 // Internal ptr reference: 20 #ptr = null; 21 22 // Lifetimes are only to keep dependencies alive. 23 // Since JS won't garbage collect until there are no incoming edges. 24 #selfEdge = []; 25 26 #internalConstructor(symbol, ptr, selfEdge) { 27 if (symbol !== diplomatRuntime.internalConstructor) { 28 console.error("MeasureUnitParser is an Opaque type. You cannot call its constructor."); 29 return; 30 } 31 this.#ptr = ptr; 32 this.#selfEdge = selfEdge; 33 34 // Are we being borrowed? If not, we can register. 35 if (this.#selfEdge.length === 0) { 36 MeasureUnitParser_box_destroy_registry.register(this, this.#ptr); 37 } 38 39 return this; 40 } 41 get ffiValue() { 42 return this.#ptr; 43 } 44 45 46 /** 47 * Construct a new [`MeasureUnitParser`] instance using compiled data. 48 * 49 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/experimental/measure/parser/struct.MeasureUnitParser.html#method.new) for more information. 50 */ 51 #defaultConstructor() { 52 53 const result = wasm.icu4x_MeasureUnitParser_create_mv1(); 54 55 try { 56 return new MeasureUnitParser(diplomatRuntime.internalConstructor, result, []); 57 } 58 59 finally { 60 } 61 } 62 63 /** 64 * Construct a new [`MeasureUnitParser`] instance using a particular data source. 65 * 66 * See the [Rust documentation for `new`](https://docs.rs/icu/latest/icu/experimental/measure/parser/struct.MeasureUnitParser.html#method.new) for more information. 67 */ 68 static createWithProvider(provider) { 69 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 70 71 72 const result = wasm.icu4x_MeasureUnitParser_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue); 73 74 try { 75 if (!diplomatReceive.resultFlag) { 76 const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 77 throw new globalThis.Error('DataError: ' + cause.value, { cause }); 78 } 79 return new MeasureUnitParser(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 80 } 81 82 finally { 83 diplomatReceive.free(); 84 } 85 } 86 87 /** 88 * See the [Rust documentation for `parse`](https://docs.rs/icu/latest/icu/experimental/measure/parser/struct.MeasureUnitParser.html#method.parse) for more information. 89 */ 90 parse(unitId) { 91 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 92 93 const unitIdSlice = diplomatRuntime.DiplomatBuf.str8(wasm, unitId); 94 95 const result = wasm.icu4x_MeasureUnitParser_parse_mv1(this.ffiValue, ...unitIdSlice.splat()); 96 97 try { 98 return result === 0 ? null : new MeasureUnit(diplomatRuntime.internalConstructor, result, []); 99 } 100 101 finally { 102 functionCleanupArena.free(); 103 104 } 105 } 106 107 constructor() { 108 if (arguments[0] === diplomatRuntime.exposeConstructor) { 109 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 110 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 111 return this.#internalConstructor(...arguments); 112 } else { 113 return this.#defaultConstructor(...arguments); 114 } 115 } 116 }