docspell/modules/webapp/src/main/elm/Data/Validated.elm

138 lines
2.3 KiB
Elm
Raw Normal View History

2021-07-04 08:24:49 +00:00
{-
2021-07-25 12:00:11 +00:00
Copyright 2020 Docspell Contributors
2021-07-04 08:24:49 +00:00
2021-07-25 12:00:11 +00:00
SPDX-License-Identifier: GPL-3.0-or-later
2021-07-04 08:24:49 +00:00
-}
2021-07-25 12:00:11 +00:00
2020-04-20 22:39:39 +00:00
module Data.Validated exposing
( Validated(..)
2020-08-28 20:17:49 +00:00
, isInvalid
2020-04-20 22:39:39 +00:00
, map
, map2
, map3
, map4
2021-04-17 09:14:29 +00:00
, toResult
2020-04-20 22:39:39 +00:00
, value
)
2020-04-20 20:53:08 +00:00
2021-04-17 09:14:29 +00:00
-- TODO Remove this, use Result
2020-04-20 20:53:08 +00:00
type Validated a
= Valid a
2020-04-20 22:39:39 +00:00
| Invalid (List String) a
2020-04-20 20:53:08 +00:00
| Unknown a
2021-04-17 09:14:29 +00:00
toResult : Validated a -> Result String a
toResult va =
case va of
Valid a ->
Ok a
Invalid errs _ ->
Err (String.join ", " errs)
Unknown a ->
Ok a
2020-08-28 20:17:49 +00:00
isInvalid : Validated a -> Bool
isInvalid v =
case v of
Valid _ ->
False
Invalid _ _ ->
True
Unknown _ ->
False
2020-04-20 20:53:08 +00:00
value : Validated a -> a
value va =
case va of
Valid a ->
a
2020-04-20 22:39:39 +00:00
Invalid _ a ->
2020-04-20 20:53:08 +00:00
a
Unknown a ->
a
2020-04-20 22:39:39 +00:00
map : (a -> b) -> Validated a -> Validated b
map f va =
case va of
Valid a ->
Valid (f a)
Invalid em a ->
Invalid em (f a)
Unknown a ->
Unknown (f a)
map2 : (a -> b -> c) -> Validated a -> Validated b -> Validated c
map2 f va vb =
case ( va, vb ) of
( Valid a, Valid b ) ->
Valid (f a b)
( Valid a, Invalid em b ) ->
Invalid em (f a b)
( Valid a, Unknown b ) ->
Unknown (f a b)
( Invalid em a, Valid b ) ->
Invalid em (f a b)
( Invalid em1 a, Invalid em2 b ) ->
Invalid (em1 ++ em2) (f a b)
( Invalid em a, Unknown b ) ->
Invalid em (f a b)
( Unknown a, Valid b ) ->
Unknown (f a b)
( Unknown a, Invalid em b ) ->
Invalid em (f a b)
( Unknown a, Unknown b ) ->
Unknown (f a b)
map3 :
(a -> b -> c -> d)
-> Validated a
-> Validated b
-> Validated c
-> Validated d
map3 f va vb vc =
let
vab =
map2 (\e1 -> \e2 -> f e1 e2) va vb
in
map2 (\g -> \e3 -> g e3) vab vc
map4 :
(a -> b -> c -> d -> e)
-> Validated a
-> Validated b
-> Validated c
-> Validated d
-> Validated e
map4 f va vb vc vd =
let
vabc =
map3 (\e1 -> \e2 -> \e3 -> f e1 e2 e3) va vb vc
in
map2 (\g -> \e4 -> g e4) vabc vd