Skip to content

Commit

Permalink
Add helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma-andex committed Jan 14, 2023
1 parent 50ffacd commit 53e67c2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# purescript-yoga-variation

10 changes: 9 additions & 1 deletion spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ to generate this file without the comments in this block.
-}
{ name = "my-project"
, dependencies =
[ "arrays", "console", "effect", "prelude", "validation", "variant" ]
[ "arrays"
, "bifunctors"
, "console"
, "effect"
, "either"
, "prelude"
, "validation"
, "variant"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
}
57 changes: 49 additions & 8 deletions src/Yoga/Variation/Variation.purs
Original file line number Diff line number Diff line change
@@ -1,34 +1,75 @@
module Yoga.Variation.Variation
( Validated
, fromEither
, invalid
, invalidM
, key
, mapErrors
, module Validation
, module Variant
, valid
)
where
, validM
) where

import Prelude

import Data.Array.NonEmpty (NonEmptyArray)
import Data.Array.NonEmpty as NonEmpty
import Data.Bifunctor (class Bifunctor, lmap)
import Data.Either (Either, either)
import Data.Symbol (class IsSymbol)
import Data.Validation.Semigroup as Validation
import Data.Variant as Variant
import Prim.Row (class Cons)
import Data.Validation.Semigroup as ValidationSG
import Data.Validation.Semigroup hiding (invalid) as Validation
import Data.Variant (class Contractable, class VariantBounded, class VariantBoundedEnums, class VariantEqs, class VariantMapCases, class VariantMatchCases, class VariantOrds, class VariantShows, class VariantTraverseCases, Unvariant(..), Unvariant', Variant, case_, contract, default, expand, inj, match, on, onMatch, over, overOne, overSome, prj, revariant, traverse, traverseOne, traverseSome, unvariant, variantBounded, variantBoundedEnums, variantEqs, variantOrds, variantShows) as Variant
import Prim.Row (class Cons, class Union)
import Prim.RowList (class RowToList)
import Type.Proxy (Proxy(..))

type Validated err result = Validation.V (NonEmptyArray (Variant.Variant err)) result

valid :: forall err result. result -> Validated err result
valid = pure

validM :: forall m err result. Applicative m => result -> m (Validated err result)
validM = valid >>> pure

invalid
(t63Type) (symSymbol) (errorType) (othersRow Type) (errorsRow Type)
(resultType) (symSymbol) (errorType) (othersRow Type) (errorsRow Type)
. Cons sym error others errors
IsSymbol sym
Proxy sym
error
Validation.V (NonEmptyArray (Variant.Variant errors)) t63
invalid proxy value = Validation.invalid $ NonEmpty.singleton $ Variant.inj proxy value
Validation.V (NonEmptyArray (Variant.Variant errors)) result
invalid proxy value = ValidationSG.invalid $ NonEmpty.singleton $ Variant.inj proxy value

invalidM
(resultType) (symSymbol) (errorType) (othersRow Type) (errorsRow Type) m
. Applicative m
=> Cons sym error others errors
IsSymbol sym
Proxy sym
error
m (Validation.V (NonEmptyArray (Variant.Variant errors)) result)
invalidM proxy = invalid proxy >>> pure

fromEither
:: (resultType) (symSymbol) (errorType) (othersRow Type) (errorsRow Type)
. Cons sym error others errors
IsSymbol sym
Proxy sym
-> Either error result
-> Validated errors result
fromEither proxy = either (invalid proxy) valid

key (sym Symbol). Proxy sym
key = Proxy

mapErrors
:: forall result handlersRl handlers errorsRemaining errorsOut errorsIn
. RowToList handlers handlersRl
=> Variant.VariantMatchCases handlersRl errorsRemaining (Variant.Variant errorsOut)
=> Union errorsRemaining errorsOut errorsIn
=> Record handlers
-> Validated errorsIn result
-> Validated errorsOut result
mapErrors handlers = lmap (map $ Variant.onMatch handlers identity)

0 comments on commit 53e67c2

Please sign in to comment.