@@ -0,0 +1,75 @@
1+module Yoga.Variation.Variation
2+ ( Validated
3+ , fromEither
4+ , invalid
5+ , invalidM
6+ , key
7+ , mapErrors
8+ , module Validation
9+ , module Variant
10+ , valid
11+ , validM
12+ ) where
13+
14+import Prelude
15+
16+import Data.Array.NonEmpty (NonEmptyArray)
17+import Data.Array.NonEmpty as NonEmpty
18+import Data.Bifunctor (class Bifunctor, lmap)
19+import Data.Either (Either, either)
20+import Data.Symbol (class IsSymbol)
21+import Data.Validation.Semigroup as ValidationSG
22+import Data.Validation.Semigroup hiding (invalid) as Validation
23+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
24+import Prim.Row (class Cons, class Union)
25+import Prim.RowList (class RowToList)
26+import Type.Proxy (Proxy(..))
27+
28+type Validated err result = Validation.V (NonEmptyArray (Variant.Variant err)) result
29+
30+valid :: forall err result. result -> Validated err result
31+valid = pure
32+
33+validM :: forall m err result. Applicative m => result -> m (Validated err result)
34+validM = valid >>> pure
35+
36+invalid
37+ ∷ ∀ (result ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row Type)
38+ . Cons sym error others errors
39+ ⇒ IsSymbol sym
40+ ⇒ Proxy sym
41+ → error
42+ → Validation.V (NonEmptyArray (Variant.Variant errors)) result
43+invalid proxy value = ValidationSG.invalid $ NonEmpty.singleton $ Variant.inj proxy value
44+
45+invalidM
46+ ∷ ∀ (result ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row Type) m
47+ . Applicative m
48+ => Cons sym error others errors
49+ ⇒ IsSymbol sym
50+ ⇒ Proxy sym
51+ → error
52+ → m (Validation.V (NonEmptyArray (Variant.Variant errors)) result)
53+invalidM proxy = invalid proxy >>> pure
54+
55+fromEither
56+ :: ∀ (result ∷ Type) (sym ∷ Symbol) (error ∷ Type) (others ∷ Row Type) (errors ∷ Row Type)
57+ . Cons sym error others errors
58+ ⇒ IsSymbol sym
59+ ⇒ Proxy sym
60+ -> Either error result
61+ -> Validated errors result
62+fromEither proxy = either (invalid proxy) valid
63+
64+key ∷ ∀ (sym ∷ Symbol). Proxy sym
65+key = Proxy
66+
67+mapErrors
68+ :: forall result handlersRl handlers errorsRemaining errorsOut errorsIn
69+ . RowToList handlers handlersRl
70+ => Variant.VariantMatchCases handlersRl errorsRemaining (Variant.Variant errorsOut)
71+ => Union errorsRemaining errorsOut errorsIn
72+ => Record handlers
73+ -> Validated errorsIn result
74+ -> Validated errorsOut result
75+mapErrors handlers = lmap (map $ Variant.onMatch handlers identity)