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