Skip to content

Commit

Permalink
Convert project to fourmolu
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Jul 25, 2023
1 parent 0ce1993 commit 46977cc
Show file tree
Hide file tree
Showing 15 changed files with 304 additions and 327 deletions.
7 changes: 2 additions & 5 deletions .restyled.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
restylers:
- brittany:
include:
- '**/*.hs'
- '!src/Graphula/Class.hs' # CPP
- stylish-haskell
- "!stylish-haskell"
- fourmolu

comments: false

Expand Down
22 changes: 0 additions & 22 deletions .stylish-haskell.yaml

This file was deleted.

1 change: 1 addition & 0 deletions Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import Distribution.Simple

main = defaultMain
77 changes: 0 additions & 77 deletions brittany.yaml

This file was deleted.

15 changes: 15 additions & 0 deletions fourmolu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
indentation: 2
column-limit: 80 # ignored until v12 / ghc-9.6
function-arrows: leading
comma-style: leading # default
import-export-style: leading
indent-wheres: false # default
record-brace-space: true
newlines-between-decls: 1 # default
haddock-style: single-line
let-style: mixed
in-style: left-align
single-constraint-parens: never # ignored until v12 / ghc-9.6
unicode: never # default
respectful: true # default
fixities: [] # default
119 changes: 62 additions & 57 deletions src/Graphula.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,64 +72,64 @@
-- $ 'ensure'
-- $ not . courseIsArchived
-- @
--
module Graphula
(
-- * Basic usage
-- ** Model requirements
HasDependencies(..)
, Only(..)
( -- * Basic usage

-- ** Model requirements
HasDependencies (..)
, Only (..)
, only

-- ** Defining the graph
-- ** Defining the graph
, node
, edit
, ensure

-- ** Running the graph
-- ** Running the graph
, GraphulaT
, runGraphulaT
, GenerationFailure(..)
, GenerationFailure (..)

-- * Advanced usage
-- ** Non-serial keys
, KeySourceType(..)
-- * Advanced usage

-- ** Non-serial keys
, KeySourceType (..)
, nodeKeyed

-- ** Running with logging
-- ** Running with logging
, GraphulaLoggedT
, runGraphulaLoggedT
, runGraphulaLoggedWithFileT

-- ** Running idempotently
-- ** Running idempotently
, GraphulaIdempotentT
, runGraphulaIdempotentT

-- * Useful synonymns
-- |
--
-- When declaring your own functions that call 'node', these synonyms can help
-- with the constraint soup.
--
-- > genSchoolWithTeacher
-- > :: GraphulaContext m '[School, Teacher]
-- > -> m (Entity Teacher)
-- > genSchoolWithTeacher = do
-- > school <- node @School () mempty
-- > node @Teacher (onlyKey school) mempty
--
-- * Useful synonymns

-- |
--
-- When declaring your own functions that call 'node', these synonyms can help
-- with the constraint soup.
--
-- > genSchoolWithTeacher
-- > :: GraphulaContext m '[School, Teacher]
-- > -> m (Entity Teacher)
-- > genSchoolWithTeacher = do
-- > school <- node @School () mempty
-- > node @Teacher (onlyKey school) mempty
, GraphulaContext
, GraphulaNode

-- * Lower-level details
-- |
--
-- These exports are likely to be removed from this module in a future
-- version. If you are using them, consider importing from their own modules.
--
-- * Lower-level details

-- |
--
-- These exports are likely to be removed from this module in a future
-- version. If you are using them, consider importing from their own modules.
, MonadGraphula
, MonadGraphulaBackend(..)
, MonadGraphulaFrontend(..)
, MonadGraphulaBackend (..)
, MonadGraphulaFrontend (..)
, NodeOptions
, GenerateKey
, NoConstraint
Expand Down Expand Up @@ -162,8 +162,11 @@ import Graphula.NoConstraint
import Graphula.Node
import System.Random (randomIO)
import Test.HUnit.Lang
(FailureReason(..), HUnitFailure(..), formatFailureReason)
import Test.QuickCheck (Arbitrary(..))
( FailureReason (..)
, HUnitFailure (..)
, formatFailureReason
)
import Test.QuickCheck (Arbitrary (..))
import Test.QuickCheck.Random (QCGen, mkQCGen)
import UnliftIO.Exception (catch, throwIO)

Expand All @@ -179,10 +182,9 @@ import UnliftIO.Exception (catch, throwIO)
-- node @C (a, b) $ edit $ \n ->
-- n { cc = "spanish" }
-- @
--
type family GraphulaContext (m :: Type -> Type) (ts :: [Type]) :: Constraint where
GraphulaContext m '[] = MonadGraphula m
GraphulaContext m (t ': ts) = (GraphulaNode m t, GraphulaContext m ts)
GraphulaContext m '[] = MonadGraphula m
GraphulaContext m (t ': ts) = (GraphulaNode m t, GraphulaContext m ts)

data Args backend n m = Args
{ dbRunner :: RunDB backend n m
Expand All @@ -191,9 +193,9 @@ data Args backend n m = Args

newtype RunDB backend n m = RunDB (forall b. ReaderT backend n b -> m b)

newtype GraphulaT n m a =
GraphulaT { runGraphulaT' :: ReaderT (Args SqlBackend n m) m a }
deriving newtype (Functor, Applicative, Monad, MonadIO, MonadReader (Args SqlBackend n m))
newtype GraphulaT n m a = GraphulaT {runGraphulaT' :: ReaderT (Args SqlBackend n m) m a}
deriving newtype
(Functor, Applicative, Monad, MonadIO, MonadReader (Args SqlBackend n m))

instance MonadTrans (GraphulaT n) where
lift = GraphulaT . lift
Expand All @@ -212,9 +214,10 @@ instance (MonadIO m, MonadIO n) => MonadGraphulaFrontend (GraphulaT n m) where
insert mKey n = do
RunDB runDB <- asks dbRunner
lift . runDB $ case mKey of
Nothing -> insertUnique n >>= \case
Nothing -> pure Nothing
Just key -> getEntity key
Nothing ->
insertUnique n >>= \case
Nothing -> pure Nothing
Just key -> getEntity key
Just key -> do
existingKey <- get key
whenNothing existingKey $ do
Expand All @@ -232,9 +235,11 @@ whenNothing Nothing f = f
whenNothing (Just _) _ = pure Nothing

runGraphulaT
:: (MonadUnliftIO m)
=> Maybe Int -- ^ Optional seed
-> (forall b . ReaderT SqlBackend n b -> m b) -- ^ Database runner
:: MonadUnliftIO m
=> Maybe Int
-- ^ Optional seed
-> (forall b. ReaderT SqlBackend n b -> m b)
-- ^ Database runner
-> GraphulaT n m a
-> m a
runGraphulaT mSeed runDB action = do
Expand All @@ -250,11 +255,11 @@ rethrowHUnitWith :: MonadIO m => String -> HUnitFailure -> m a
rethrowHUnitWith message (HUnitFailure l r) =
throwIO . HUnitFailure l . Reason $ message ++ "\n\n" ++ formatFailureReason r

type GraphulaNode m a
= ( HasDependencies a
, Logging m a
, PersistEntityBackend a ~ SqlBackend
, PersistEntity a
, Typeable a
, Arbitrary a
)
type GraphulaNode m a =
( HasDependencies a
, Logging m a
, PersistEntityBackend a ~ SqlBackend
, PersistEntity a
, Typeable a
, Arbitrary a
)
1 change: 0 additions & 1 deletion src/Graphula/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import Test.QuickCheck.Gen (unGen)
-- This is akin to 'Test.QuickCheck.generate', but utilizing graphula's
-- generation. The size passed to the generator is always 30; if you want
-- another size then you should explicitly use 'Test.QuickCheck.resize'.
--
generate :: (MonadIO m, MonadGraphulaBackend m) => Gen a -> m a
generate gen = do
genRef <- askGen
Expand Down
22 changes: 15 additions & 7 deletions src/Graphula/Class.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
Expand All @@ -13,21 +13,23 @@
-- | Internal type class(es) for Graphula-related behaviors
module Graphula.Class
( MonadGraphula
, MonadGraphulaFrontend(..)
, MonadGraphulaBackend(..)
, MonadGraphulaFrontend (..)
, MonadGraphulaBackend (..)
, GraphulaSafeToInsert
) where

import Control.Monad.IO.Class (MonadIO)
import Data.IORef (IORef)
import Data.Kind (Constraint, Type)
import Database.Persist (Entity(..), Key, PersistEntity, PersistEntityBackend)
import Database.Persist (Entity (..), Key, PersistEntity, PersistEntityBackend)
import Database.Persist.Sql (SqlBackend)
import Test.QuickCheck.Random (QCGen)
#if MIN_VERSION_persistent(2,14,0)
import Database.Persist.Class.PersistEntity (SafeToInsert)
#endif

{- FOURMOLU_DISABLE -}

-- | A class that provides backwards compatibility with @persistent-2.14@
--
-- If you are using that version or above, then this is a class alias for
Expand All @@ -49,12 +51,18 @@ instance
#endif
GraphulaSafeToInsert a

type MonadGraphula m
= (Monad m, MonadIO m, MonadGraphulaBackend m, MonadGraphulaFrontend m)
{- FOURMOLU_ENABLE -}

type MonadGraphula m =
(Monad m, MonadIO m, MonadGraphulaBackend m, MonadGraphulaFrontend m)

class MonadGraphulaFrontend m where
insert
:: (PersistEntityBackend a ~ SqlBackend, PersistEntity a, Monad m, GraphulaSafeToInsert a)
:: ( PersistEntityBackend a ~ SqlBackend
, PersistEntity a
, Monad m
, GraphulaSafeToInsert a
)
=> Maybe (Key a)
-> a
-> m (Maybe (Entity a))
Expand Down
Loading

0 comments on commit 46977cc

Please sign in to comment.