42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import * as _ from "@preserves/core";
|
|
|
|
export const $Instance = Symbol.for("Instance");
|
|
|
|
export type _ptr = never;
|
|
|
|
export type _val = _.Value<_ptr>;
|
|
|
|
export type Instance = {"name": string, "argument": _val};
|
|
|
|
|
|
export const _toPtr = () => { throw new _.DecodeError("Pointers forbidden"); };
|
|
|
|
export function Instance(name: string, argument: _val): Instance {return {name, argument};}
|
|
|
|
export function asInstance(v: _val): Instance {
|
|
let result = toInstance(v);
|
|
if (result === void 0) throw new TypeError(`Invalid Instance: ${_.stringify(v)}`);
|
|
return result;
|
|
}
|
|
|
|
export function toInstance(v: _val): undefined | Instance {
|
|
let result: undefined | Instance;
|
|
if (_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v)) {
|
|
let _tmp0: (null) | undefined;
|
|
_tmp0 = _.is(v.label, $Instance) ? null : void 0;
|
|
if (_tmp0 !== void 0) {
|
|
let _tmp1: (string) | undefined;
|
|
_tmp1 = typeof v[0] === 'string' ? v[0] : void 0;
|
|
if (_tmp1 !== void 0) {
|
|
let _tmp2: (_val) | undefined;
|
|
_tmp2 = v[1];
|
|
if (_tmp2 !== void 0) {result = {"name": _tmp1, "argument": _tmp2};};
|
|
};
|
|
};
|
|
};
|
|
return result;
|
|
}
|
|
|
|
export function fromInstance(_v: Instance): _val {return _.Record($Instance, [_v["name"], _v["argument"]]);}
|
|
|