178 lines
5.5 KiB
TypeScript
178 lines
5.5 KiB
TypeScript
import * as _ from "@preserves/core";
|
|
import * as _i_rewrite from "./rewrite";
|
|
|
|
export const $assert = Symbol.for("assert");
|
|
export const $message = Symbol.for("message");
|
|
export const $or = Symbol.for("or");
|
|
export const $ref = Symbol.for("ref");
|
|
export const $retract = Symbol.for("retract");
|
|
export const $sync = Symbol.for("sync");
|
|
|
|
export const SturdyRef = _.Record.makeConstructor<{"_field0": _val, "_field1": Array<Caveat>, "_field2": _.Bytes}, _ptr>()($ref, ["_field0","_field1","_field2"]);
|
|
|
|
export type SturdyRef = _.Record<(typeof $ref), [_val, Array<Caveat>, _.Bytes], _ptr>;
|
|
|
|
export type Caveat = (_i_rewrite.Rewrite | _.Record<(typeof $or), [Array<_i_rewrite.Rewrite>], _ptr>);
|
|
|
|
export type Handle = number;
|
|
|
|
export type Event = (Assert | Retract | Message | Sync);
|
|
|
|
export type Oid = number;
|
|
|
|
export type Turn = Array<[Oid, Event]>;
|
|
|
|
export const Assert = _.Record.makeConstructor<{"assertion": _val, "handle": Handle}, _ptr>()($assert, ["assertion","handle"]);
|
|
|
|
export type Assert = _.Record<(typeof $assert), [_val, Handle], _ptr>;
|
|
|
|
export const Retract = _.Record.makeConstructor<{"handle": Handle}, _ptr>()($retract, ["handle"]);
|
|
|
|
export type Retract = _.Record<(typeof $retract), [Handle], _ptr>;
|
|
|
|
export const Message = _.Record.makeConstructor<{"body": _val}, _ptr>()($message, ["body"]);
|
|
|
|
export type Message = _.Record<(typeof $message), [_val], _ptr>;
|
|
|
|
export const Sync = _.Record.makeConstructor<{"peer": _ptr}, _ptr>()($sync, ["peer"]);
|
|
|
|
export type Sync = _.Record<(typeof $sync), [_ptr], _ptr>;
|
|
|
|
export type _ptr = never;
|
|
|
|
export type _val = _.Value<_ptr>;
|
|
|
|
|
|
export function isSturdyRef(v: any): v is SturdyRef {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $ref) &&
|
|
(
|
|
(v.length === 3) &&
|
|
true &&
|
|
(
|
|
_.Array.isArray(v[1]) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[1]) &&
|
|
(v[1].length >= 0) &&
|
|
v[1].slice(0).every(v => (isCaveat(v)))
|
|
) &&
|
|
_.Bytes.isBytes(v[2])
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asSturdyRef(v: any): SturdyRef {
|
|
if (!isSturdyRef(v)) {throw new TypeError(`Invalid SturdyRef: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isCaveat(v: any): v is Caveat {
|
|
return (
|
|
_i_rewrite.isRewrite(v) ||
|
|
(
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $or) &&
|
|
(
|
|
(v.length === 1) &&
|
|
(
|
|
_.Array.isArray(v[0]) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v[0]) &&
|
|
(v[0].length >= 0) &&
|
|
v[0].slice(0).every(v => (_i_rewrite.isRewrite(v)))
|
|
)
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
export function asCaveat(v: any): Caveat {
|
|
if (!isCaveat(v)) {throw new TypeError(`Invalid Caveat: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isHandle(v: any): v is Handle {return typeof v === 'number';}
|
|
|
|
export function asHandle(v: any): Handle {
|
|
if (!isHandle(v)) {throw new TypeError(`Invalid Handle: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isEvent(v: any): v is Event {return (isAssert(v) || isRetract(v) || isMessage(v) || isSync(v));}
|
|
|
|
export function asEvent(v: any): Event {
|
|
if (!isEvent(v)) {throw new TypeError(`Invalid Event: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isOid(v: any): v is Oid {return typeof v === 'number';}
|
|
|
|
export function asOid(v: any): Oid {
|
|
if (!isOid(v)) {throw new TypeError(`Invalid Oid: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isTurn(v: any): v is Turn {
|
|
return (
|
|
_.Array.isArray(v) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
(v.length >= 0) &&
|
|
v.slice(0).every(v => (
|
|
(
|
|
_.Array.isArray(v) &&
|
|
!_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
(v.length === 2) &&
|
|
isOid(v[0]) &&
|
|
isEvent(v[1])
|
|
)
|
|
))
|
|
);
|
|
}
|
|
|
|
export function asTurn(v: any): Turn {
|
|
if (!isTurn(v)) {throw new TypeError(`Invalid Turn: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isAssert(v: any): v is Assert {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $assert) &&
|
|
((v.length === 2) && true && isHandle(v[1]))
|
|
);
|
|
}
|
|
|
|
export function asAssert(v: any): Assert {
|
|
if (!isAssert(v)) {throw new TypeError(`Invalid Assert: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isRetract(v: any): v is Retract {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $retract) &&
|
|
((v.length === 1) && isHandle(v[0]))
|
|
);
|
|
}
|
|
|
|
export function asRetract(v: any): Retract {
|
|
if (!isRetract(v)) {throw new TypeError(`Invalid Retract: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isMessage(v: any): v is Message {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $message) &&
|
|
((v.length === 1) && true)
|
|
);
|
|
}
|
|
|
|
export function asMessage(v: any): Message {
|
|
if (!isMessage(v)) {throw new TypeError(`Invalid Message: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|
|
export function isSync(v: any): v is Sync {
|
|
return (
|
|
_.Record.isRecord<_val, _.Tuple<_val>, _ptr>(v) &&
|
|
_.is(v.label, $sync) &&
|
|
((v.length === 1) && _.isPointer(v[0]))
|
|
);
|
|
}
|
|
|
|
export function asSync(v: any): Sync {
|
|
if (!isSync(v)) {throw new TypeError(`Invalid Sync: ${_.stringify(v)}`);} else {return v;};
|
|
}
|
|
|