import * as svt from "simple-validation-tools"; import * as external from "./external"; import * as other from "./other"; export type Result = Success | Failure; export enum ResultTag { Success = "Success", Failure = "Failure", } export type Success = { type: ResultTag.Success; data: T; }; export type Failure = { type: ResultTag.Failure; data: E; }; export function Success(data: T): Success { return {type: ResultTag.Success, data}; } export function Failure(data: E): Failure { return {type: ResultTag.Failure, data}; } export function isResult(isT: svt.TypePredicate, isE: svt.TypePredicate): svt.TypePredicate> { return function isResultTE(value: unknown): value is Result { return [isSuccess(isT), isFailure(isE)].some((typePredicate) => typePredicate(value)); }; } export function isSuccess(isT: svt.TypePredicate): svt.TypePredicate> { return function isSuccessT(value: unknown): value is Success { return svt.isInterface>(value, {type: ResultTag.Success, data: isT}); }; } export function isFailure(isE: svt.TypePredicate): svt.TypePredicate> { return function isFailureE(value: unknown): value is Failure { return svt.isInterface>(value, {type: ResultTag.Failure, data: isE}); }; } export function validateResult(validateT: svt.Validator, validateE: svt.Validator): svt.Validator> { return function validateResultTE(value: unknown): svt.ValidationResult> { return svt.validateWithTypeTag>(value, {[ResultTag.Success]: validateSuccess(validateT), [ResultTag.Failure]: validateFailure(validateE)}, "type"); }; } export function validateSuccess(validateT: svt.Validator): svt.Validator> { return function validateSuccessT(value: unknown): svt.ValidationResult> { return svt.validate>(value, {type: ResultTag.Success, data: validateT}); }; } export function validateFailure(validateE: svt.Validator): svt.Validator> { return function validateFailureE(value: unknown): svt.ValidationResult> { return svt.validate>(value, {type: ResultTag.Failure, data: validateE}); }; } export type Holder = { value: T; }; export function isHolder(isT: svt.TypePredicate): svt.TypePredicate> { return function isHolderT(value: unknown): value is Holder { return svt.isInterface>(value, {value: isT}); }; } export function validateHolder(validateT: svt.Validator): svt.Validator> { return function validateHolderT(value: unknown): svt.ValidationResult> { return svt.validate>(value, {value: validateT}); }; } export type MaybeHolder = { value: external.Option; otherValue: other.Plain; }; export function isMaybeHolder(isT: svt.TypePredicate): svt.TypePredicate> { return function isMaybeHolderT(value: unknown): value is MaybeHolder { return svt.isInterface>(value, {value: external.isOption(isT), otherValue: other.isPlain}); }; } export function validateMaybeHolder(validateT: svt.Validator): svt.Validator> { return function validateMaybeHolderT(value: unknown): svt.ValidationResult> { return svt.validate>(value, {value: external.validateOption(validateT), otherValue: other.validatePlain}); }; } export type HasGenericEvent = PlainEvent | GenericEvent; export enum HasGenericEventTag { PlainEvent = "PlainEvent", GenericEvent = "GenericEvent", } export type PlainEvent = { type: HasGenericEventTag.PlainEvent; data: other.Plain; }; export type GenericEvent = { type: HasGenericEventTag.GenericEvent; data: external.Option; }; export function PlainEvent(data: other.Plain): PlainEvent { return {type: HasGenericEventTag.PlainEvent, data}; } export function GenericEvent(data: external.Option): GenericEvent { return {type: HasGenericEventTag.GenericEvent, data}; } export function isHasGenericEvent(isT: svt.TypePredicate): svt.TypePredicate> { return function isHasGenericEventT(value: unknown): value is HasGenericEvent { return [isPlainEvent, isGenericEvent(isT)].some((typePredicate) => typePredicate(value)); }; } export function isPlainEvent(value: unknown): value is PlainEvent { return svt.isInterface(value, {type: HasGenericEventTag.PlainEvent, data: other.isPlain}); } export function isGenericEvent(isT: svt.TypePredicate): svt.TypePredicate> { return function isGenericEventT(value: unknown): value is GenericEvent { return svt.isInterface>(value, {type: HasGenericEventTag.GenericEvent, data: external.isOption(isT)}); }; } export function validateHasGenericEvent(validateT: svt.Validator): svt.Validator> { return function validateHasGenericEventT(value: unknown): svt.ValidationResult> { return svt.validateWithTypeTag>(value, {[HasGenericEventTag.PlainEvent]: validatePlainEvent, [HasGenericEventTag.GenericEvent]: validateGenericEvent(validateT)}, "type"); }; } export function validatePlainEvent(value: unknown): svt.ValidationResult { return svt.validate(value, {type: HasGenericEventTag.PlainEvent, data: other.validatePlain}); } export function validateGenericEvent(validateT: svt.Validator): svt.Validator> { return function validateGenericEventT(value: unknown): svt.ValidationResult> { return svt.validate>(value, {type: HasGenericEventTag.GenericEvent, data: external.validateOption(validateT)}); }; }