Add statusFromNat function to retrieve Status from a KnownNat

This commit is contained in:
Giorgio Marinelli 2021-10-19 19:02:54 +02:00
parent 9e8f508026
commit 7fce9ec7f1

View File

@ -1,11 +1,16 @@
{-# LANGUAGE DataKinds #-}
-- Flexible instances is necessary on GHC 8.4 and earlier
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Servant.API.Status where
import Network.HTTP.Types.Status
import GHC.TypeLits
-- | Retrieve a known or unknown Status from a KnownNat
statusFromNat :: forall a proxy. KnownNat a => proxy a -> Status
statusFromNat = toEnum . fromInteger . natVal
-- | Witness that a type-level natural number corresponds to a HTTP status code
class KnownNat n => KnownStatus n where
statusVal :: proxy n -> Status