Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | import { itMap, itRange, itToArr, itToMap } from '../utils.js'; |
| 19 | |
| 20 | enum Bool { |
| 21 | False = 'false', |
| 22 | True = 'true', |
| 23 | } |
| 24 | |
| 25 | interface SwigVec<T> { |
| 26 | size(): number; |
| 27 | get(i: number): T; // TODO: | undefined; |
| 28 | } |
| 29 | |
| 30 | interface SwigMap<T, U> { |
| 31 | keys(): SwigVec<T>; |
| 32 | get(k: T): U; // TODO: | undefined; |
| 33 | set(k: T, v: U): void; |
| 34 | } |
| 35 | |
| 36 | const swigVecToIt = <T>(v: SwigVec<T>) => itMap(itRange(0, v.size()), (i) => v.get(i)); |
| 37 | const swigMapToIt = <T, U>(m: SwigMap<T, U>) => itMap(swigVecToIt(m.keys()), (k): [T, U] => [k, m.get(k)]); |
| 38 | |
| 39 | // type IntVect = SwigVec<number>; |
| 40 | // type UintVect = SwigVec<number>; |
| 41 | // type FloatVect = SwigVec<number>; |
| 42 | export type StringVect = SwigVec<string>; |
| 43 | // type IntegerMap = SwigMap<string, number>; |
| 44 | export type StringMap = SwigMap<string, string>; |
| 45 | // type VectMap = SwigVec<StringMap>; |
| 46 | // type Blob = SwigVec<number>; |
| 47 | |
| 48 | export const stringVectToArr = (sv: StringVect) => itToArr(swigVecToIt(sv)); |
| 49 | export const stringMapToMap = (sm: StringMap) => itToMap(swigMapToIt(sm)); |
| 50 | // const vectMapToJs = (vm: VectMap) => itToArr(itMap(swigVecToIt(vm), stringMapToMap)); |
| 51 | |
| 52 | export interface JamiSwig { |
| 53 | init(args: Record<string, unknown>): void; |
| 54 | |
| 55 | // IntVect(): IntVect; |
| 56 | // UintVect(): UintVect; |
| 57 | // FloatVect(): FloatVect; |
| 58 | // StringVect(): StringVect; |
| 59 | // IntegerMap(): IntegerMap |
| 60 | // StringMap(): StringMap; |
| 61 | // VectMap(): VectMap; |
| 62 | // IntegerMap(): IntegerMap; |
| 63 | |
| 64 | addAccount(details: StringMap): string; |
| 65 | removeAccount(id: string): void; |
| 66 | |
| 67 | getAccountList(): StringVect; |
| 68 | |
| 69 | registerName(id: string, password: string, username: string): boolean; |
| 70 | lookupName(id: string, nameserver: string, username: string): boolean; |
| 71 | lookupAddress(id: string, nameserver: string, address: string): boolean; |
| 72 | |
| 73 | getAccountDetails(id: string): StringMap; |
| 74 | setAccountDetails(id: string, details: StringMap): void; |
| 75 | setAccountActive(id: string, active: Bool): void; |
| 76 | } |