-- SPDX-FileCopyrightText: 2022 Oxhead Alpha
-- SPDX-License-Identifier: LicenseRef-MIT-OA

{-# OPTIONS_HADDOCK not-home #-}

-- | Internal helper types and functions for manipulating view sets
module Morley.Michelson.Internal.ViewsSet
  ( module Morley.Michelson.Internal.ViewsSet
  ) where

import Control.Monad.Except (throwError)
import Data.Aeson (FromJSON, ToJSON)
import Data.Coerce (coerce)
import Data.Default (Default(..))
import Data.List.NonEmpty qualified as NE (group)
import Data.Map qualified as Map
import Fmt (Buildable(..), (+|), (|+))

import Morley.Michelson.Internal.ViewName

-- | Type for intermediate coercions between typed and untyped view sets.
-- Intended as an internal helper.
newtype ViewsSetF a = ViewsSetF { forall a. ViewsSetF a -> Map ViewName a
unViewsSetF :: Map ViewName a }
  deriving newtype (Value -> Parser [ViewsSetF a]
Value -> Parser (ViewsSetF a)
(Value -> Parser (ViewsSetF a))
-> (Value -> Parser [ViewsSetF a]) -> FromJSON (ViewsSetF a)
forall a. FromJSON a => Value -> Parser [ViewsSetF a]
forall a. FromJSON a => Value -> Parser (ViewsSetF a)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: forall a. FromJSON a => Value -> Parser (ViewsSetF a)
parseJSON :: Value -> Parser (ViewsSetF a)
$cparseJSONList :: forall a. FromJSON a => Value -> Parser [ViewsSetF a]
parseJSONList :: Value -> Parser [ViewsSetF a]
FromJSON, [ViewsSetF a] -> Value
[ViewsSetF a] -> Encoding
ViewsSetF a -> Value
ViewsSetF a -> Encoding
(ViewsSetF a -> Value)
-> (ViewsSetF a -> Encoding)
-> ([ViewsSetF a] -> Value)
-> ([ViewsSetF a] -> Encoding)
-> ToJSON (ViewsSetF a)
forall a. ToJSON a => [ViewsSetF a] -> Value
forall a. ToJSON a => [ViewsSetF a] -> Encoding
forall a. ToJSON a => ViewsSetF a -> Value
forall a. ToJSON a => ViewsSetF a -> Encoding
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: forall a. ToJSON a => ViewsSetF a -> Value
toJSON :: ViewsSetF a -> Value
$ctoEncoding :: forall a. ToJSON a => ViewsSetF a -> Encoding
toEncoding :: ViewsSetF a -> Encoding
$ctoJSONList :: forall a. ToJSON a => [ViewsSetF a] -> Value
toJSONList :: [ViewsSetF a] -> Value
$ctoEncodingList :: forall a. ToJSON a => [ViewsSetF a] -> Encoding
toEncodingList :: [ViewsSetF a] -> Encoding
ToJSON, ViewsSetF a
ViewsSetF a -> Default (ViewsSetF a)
forall a. ViewsSetF a
forall a. a -> Default a
$cdef :: forall a. ViewsSetF a
def :: ViewsSetF a
Default, ViewsSetF a -> ()
(ViewsSetF a -> ()) -> NFData (ViewsSetF a)
forall a. NFData a => ViewsSetF a -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall a. NFData a => ViewsSetF a -> ()
rnf :: ViewsSetF a -> ()
NFData, Eq (Element (ViewsSetF a)) =>
Element (ViewsSetF a) -> ViewsSetF a -> Bool
Ord (Element (ViewsSetF a)) =>
ViewsSetF a -> Maybe (Element (ViewsSetF a))
Monoid (Element (ViewsSetF a)) =>
ViewsSetF a -> Element (ViewsSetF a)
(Element (ViewsSetF a) ~ Bool) => ViewsSetF a -> Bool
ViewsSetF a -> Bool
ViewsSetF a -> Int
ViewsSetF a -> [Element (ViewsSetF a)]
ViewsSetF a -> Maybe (Element (ViewsSetF a))
(Element (ViewsSetF a) -> Bool) -> ViewsSetF a -> Bool
(Element (ViewsSetF a) -> Bool)
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
(Element (ViewsSetF a)
 -> Element (ViewsSetF a) -> Element (ViewsSetF a))
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
(ViewsSetF a -> [Element (ViewsSetF a)])
-> (ViewsSetF a -> Bool)
-> (forall b.
    (Element (ViewsSetF a) -> b -> b) -> b -> ViewsSetF a -> b)
-> (forall b.
    (b -> Element (ViewsSetF a) -> b) -> b -> ViewsSetF a -> b)
-> (forall b.
    (b -> Element (ViewsSetF a) -> b) -> b -> ViewsSetF a -> b)
-> (ViewsSetF a -> Int)
-> (Eq (Element (ViewsSetF a)) =>
    Element (ViewsSetF a) -> ViewsSetF a -> Bool)
-> (forall m.
    Monoid m =>
    (Element (ViewsSetF a) -> m) -> ViewsSetF a -> m)
-> (Monoid (Element (ViewsSetF a)) =>
    ViewsSetF a -> Element (ViewsSetF a))
-> (forall b.
    (Element (ViewsSetF a) -> b -> b) -> b -> ViewsSetF a -> b)
-> (Eq (Element (ViewsSetF a)) =>
    Element (ViewsSetF a) -> ViewsSetF a -> Bool)
-> ((Element (ViewsSetF a) -> Bool) -> ViewsSetF a -> Bool)
-> ((Element (ViewsSetF a) -> Bool) -> ViewsSetF a -> Bool)
-> ((Element (ViewsSetF a) ~ Bool) => ViewsSetF a -> Bool)
-> ((Element (ViewsSetF a) ~ Bool) => ViewsSetF a -> Bool)
-> ((Element (ViewsSetF a) -> Bool)
    -> ViewsSetF a -> Maybe (Element (ViewsSetF a)))
-> (ViewsSetF a -> Maybe (Element (ViewsSetF a)))
-> (Ord (Element (ViewsSetF a)) =>
    ViewsSetF a -> Maybe (Element (ViewsSetF a)))
-> (Ord (Element (ViewsSetF a)) =>
    ViewsSetF a -> Maybe (Element (ViewsSetF a)))
-> ((Element (ViewsSetF a)
     -> Element (ViewsSetF a) -> Element (ViewsSetF a))
    -> ViewsSetF a -> Maybe (Element (ViewsSetF a)))
-> ((Element (ViewsSetF a)
     -> Element (ViewsSetF a) -> Element (ViewsSetF a))
    -> ViewsSetF a -> Maybe (Element (ViewsSetF a)))
-> Container (ViewsSetF a)
forall a.
Eq (Element (ViewsSetF a)) =>
Element (ViewsSetF a) -> ViewsSetF a -> Bool
forall a.
Ord (Element (ViewsSetF a)) =>
ViewsSetF a -> Maybe (Element (ViewsSetF a))
forall m.
Monoid m =>
(Element (ViewsSetF a) -> m) -> ViewsSetF a -> m
forall a.
Monoid (Element (ViewsSetF a)) =>
ViewsSetF a -> Element (ViewsSetF a)
forall a. (Element (ViewsSetF a) ~ Bool) => ViewsSetF a -> Bool
forall a. ViewsSetF a -> Bool
forall a. ViewsSetF a -> Int
forall a. ViewsSetF a -> [Element (ViewsSetF a)]
forall a. ViewsSetF a -> Maybe (Element (ViewsSetF a))
forall t.
(t -> [Element t])
-> (t -> Bool)
-> (forall b. (Element t -> b -> b) -> b -> t -> b)
-> (forall b. (b -> Element t -> b) -> b -> t -> b)
-> (forall b. (b -> Element t -> b) -> b -> t -> b)
-> (t -> Int)
-> (Eq (Element t) => Element t -> t -> Bool)
-> (forall m. Monoid m => (Element t -> m) -> t -> m)
-> (Monoid (Element t) => t -> Element t)
-> (forall b. (Element t -> b -> b) -> b -> t -> b)
-> (Eq (Element t) => Element t -> t -> Bool)
-> ((Element t -> Bool) -> t -> Bool)
-> ((Element t -> Bool) -> t -> Bool)
-> ((Element t ~ Bool) => t -> Bool)
-> ((Element t ~ Bool) => t -> Bool)
-> ((Element t -> Bool) -> t -> Maybe (Element t))
-> (t -> Maybe (Element t))
-> (Ord (Element t) => t -> Maybe (Element t))
-> (Ord (Element t) => t -> Maybe (Element t))
-> ((Element t -> Element t -> Element t)
    -> t -> Maybe (Element t))
-> ((Element t -> Element t -> Element t)
    -> t -> Maybe (Element t))
-> Container t
forall b.
(b -> Element (ViewsSetF a) -> b) -> b -> ViewsSetF a -> b
forall a. (Element (ViewsSetF a) -> Bool) -> ViewsSetF a -> Bool
forall a.
(Element (ViewsSetF a) -> Bool)
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
forall a.
(Element (ViewsSetF a)
 -> Element (ViewsSetF a) -> Element (ViewsSetF a))
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
forall b.
(Element (ViewsSetF a) -> b -> b) -> b -> ViewsSetF a -> b
forall a m.
Monoid m =>
(Element (ViewsSetF a) -> m) -> ViewsSetF a -> m
forall a b.
(b -> Element (ViewsSetF a) -> b) -> b -> ViewsSetF a -> b
forall a b.
(Element (ViewsSetF a) -> b -> b) -> b -> ViewsSetF a -> b
$ctoList :: forall a. ViewsSetF a -> [Element (ViewsSetF a)]
toList :: ViewsSetF a -> [Element (ViewsSetF a)]
$cnull :: forall a. ViewsSetF a -> Bool
null :: ViewsSetF a -> Bool
$cfoldr :: forall a b.
(Element (ViewsSetF a) -> b -> b) -> b -> ViewsSetF a -> b
foldr :: forall b.
(Element (ViewsSetF a) -> b -> b) -> b -> ViewsSetF a -> b
$cfoldl :: forall a b.
(b -> Element (ViewsSetF a) -> b) -> b -> ViewsSetF a -> b
foldl :: forall b.
(b -> Element (ViewsSetF a) -> b) -> b -> ViewsSetF a -> b
$cfoldl' :: forall a b.
(b -> Element (ViewsSetF a) -> b) -> b -> ViewsSetF a -> b
foldl' :: forall b.
(b -> Element (ViewsSetF a) -> b) -> b -> ViewsSetF a -> b
$clength :: forall a. ViewsSetF a -> Int
length :: ViewsSetF a -> Int
$celem :: forall a.
Eq (Element (ViewsSetF a)) =>
Element (ViewsSetF a) -> ViewsSetF a -> Bool
elem :: Eq (Element (ViewsSetF a)) =>
Element (ViewsSetF a) -> ViewsSetF a -> Bool
$cfoldMap :: forall a m.
Monoid m =>
(Element (ViewsSetF a) -> m) -> ViewsSetF a -> m
foldMap :: forall m.
Monoid m =>
(Element (ViewsSetF a) -> m) -> ViewsSetF a -> m
$cfold :: forall a.
Monoid (Element (ViewsSetF a)) =>
ViewsSetF a -> Element (ViewsSetF a)
fold :: Monoid (Element (ViewsSetF a)) =>
ViewsSetF a -> Element (ViewsSetF a)
$cfoldr' :: forall a b.
(Element (ViewsSetF a) -> b -> b) -> b -> ViewsSetF a -> b
foldr' :: forall b.
(Element (ViewsSetF a) -> b -> b) -> b -> ViewsSetF a -> b
$cnotElem :: forall a.
Eq (Element (ViewsSetF a)) =>
Element (ViewsSetF a) -> ViewsSetF a -> Bool
notElem :: Eq (Element (ViewsSetF a)) =>
Element (ViewsSetF a) -> ViewsSetF a -> Bool
$call :: forall a. (Element (ViewsSetF a) -> Bool) -> ViewsSetF a -> Bool
all :: (Element (ViewsSetF a) -> Bool) -> ViewsSetF a -> Bool
$cany :: forall a. (Element (ViewsSetF a) -> Bool) -> ViewsSetF a -> Bool
any :: (Element (ViewsSetF a) -> Bool) -> ViewsSetF a -> Bool
$cand :: forall a. (Element (ViewsSetF a) ~ Bool) => ViewsSetF a -> Bool
and :: (Element (ViewsSetF a) ~ Bool) => ViewsSetF a -> Bool
$cor :: forall a. (Element (ViewsSetF a) ~ Bool) => ViewsSetF a -> Bool
or :: (Element (ViewsSetF a) ~ Bool) => ViewsSetF a -> Bool
$cfind :: forall a.
(Element (ViewsSetF a) -> Bool)
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
find :: (Element (ViewsSetF a) -> Bool)
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
$csafeHead :: forall a. ViewsSetF a -> Maybe (Element (ViewsSetF a))
safeHead :: ViewsSetF a -> Maybe (Element (ViewsSetF a))
$csafeMaximum :: forall a.
Ord (Element (ViewsSetF a)) =>
ViewsSetF a -> Maybe (Element (ViewsSetF a))
safeMaximum :: Ord (Element (ViewsSetF a)) =>
ViewsSetF a -> Maybe (Element (ViewsSetF a))
$csafeMinimum :: forall a.
Ord (Element (ViewsSetF a)) =>
ViewsSetF a -> Maybe (Element (ViewsSetF a))
safeMinimum :: Ord (Element (ViewsSetF a)) =>
ViewsSetF a -> Maybe (Element (ViewsSetF a))
$csafeFoldr1 :: forall a.
(Element (ViewsSetF a)
 -> Element (ViewsSetF a) -> Element (ViewsSetF a))
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
safeFoldr1 :: (Element (ViewsSetF a)
 -> Element (ViewsSetF a) -> Element (ViewsSetF a))
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
$csafeFoldl1 :: forall a.
(Element (ViewsSetF a)
 -> Element (ViewsSetF a) -> Element (ViewsSetF a))
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
safeFoldl1 :: (Element (ViewsSetF a)
 -> Element (ViewsSetF a) -> Element (ViewsSetF a))
-> ViewsSetF a -> Maybe (Element (ViewsSetF a))
Container, ViewsSetF a -> [(Key (ViewsSetF a), Val (ViewsSetF a))]
ViewsSetF a -> [Key (ViewsSetF a)]
ViewsSetF a -> [Val (ViewsSetF a)]
(ViewsSetF a -> [(Key (ViewsSetF a), Val (ViewsSetF a))])
-> (ViewsSetF a -> [Key (ViewsSetF a)])
-> (ViewsSetF a -> [Val (ViewsSetF a)])
-> ToPairs (ViewsSetF a)
forall a. ViewsSetF a -> [(Key (ViewsSetF a), Val (ViewsSetF a))]
forall a. ViewsSetF a -> [Key (ViewsSetF a)]
forall a. ViewsSetF a -> [Val (ViewsSetF a)]
forall t.
(t -> [(Key t, Val t)])
-> (t -> [Key t]) -> (t -> [Val t]) -> ToPairs t
$ctoPairs :: forall a. ViewsSetF a -> [(Key (ViewsSetF a), Val (ViewsSetF a))]
toPairs :: ViewsSetF a -> [(Key (ViewsSetF a), Val (ViewsSetF a))]
$ckeys :: forall a. ViewsSetF a -> [Key (ViewsSetF a)]
keys :: ViewsSetF a -> [Key (ViewsSetF a)]
$celems :: forall a. ViewsSetF a -> [Val (ViewsSetF a)]
elems :: ViewsSetF a -> [Val (ViewsSetF a)]
ToPairs)
  deriving stock (ViewsSetF a -> ViewsSetF a -> Bool
(ViewsSetF a -> ViewsSetF a -> Bool)
-> (ViewsSetF a -> ViewsSetF a -> Bool) -> Eq (ViewsSetF a)
forall a. Eq a => ViewsSetF a -> ViewsSetF a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => ViewsSetF a -> ViewsSetF a -> Bool
== :: ViewsSetF a -> ViewsSetF a -> Bool
$c/= :: forall a. Eq a => ViewsSetF a -> ViewsSetF a -> Bool
/= :: ViewsSetF a -> ViewsSetF a -> Bool
Eq, Int -> ViewsSetF a -> ShowS
[ViewsSetF a] -> ShowS
ViewsSetF a -> String
(Int -> ViewsSetF a -> ShowS)
-> (ViewsSetF a -> String)
-> ([ViewsSetF a] -> ShowS)
-> Show (ViewsSetF a)
forall a. Show a => Int -> ViewsSetF a -> ShowS
forall a. Show a => [ViewsSetF a] -> ShowS
forall a. Show a => ViewsSetF a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> ViewsSetF a -> ShowS
showsPrec :: Int -> ViewsSetF a -> ShowS
$cshow :: forall a. Show a => ViewsSetF a -> String
show :: ViewsSetF a -> String
$cshowList :: forall a. Show a => [ViewsSetF a] -> ShowS
showList :: [ViewsSetF a] -> ShowS
Show, (forall a b. (a -> b) -> ViewsSetF a -> ViewsSetF b)
-> (forall a b. a -> ViewsSetF b -> ViewsSetF a)
-> Functor ViewsSetF
forall a b. a -> ViewsSetF b -> ViewsSetF a
forall a b. (a -> b) -> ViewsSetF a -> ViewsSetF b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ViewsSetF a -> ViewsSetF b
fmap :: forall a b. (a -> b) -> ViewsSetF a -> ViewsSetF b
$c<$ :: forall a b. a -> ViewsSetF b -> ViewsSetF a
<$ :: forall a b. a -> ViewsSetF b -> ViewsSetF a
Functor)

-- | Errors possible when constructing @ViewsSet@.
data ViewsSetError
  = DuplicatedViewName ViewName
  deriving stock (Int -> ViewsSetError -> ShowS
[ViewsSetError] -> ShowS
ViewsSetError -> String
(Int -> ViewsSetError -> ShowS)
-> (ViewsSetError -> String)
-> ([ViewsSetError] -> ShowS)
-> Show ViewsSetError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ViewsSetError -> ShowS
showsPrec :: Int -> ViewsSetError -> ShowS
$cshow :: ViewsSetError -> String
show :: ViewsSetError -> String
$cshowList :: [ViewsSetError] -> ShowS
showList :: [ViewsSetError] -> ShowS
Show, ViewsSetError -> ViewsSetError -> Bool
(ViewsSetError -> ViewsSetError -> Bool)
-> (ViewsSetError -> ViewsSetError -> Bool) -> Eq ViewsSetError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ViewsSetError -> ViewsSetError -> Bool
== :: ViewsSetError -> ViewsSetError -> Bool
$c/= :: ViewsSetError -> ViewsSetError -> Bool
/= :: ViewsSetError -> ViewsSetError -> Bool
Eq)

instance Buildable ViewsSetError where
  build :: ViewsSetError -> Doc
build = \case
    DuplicatedViewName ViewName
name -> Doc
"Duplicated view name '" Doc -> Doc -> Doc
forall b. FromDoc b => Doc -> Doc -> b
+| ViewName
name ViewName -> Doc -> Doc
forall a b. (Buildable a, FromDoc b) => a -> Doc -> b
|+ Doc
"'"

-- | Convenience function to construct 'ViewsSetF'.
mkViewsSet :: (a -> ViewName) -> [a] -> Either ViewsSetError (ViewsSetF a)
mkViewsSet :: forall a.
(a -> ViewName) -> [a] -> Either ViewsSetError (ViewsSetF a)
mkViewsSet a -> ViewName
viewName [a]
views = do
  [ViewName] -> Either ViewsSetError ()
ensureNoDuplicates ([ViewName] -> Either ViewsSetError ())
-> [ViewName] -> Either ViewsSetError ()
forall a b. (a -> b) -> a -> b
$ a -> ViewName
viewName (a -> ViewName) -> [a] -> [ViewName]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
views
  pure $ Map ViewName a -> ViewsSetF a
forall a. Map ViewName a -> ViewsSetF a
ViewsSetF (Map ViewName a -> ViewsSetF a) -> Map ViewName a -> ViewsSetF a
forall a b. (a -> b) -> a -> b
$ [ListElement (Map ViewName a)] -> Map ViewName a
forall l. (FromList l, FromListC l) => [ListElement l] -> l
fromList ([ListElement (Map ViewName a)] -> Map ViewName a)
-> [ListElement (Map ViewName a)] -> Map ViewName a
forall a b. (a -> b) -> a -> b
$ [a]
views [a] -> (a -> (ViewName, a)) -> [(ViewName, a)]
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \a
v -> (a -> ViewName
viewName a
v, a
v)

-- | No views.
emptyViewsSet :: ViewsSetF a
emptyViewsSet :: forall a. ViewsSetF a
emptyViewsSet = ViewsSetF a
forall a. Default a => a
def

-- | Add a view to set.
addViewToSet
  :: (a -> ViewName)
  -> a
  -> ViewsSetF a
  -> Either ViewsSetError (ViewsSetF a)
addViewToSet :: forall a.
(a -> ViewName)
-> a -> ViewsSetF a -> Either ViewsSetError (ViewsSetF a)
addViewToSet a -> ViewName
name a
v ViewsSetF a
views = do
  let viewName :: ViewName
viewName = a -> ViewName
name a
v
  Bool -> Either ViewsSetError () -> Either ViewsSetError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ViewName
viewName ViewName -> Map ViewName a -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`Map.member` ViewsSetF a -> Map ViewName a
forall a. ViewsSetF a -> Map ViewName a
unViewsSetF ViewsSetF a
views) (Either ViewsSetError () -> Either ViewsSetError ())
-> Either ViewsSetError () -> Either ViewsSetError ()
forall a b. (a -> b) -> a -> b
$
    ViewsSetError -> Either ViewsSetError ()
forall a. ViewsSetError -> Either ViewsSetError a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (ViewsSetError -> Either ViewsSetError ())
-> ViewsSetError -> Either ViewsSetError ()
forall a b. (a -> b) -> a -> b
$ ViewName -> ViewsSetError
DuplicatedViewName ViewName
viewName
  pure $ (Map ViewName a -> Map ViewName a) -> ViewsSetF a -> ViewsSetF a
forall a b. Coercible a b => a -> b
coerce (ViewName -> a -> Map ViewName a -> Map ViewName a
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert ViewName
viewName a
v) ViewsSetF a
views

-- | Find a view in the set.
lookupView :: ViewName -> ViewsSetF a -> Maybe a
lookupView :: forall a. ViewName -> ViewsSetF a -> Maybe a
lookupView ViewName
name = ViewName -> Map ViewName a -> Maybe a
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ViewName
name (Map ViewName a -> Maybe a)
-> (ViewsSetF a -> Map ViewName a) -> ViewsSetF a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ViewsSetF a -> Map ViewName a
forall a. ViewsSetF a -> Map ViewName a
unViewsSetF

-- | Get all taken names in views set.
viewsSetNames :: ViewsSetF a -> Set ViewName
viewsSetNames :: forall a. ViewsSetF a -> Set ViewName
viewsSetNames = Map ViewName a -> Set ViewName
forall k a. Map k a -> Set k
Map.keysSet (Map ViewName a -> Set ViewName)
-> (ViewsSetF a -> Map ViewName a) -> ViewsSetF a -> Set ViewName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ViewsSetF a -> Map ViewName a
forall a. ViewsSetF a -> Map ViewName a
unViewsSetF

ensureNoDuplicates :: [ViewName] -> Either ViewsSetError ()
ensureNoDuplicates :: [ViewName] -> Either ViewsSetError ()
ensureNoDuplicates [ViewName]
names =
  [NonEmpty ViewName]
-> (Element [NonEmpty ViewName] -> Either ViewsSetError ())
-> Either ViewsSetError ()
forall t (m :: * -> *) b.
(Container t, Monad m) =>
t -> (Element t -> m b) -> m ()
forM_ ([ViewName] -> [NonEmpty ViewName]
forall (f :: * -> *) a. (Foldable f, Eq a) => f a -> [NonEmpty a]
NE.group ([ViewName] -> [NonEmpty ViewName])
-> [ViewName] -> [NonEmpty ViewName]
forall a b. (a -> b) -> a -> b
$ [ViewName] -> [ViewName]
forall a. Ord a => [a] -> [a]
sort [ViewName]
names) \case
    ViewName
name :| ViewName
_ : [ViewName]
_ -> ViewsSetError -> Either ViewsSetError ()
forall a. ViewsSetError -> Either ViewsSetError a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (ViewsSetError -> Either ViewsSetError ())
-> ViewsSetError -> Either ViewsSetError ()
forall a b. (a -> b) -> a -> b
$ ViewName -> ViewsSetError
DuplicatedViewName ViewName
name
    ViewName
_ :| [ViewName]
_ -> Either ViewsSetError ()
forall (f :: * -> *). Applicative f => f ()
pass