{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
module Data.Interned.URI
( InternedURI
) where
import Data.String (IsString(..))
import Data.Hashable
import Data.Interned
import Data.Maybe (fromMaybe)
import Network.URI
data InternedURI = InternedURI !Int !URI
instance IsString InternedURI where
fromString :: String -> InternedURI
fromString = URI -> InternedURI
forall t. Interned t => Uninterned t -> t
intern (URI -> InternedURI) -> (String -> URI) -> String -> InternedURI
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
URI -> Maybe URI -> URI
forall a. a -> Maybe a -> a
fromMaybe (String -> URI
forall a. HasCallStack => String -> a
error String
"Error: unable to create a URI.") (Maybe URI -> URI) -> (String -> Maybe URI) -> String -> URI
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> Maybe URI
parseURIReference
instance Eq InternedURI where
InternedURI Int
a URI
_ == :: InternedURI -> InternedURI -> Bool
== InternedURI Int
b URI
_ = Int
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
b
instance Ord InternedURI where
compare :: InternedURI -> InternedURI -> Ordering
compare (InternedURI Int
a URI
_) (InternedURI Int
b URI
_) = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
a Int
b
instance Show InternedURI where
showsPrec :: Int -> InternedURI -> ShowS
showsPrec Int
d (InternedURI Int
_ URI
b) = Int -> URI -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
d URI
b
instance Interned InternedURI where
type Uninterned InternedURI = URI
data Description InternedURI = DU !URI deriving (Description InternedURI -> Description InternedURI -> Bool
(Description InternedURI -> Description InternedURI -> Bool)
-> (Description InternedURI -> Description InternedURI -> Bool)
-> Eq (Description InternedURI)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Description InternedURI -> Description InternedURI -> Bool
$c/= :: Description InternedURI -> Description InternedURI -> Bool
== :: Description InternedURI -> Description InternedURI -> Bool
$c== :: Description InternedURI -> Description InternedURI -> Bool
Eq)
describe :: Uninterned InternedURI -> Description InternedURI
describe = Uninterned InternedURI -> Description InternedURI
URI -> Description InternedURI
DU
identify :: Int -> Uninterned InternedURI -> InternedURI
identify = Int -> Uninterned InternedURI -> InternedURI
Int -> URI -> InternedURI
InternedURI
#if MIN_VERSION_intern(0,9,0)
#else
identity (InternedURI i _) = i
#endif
cache :: Cache InternedURI
cache = Cache InternedURI
iuCache
instance Uninternable InternedURI where
unintern :: InternedURI -> Uninterned InternedURI
unintern (InternedURI Int
_ URI
b) = Uninterned InternedURI
URI
b
instance Hashable (Description InternedURI) where
#if MIN_VERSION_hashable(1,2,0)
#else
hash = hashWithSalt 5381
#endif
hashWithSalt :: Int -> Description InternedURI -> Int
hashWithSalt Int
salt (DU u) = Int -> String -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt ((ShowS
forall a. [a] -> [a]
reverse ShowS -> (URI -> String) -> URI -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. URI -> String
forall a. Show a => a -> String
show) URI
u)
iuCache :: Cache InternedURI
iuCache :: Cache InternedURI
iuCache = Cache InternedURI
forall t. Interned t => Cache t
mkCache
{-# NOINLINE iuCache #-}