• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { DataError } from "./DataError.mjs"
3import { DataProvider } from "./DataProvider.mjs"
4import { Locale } from "./Locale.mjs"
5import { TitlecaseOptions } from "./TitlecaseOptions.mjs"
6import wasm from "./diplomat-wasm.mjs";
7import * as diplomatRuntime from "./diplomat-runtime.mjs";
8
9
10/** See the [Rust documentation for `TitlecaseMapper`](https://docs.rs/icu/latest/icu/casemap/struct.TitlecaseMapper.html) for more information.
11*/
12const TitlecaseMapper_box_destroy_registry = new FinalizationRegistry((ptr) => {
13    wasm.icu4x_TitlecaseMapper_destroy_mv1(ptr);
14});
15
16export class TitlecaseMapper {
17
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("TitlecaseMapper is an Opaque type. You cannot call its constructor.");
28            return;
29        }
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            TitlecaseMapper_box_destroy_registry.register(this, this.#ptr);
37        }
38
39        return this;
40    }
41    get ffiValue() {
42        return this.#ptr;
43    }
44
45    #defaultConstructor() {
46        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
47
48        const result = wasm.icu4x_TitlecaseMapper_create_mv1(diplomatReceive.buffer);
49
50        try {
51            if (!diplomatReceive.resultFlag) {
52                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
53                throw new globalThis.Error('DataError: ' + cause.value, { cause });
54            }
55            return new TitlecaseMapper(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
56        }
57
58        finally {
59            diplomatReceive.free();
60        }
61    }
62
63    static createWithProvider(provider) {
64        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true);
65
66        const result = wasm.icu4x_TitlecaseMapper_create_with_provider_mv1(diplomatReceive.buffer, provider.ffiValue);
67
68        try {
69            if (!diplomatReceive.resultFlag) {
70                const cause = new DataError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
71                throw new globalThis.Error('DataError: ' + cause.value, { cause });
72            }
73            return new TitlecaseMapper(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []);
74        }
75
76        finally {
77            diplomatReceive.free();
78        }
79    }
80
81    titlecaseSegment(s, locale, options) {
82        let functionCleanupArena = new diplomatRuntime.CleanupArena();
83
84        const sSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, s));
85
86        const write = new diplomatRuntime.DiplomatWriteBuf(wasm);
87        wasm.icu4x_TitlecaseMapper_titlecase_segment_v1_mv1(this.ffiValue, ...sSlice.splat(), locale.ffiValue, ...TitlecaseOptions._fromSuppliedValue(diplomatRuntime.internalConstructor, options)._intoFFI(functionCleanupArena, {}), write.buffer);
88
89        try {
90            return write.readString8();
91        }
92
93        finally {
94            functionCleanupArena.free();
95
96            write.free();
97        }
98    }
99
100    constructor() {
101        if (arguments[0] === diplomatRuntime.exposeConstructor) {
102            return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1));
103        } else if (arguments[0] === diplomatRuntime.internalConstructor) {
104            return this.#internalConstructor(...arguments);
105        } else {
106            return this.#defaultConstructor(...arguments);
107        }
108    }
109}