--- |'emptyResource' is a resource definition with no actual
--- handlers. You can construct a 'ResourceDef' by selectively
--- overriding 'emptyResource'. It is defined as follows:
---
--- @
--- emptyResource = ResourceDef {
--- resUsesNativeThread = False
--- , resIsGreedy = False
--- , resGet = Nothing
--- , resHead = Nothing
--- , resPost = Nothing
--- , resPut = Nothing
--- , resDelete = Nothing
--- }
--- @
-emptyResource ∷ ResourceDef
-emptyResource = ResourceDef {
- resUsesNativeThread = False
- , resIsGreedy = False
- , resGet = Nothing
- , resHead = Nothing
- , resPost = Nothing
- , resPut = Nothing
- , resDelete = Nothing
- }
-
-spawnResource ∷ ResourceDef → NormalInteraction → IO ThreadId
-spawnResource (ResourceDef {..}) ni@(NI {..})
- = fork $ run `catch` processException
+instance Monoid Resource where
+ {-# INLINE mempty #-}
+ mempty
+ = Resource {
+ resGet = Nothing
+ , resHead = Nothing
+ , resPost = Nothing
+ , resPut = Nothing
+ , resDelete = Nothing
+ }
+ {-# INLINEABLE mappend #-}
+ mappend a b
+ = Resource {
+ resGet = resGet a <|> resGet b
+ , resHead = resHead a <|> resHead b
+ , resPost = resPost a <|> resPost b
+ , resPut = resPut a <|> resPut b
+ , resDelete = resDelete a <|> resDelete b
+ }
+
+instance Unfoldable Resource (Method, Rsrc ()) where
+ {-# INLINEABLE insert #-}
+ insert (GET , a) r = r { resGet = Just a }
+ insert (HEAD , a) r = r { resHead = Just a }
+ insert (POST , a) r = r { resPost = Just a }
+ insert (PUT , a) r = r { resPut = Just a }
+ insert (DELETE, a) r = r { resDelete = Just a }
+ insert _ r = r
+ {-# INLINE empty #-}
+ empty = (∅)
+
+instance Foldable Resource (Method, Rsrc ()) where
+ {-# INLINEABLE foldMap #-}
+ foldMap f (Resource {..})
+ = maybe (∅) (f ∘ ((,) GET )) resGet ⊕
+ maybe (∅) (f ∘ ((,) HEAD )) resHead ⊕
+ maybe (∅) (f ∘ ((,) POST )) resPost ⊕
+ maybe (∅) (f ∘ ((,) PUT )) resPut ⊕
+ maybe (∅) (f ∘ ((,) DELETE)) resDelete
+
+instance Collection Resource (Method, Rsrc ()) where
+ {-# INLINE filter #-}
+ filter = (fromList ∘) ∘ (∘ fromFoldable) ∘ filter
+
+spawnRsrc ∷ Resource → NormalInteraction → IO ThreadId
+spawnRsrc (Resource {..}) ni@(NI {..})
+ = forkIO $ run `catch` processException