All files / src/codec schema.ts

100% Statements 19/19
100% Branches 6/6
100% Functions 5/5
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 331x   1x             1x 6x 6x 6x 6x   1x 5x 5x   5x   1x 5x 5x   5x   1x 6x   1x 6x  
import {type Branch, type Leaf, type Tree} from '#tree'
import type {BranchF, LeafF} from '#treeF'
import {Schema} from 'effect'
 
/**
 * Returns an Effect schema for a tree of `A` when given a schema for A.
 * @category codec
 * @function
 */
export const treeSchema = <A>(node: Schema.Schema<A>): Schema.Schema<Tree<A>> =>
  Schema.Union(
    Schema.suspend(() => branchSchema(node, treeSchema(node))),
    leafSchema(node),
  )
 
const branchSchema = <A>(
  node: Schema.Schema<A>,
  forest: Schema.Schema<Tree<A>>,
): Schema.Schema<Branch<A>> =>
  Schema.Struct({unfixed: branchFSchema(node, forest)})
 
const branchFSchema = <A, C>(
  node: Schema.Schema<A>,
  tree: Schema.Schema<C>,
): Schema.Schema<BranchF<A, C>> =>
  Schema.Struct({node, forest: Schema.NonEmptyArray(tree)})
 
const leafSchema = <A>(node: Schema.Schema<A>): Schema.Schema<Leaf<A>> =>
  Schema.Struct({unfixed: leafFSchema(node)})
 
const leafFSchema = <A>(node: Schema.Schema<A>): Schema.Schema<LeafF<A>> =>
  Schema.Struct({node})