{-# OPTIONS_GHC -fno-warn-orphans #-}

module StrongPath.Instances where

import Data.Hashable
import StrongPath.FilePath
import StrongPath.Types

-- Hashable instance for Path declared here, as an orphaned instance, instead of
-- in StrongPath.Internal to avoid cyclic dependency between StrongPath.FilePath
-- and StrongPath.Internal. (This cycle would arise due to the use of
-- `toFilePath` from FilePath in the instance declaration and the dependency of
-- the FilePath module on the types from the Internal module)

-- |
-- Caveat: For two relative Paths, that only differ in the Directory, that they
-- are relative to, this Hashable instance will return the same hash even though
-- they are different paths.
instance Hashable (Path s b t) where
  hashWithSalt :: Int -> Path s b t -> Int
hashWithSalt Int
salt = Int -> FilePath -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt (FilePath -> Int) -> (Path s b t -> FilePath) -> Path s b t -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Path s b t -> FilePath
forall s b t. Path s b t -> FilePath
toFilePath

-- Paths can be compared
instance Ord (Path s b t) where
  compare :: Path s b t -> Path s b t -> Ordering
compare Path s b t
p1 Path s b t
p2 = FilePath -> FilePath -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Path s b t -> FilePath
forall s b t. Path s b t -> FilePath
toFilePath Path s b t
p1) (Path s b t -> FilePath
forall s b t. Path s b t -> FilePath
toFilePath Path s b t
p2)