{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE Trustworthy #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module ReWire.Orphans where

import safe ReWire.Pretty (Doc, TextShow (showb, showbPrec), genericShowbPrec)
import safe ReWire.BitVector (BV (nat), width, showHex)

import safe Data.HashMap.Strict (HashMap)
import safe Data.HashSet (HashSet)
import safe Data.Hashable (Hashable (hashWithSalt, hash))
import safe Data.Map.Strict.Internal (Map (..))
import safe Data.Set.Internal (Set (..))
import safe GHC.Generics (Generic)

import qualified Data.Yaml                as YAML
import safe qualified Data.HashMap.Strict as Map
import safe qualified Data.HashSet        as Set

instance TextShow (Doc ann) where
      showb :: Doc ann -> Builder
showb = String -> Builder
forall a. TextShow a => a -> Builder
showb (String -> Builder) -> (Doc ann -> String) -> Doc ann -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc ann -> String
forall a. Show a => a -> String
show

instance Hashable BV where
      hashWithSalt :: Int -> BV -> Int
hashWithSalt Int
s BV
bv = Int -> (Int, Integer) -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
s (BV -> Int
width BV
bv, BV -> Integer
nat BV
bv)
      hash :: BV -> Int
hash BV
bv = (Int, Integer) -> Int
forall a. Hashable a => a -> Int
hash (BV -> Int
width BV
bv, BV -> Integer
nat BV
bv)

instance TextShow BV where
      showb :: BV -> Builder
showb = Text -> Builder
forall a. TextShow a => a -> Builder
showb (Text -> Builder) -> (BV -> Text) -> BV -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BV -> Text
showHex

instance YAML.ToJSON BV where
      toJSON :: BV -> Value
toJSON = Text -> Value
YAML.String (Text -> Value) -> (BV -> Text) -> BV -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BV -> Text
showHex

instance TextShow a => TextShow (HashSet a) where
      showb :: HashSet a -> Builder
showb = [a] -> Builder
forall a. TextShow a => a -> Builder
showb ([a] -> Builder) -> (HashSet a -> [a]) -> HashSet a -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashSet a -> [a]
forall a. HashSet a -> [a]
Set.toList

instance (TextShow a, TextShow b) => TextShow (HashMap a b) where
      showb :: HashMap a b -> Builder
showb = [(a, b)] -> Builder
forall a. TextShow a => a -> Builder
showb ([(a, b)] -> Builder)
-> (HashMap a b -> [(a, b)]) -> HashMap a b -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap a b -> [(a, b)]
forall k v. HashMap k v -> [(k, v)]
Map.toList

deriving instance Generic a => Generic (Set a)
deriving instance (Generic a, Generic b) => Generic (Map a b)

instance (Generic a, TextShow a) => TextShow (Set a) where
      showbPrec :: Int -> Set a -> Builder
showbPrec = Int -> Set a -> Builder
forall a. (Generic a, GTextShowB (Rep a ())) => Int -> a -> Builder
genericShowbPrec
instance (Generic a, Generic b, TextShow a, TextShow b) => TextShow (Map a b) where
      showbPrec :: Int -> Map a b -> Builder
showbPrec = Int -> Map a b -> Builder
forall a. (Generic a, GTextShowB (Rep a ())) => Int -> a -> Builder
genericShowbPrec