/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-return */ import * as B from "effect/Brand" import type * as Option from "effect/Option" import type * as Result from "effect/Result" import * as S from "effect/Schema" export interface Constructor> { /** * Constructs a branded type from a value of type `A`, throwing an error if * the provided `A` is not valid. */ (args: Unbranded): A /** * Constructs a branded type from a value of type `A`, returning `Some` * if the provided `A` is valid, `None` otherwise. */ option(args: Unbranded): Option.Option /** * Constructs a branded type from a value of type `A`, returning `Result.succeed` * if the provided `A` is valid, `Result.fail` otherwise. */ result(args: Unbranded): Result.Result /** * Attempts to refine the provided value of type `A`, returning `true` if * the provided `A` is valid, `false` otherwise. */ is(a: Unbranded): a is Unbranded & A } type BrandAnnotations> = & S.Annotations.Filter & ( C extends string ? { readonly toArbitrary?: S.Annotations.ToArbitrary.Declaration } : {} ) export interface BrandedSchema> extends S.Bottom< C, S["Encoded"], S["DecodingServices"], S["EncodingServices"], S["ast"], BrandedSchema, S["~type.make.in"], S["Iso"], S["~type.parameters"], C, S["~type.mutability"], S["~type.optionality"], S["~type.constructor.default"], S["~encoded.mutability"], S["~encoded.optionality"] > {} export const fromBrand = >( constructor: Constructor, options?: BrandAnnotations ) => (self: Self): BrandedSchema => { const branded = S.fromBrand(options?.identifier ?? "Brand", constructor as any)(self as any) return options ? (branded as any).pipe(S.annotate(options)) : branded as any } export type Brands

= P extends B.Brand ? B.Brand.Brands

: never export type Unbranded

= P extends B.Brand ? B.Brand.Unbranded

: P export const nominal: >() => Constructor = < A extends B.Brand >(): Constructor< A > => B.nominal() as any