{-# LANGUAGE DataKinds #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} module Coolbal.DataVersionTag where import Data.Binary import GHC.TypeNats data DataVersionTag (n :: Nat) = DataVersionTag deriving (Show, Eq, Ord) instance (KnownNat n, n <= 255) => Binary (DataVersionTag n) where put v = putWord8 (fromIntegral (natVal v)) get = do b <- getWord8 let result = DataVersionTag :: DataVersionTag n if b == fromIntegral (natVal result) then return result else fail ("DataVersionTag: read version " ++ show b ++ ", application expects version " ++ show (natVal result))