--- Instances of SchemeMapper --------------------------------------------------
-instance SchemeMapper SchemeMap where
- {-# INLINE findHostMap #-}
- findHostMap s (SMap α) = findHostMap s α
- {-# INLINE schemeMap #-}
- schemeMap = id
-
--- |'HostMap's are also 'SchemeMapper's too, which matches to any
--- schemes.
-instance SchemeMapper HostMap where
- {-# INLINE findHostMap #-}
- findHostMap = const return
-
--- |'ResourceMap's are also 'SchemeMapper's too, which matches to any
--- schemes and hosts.
-instance SchemeMapper ResourceMap where
- {-# INLINE findHostMap #-}
- findHostMap _ r = return $ hostMap f
- where
- f ∷ Host → Maybe ResourceMap
- {-# INLINE f #-}
- f = const $ Just r
-
--- |@'insert' a b@ first tries @a@, and if it fails, tries @b@ next.
-instance SchemeMapper α ⇒ Unfoldable SchemeMap α where
- {-# INLINE insert #-}
- insert a (SMap b) = schemeMap c
- where
- c ∷ Scheme → MaybeT IO HostMap
- {-# INLINEABLE c #-}
- c s = findHostMap s a <|> findHostMap s b
- {-# INLINE empty #-}
- empty = (∅)
- {-# INLINE singleton #-}
- singleton = schemeMap
-
--- |@'mappend' a b@ first tries @a@, and if it fails, tries @b@ next.
-instance Monoid SchemeMap where
- {-# INLINE mempty #-}
- mempty = schemeMap e
- where
- e ∷ Scheme → MaybeT IO HostMap
- {-# INLINE e #-}
- e = const (fail (⊥))
- {-# INLINE mappend #-}
- mappend = insert
-
--- |Any 'Map's from 'Scheme' to 'HostMap' are also 'SchemeMapper's.
-instance Map α Scheme HostMap ⇒ SchemeMapper α where
- {-# INLINE findHostMap #-}
- findHostMap = (maybe (fail (⊥)) return ∘) ∘ lookup
-
--- |An IO-based scheme mapper.
-instance SchemeMapper (Scheme → MaybeT IO HostMap) where
- {-# INLINE findHostMap #-}
- findHostMap = flip id
-
--- |A pure scheme mapper.
-instance SchemeMapper (Scheme → Maybe HostMap) where
- {-# INLINE findHostMap #-}
- findHostMap = (maybe (fail (⊥)) return ∘) ∘ flip id
-