-- |Provide paths for 'Ribosome.Persist'
module Ribosome.Effect.PersistPath where

import Path (Abs, Dir, Path, Rel)

import Ribosome.Data.Setting (Setting (Setting))

-- |This is a utility effect for 'Ribosome.Persist', determining the root directory for persistence files.
data PersistPath :: Effect where
  -- |Return the root if 'Nothing' is given, or the subdir of the root if 'Just' is given.
  PersistPath :: Maybe (Path Rel Dir) -> PersistPath m (Path Abs Dir)

makeSem_ ''PersistPath

-- |Return the root if 'Nothing' is given, or the subdir of the root if 'Just' is given.
persistPath ::
   r .
  Member PersistPath r =>
  Maybe (Path Rel Dir) ->
  Sem r (Path Abs Dir)

-- |This setting may be used to specify the root directory for all plugins.
-- The default is to use the XDG cache dir.
setting :: Setting (Path Abs Dir)
setting :: Setting (Path Abs Dir)
setting =
  Text -> Bool -> Maybe (Path Abs Dir) -> Setting (Path Abs Dir)
forall a. Text -> Bool -> Maybe a -> Setting a
Setting Text
"ribosome_persistence_dir" Bool
False Maybe (Path Abs Dir)
forall a. Maybe a
Nothing

-- |Get the root directory for persistence files.
persistRoot ::
  Member PersistPath r =>
  Sem r (Path Abs Dir)
persistRoot :: forall (r :: EffectRow).
Member PersistPath r =>
Sem r (Path Abs Dir)
persistRoot =
  Maybe (Path Rel Dir) -> Sem r (Path Abs Dir)
forall (r :: EffectRow).
Member PersistPath r =>
Maybe (Path Rel Dir) -> Sem r (Path Abs Dir)
persistPath Maybe (Path Rel Dir)
forall a. Maybe a
Nothing

-- |Get a subdir of the root directory for persistence files.
persistSubPath ::
  Member PersistPath r =>
  Path Rel Dir ->
  Sem r (Path Abs Dir)
persistSubPath :: forall (r :: EffectRow).
Member PersistPath r =>
Path Rel Dir -> Sem r (Path Abs Dir)
persistSubPath Path Rel Dir
p =
  Maybe (Path Rel Dir) -> Sem r (Path Abs Dir)
forall (r :: EffectRow).
Member PersistPath r =>
Maybe (Path Rel Dir) -> Sem r (Path Abs Dir)
persistPath (Path Rel Dir -> Maybe (Path Rel Dir)
forall a. a -> Maybe a
Just Path Rel Dir
p)