diff --git a/README.md b/README.md index 20c9943..76c1a23 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ # purescript-yoga-variation + diff --git a/spago.dhall b/spago.dhall index d3e577a..baf03b0 100644 --- a/spago.dhall +++ b/spago.dhall @@ -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" ] } diff --git a/src/Yoga/Variation/Variation.purs b/src/Yoga/Variation/Variation.purs index f16dc8b..c613f40 100644 --- a/src/Yoga/Variation/Variation.purs +++ b/src/Yoga/Variation/Variation.purs @@ -1,19 +1,28 @@ 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 @@ -21,14 +30,46 @@ type Validated err result = Validation.V (NonEmptyArray (Variant.Variant err)) r 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 - ∷ ∀ (t63 ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row Type) + ∷ ∀ (result ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row 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 + ∷ ∀ (result ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row 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 + :: ∀ (result ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row 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)