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

module Network.URI.Extended
  ( module Network.URI,
  )
where

import Data.Aeson
import Data.Aeson.Types
import Data.Hashable
import Data.Text qualified as T
import Hasura.Prelude
import Network.URI

instance {-# INCOHERENT #-} FromJSON URI where
  parseJSON :: Value -> Parser URI
parseJSON (String Text
uri) = do
    let mUrl :: Maybe URI
mUrl = String -> Maybe URI
parseURI (String -> Maybe URI) -> String -> Maybe URI
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack Text
uri
    Maybe URI -> Parser URI -> Parser URI
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
onNothing Maybe URI
mUrl (String -> Parser URI
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"not a valid URI")
  parseJSON Value
_ = String -> Parser URI
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"not a valid URI"

instance {-# INCOHERENT #-} ToJSON URI where
  toJSON :: URI -> Value
toJSON = Text -> Value
String (Text -> Value) -> (URI -> Text) -> URI -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> Text
forall a. Show a => a -> Text
tshow

instance {-# INCOHERENT #-} ToJSONKey URI where
  toJSONKey :: ToJSONKeyFunction URI
toJSONKey = (URI -> Text) -> ToJSONKeyFunction URI
forall a. (a -> Text) -> ToJSONKeyFunction a
toJSONKeyText URI -> Text
forall a. Show a => a -> Text
tshow

instance Hashable URI where
  hashWithSalt :: Int -> URI -> Int
hashWithSalt Int
i = Int -> Text -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
i (Text -> Int) -> (URI -> Text) -> URI -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> Text
forall a. Show a => a -> Text
tshow