{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TypeFamilies #-}

module System.FilePath.FilePather.ToFilePath(
  ToFilePathT(..)
, ToFilePath
, toFilePath
, toRead
) where

import Control.Category ( Category((.)) )
import Control.Lens
    ( view,
      from,
      iso,
      Iso,
      Iso',
      Wrapped(..) )
import Data.Functor.Contravariant ( Contravariant(contramap) )
import Data.Functor.Identity ( Identity(..) )
import System.FilePath ( FilePath )
import System.FilePath.FilePather.ReadFilePath
    ( ReadFilePath, readFilePath )

newtype ToFilePathT f a =
  ToFilePathT (a -> f FilePath)

type ToFilePath a =
  ToFilePathT Identity a

instance Wrapped (ToFilePathT f a) where
  type Unwrapped (ToFilePathT f a) =
    a
    -> f FilePath
  _Wrapped' :: p (Unwrapped (ToFilePathT f a)) (f (Unwrapped (ToFilePathT f a)))
-> p (ToFilePathT f a) (f (ToFilePathT f a))
_Wrapped' =
    (ToFilePathT f a -> a -> f FilePath)
-> ((a -> f FilePath) -> ToFilePathT f a)
-> Iso
     (ToFilePathT f a)
     (ToFilePathT f a)
     (a -> f FilePath)
     (a -> f FilePath)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso (\(ToFilePathT a -> f FilePath
x) -> a -> f FilePath
x) (a -> f FilePath) -> ToFilePathT f a
forall (f :: * -> *) a. (a -> f FilePath) -> ToFilePathT f a
ToFilePathT
  {-# INLINE _Wrapped' #-}

instance Contravariant (ToFilePathT f) where
  contramap :: (a -> b) -> ToFilePathT f b -> ToFilePathT f a
contramap a -> b
f (ToFilePathT b -> f FilePath
g) =
    (a -> f FilePath) -> ToFilePathT f a
forall (f :: * -> *) a. (a -> f FilePath) -> ToFilePathT f a
ToFilePathT (b -> f FilePath
g (b -> f FilePath) -> (a -> b) -> a -> f FilePath
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> b
f)

toFilePath ::
  Iso
    (ToFilePath a)
    (ToFilePath a')
    (a -> FilePath)
    (a' -> FilePath)
toFilePath :: p (a -> FilePath) (f (a' -> FilePath))
-> p (ToFilePath a) (f (ToFilePath a'))
toFilePath =
  (ToFilePath a -> a -> FilePath)
-> ((a' -> FilePath) -> ToFilePath a')
-> Iso
     (ToFilePath a) (ToFilePath a') (a -> FilePath) (a' -> FilePath)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
    (\(ToFilePathT a -> Identity FilePath
x) -> Identity FilePath -> FilePath
forall a. Identity a -> a
runIdentity (Identity FilePath -> FilePath)
-> (a -> Identity FilePath) -> a -> FilePath
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Identity FilePath
x)
    (\a' -> FilePath
p -> (a' -> Identity FilePath) -> ToFilePath a'
forall (f :: * -> *) a. (a -> f FilePath) -> ToFilePathT f a
ToFilePathT (FilePath -> Identity FilePath
forall a. a -> Identity a
Identity (FilePath -> Identity FilePath)
-> (a' -> FilePath) -> a' -> Identity FilePath
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a' -> FilePath
p))
{-# INLINE toFilePath #-}

toRead ::
  Iso'
    (ToFilePath FilePath)
    (ReadFilePath FilePath)
toRead :: p (ReadFilePath FilePath) (f (ReadFilePath FilePath))
-> p (ToFilePath FilePath) (f (ToFilePath FilePath))
toRead =
  (ToFilePath FilePath -> ReadFilePath FilePath)
-> (ReadFilePath FilePath -> ToFilePath FilePath)
-> Iso
     (ToFilePath FilePath)
     (ToFilePath FilePath)
     (ReadFilePath FilePath)
     (ReadFilePath FilePath)
forall s a b t. (s -> a) -> (b -> t) -> Iso s t a b
iso
    (Getting
  (ReadFilePath FilePath)
  (ToFilePath FilePath)
  (ReadFilePath FilePath)
-> ToFilePath FilePath -> ReadFilePath FilePath
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (((FilePath -> FilePath)
 -> Const (ReadFilePath FilePath) (FilePath -> FilePath))
-> ToFilePath FilePath
-> Const (ReadFilePath FilePath) (ToFilePath FilePath)
forall a a'.
Iso (ToFilePath a) (ToFilePath a') (a -> FilePath) (a' -> FilePath)
toFilePath (((FilePath -> FilePath)
  -> Const (ReadFilePath FilePath) (FilePath -> FilePath))
 -> ToFilePath FilePath
 -> Const (ReadFilePath FilePath) (ToFilePath FilePath))
-> ((ReadFilePath FilePath
     -> Const (ReadFilePath FilePath) (ReadFilePath FilePath))
    -> (FilePath -> FilePath)
    -> Const (ReadFilePath FilePath) (FilePath -> FilePath))
-> Getting
     (ReadFilePath FilePath)
     (ToFilePath FilePath)
     (ReadFilePath FilePath)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AnIso
  (ReadFilePath FilePath)
  (ReadFilePath FilePath)
  (FilePath -> FilePath)
  (FilePath -> FilePath)
-> Iso
     (FilePath -> FilePath)
     (FilePath -> FilePath)
     (ReadFilePath FilePath)
     (ReadFilePath FilePath)
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso
  (ReadFilePath FilePath)
  (ReadFilePath FilePath)
  (FilePath -> FilePath)
  (FilePath -> FilePath)
forall a a'.
Iso
  (ReadFilePath a) (ReadFilePath a') (FilePath -> a) (FilePath -> a')
readFilePath))
    (Getting
  (ToFilePath FilePath) (ReadFilePath FilePath) (ToFilePath FilePath)
-> ReadFilePath FilePath -> ToFilePath FilePath
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view (((FilePath -> FilePath)
 -> Const (ToFilePath FilePath) (FilePath -> FilePath))
-> ReadFilePath FilePath
-> Const (ToFilePath FilePath) (ReadFilePath FilePath)
forall a a'.
Iso
  (ReadFilePath a) (ReadFilePath a') (FilePath -> a) (FilePath -> a')
readFilePath (((FilePath -> FilePath)
  -> Const (ToFilePath FilePath) (FilePath -> FilePath))
 -> ReadFilePath FilePath
 -> Const (ToFilePath FilePath) (ReadFilePath FilePath))
-> ((ToFilePath FilePath
     -> Const (ToFilePath FilePath) (ToFilePath FilePath))
    -> (FilePath -> FilePath)
    -> Const (ToFilePath FilePath) (FilePath -> FilePath))
-> Getting
     (ToFilePath FilePath) (ReadFilePath FilePath) (ToFilePath FilePath)
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AnIso
  (ToFilePath FilePath)
  (ToFilePath FilePath)
  (FilePath -> FilePath)
  (FilePath -> FilePath)
-> Iso
     (FilePath -> FilePath)
     (FilePath -> FilePath)
     (ToFilePath FilePath)
     (ToFilePath FilePath)
forall s t a b. AnIso s t a b -> Iso b a t s
from AnIso
  (ToFilePath FilePath)
  (ToFilePath FilePath)
  (FilePath -> FilePath)
  (FilePath -> FilePath)
forall a a'.
Iso (ToFilePath a) (ToFilePath a') (a -> FilePath) (a' -> FilePath)
toFilePath))