Skip to content

Commit

Permalink
remote: delete obsolete serialization prims and instances
Browse files Browse the repository at this point in the history
  • Loading branch information
sorki committed Dec 7, 2023
1 parent 8a74c62 commit 6d306c5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 508 deletions.
6 changes: 0 additions & 6 deletions hnix-store-remote/hnix-store-remote.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ library
, System.Nix.Store.Remote.Client.Core
, System.Nix.Store.Remote.Logger
, System.Nix.Store.Remote.MonadStore
, System.Nix.Store.Remote.Serialize
, System.Nix.Store.Remote.Serialize.Prim
, System.Nix.Store.Remote.Serializer
, System.Nix.Store.Remote.Server
, System.Nix.Store.Remote.Socket
Expand Down Expand Up @@ -174,7 +172,6 @@ test-suite remote
Data.SerializerSpec
EnumSpec
NixSerializerSpec
SerializeSpec
build-tool-depends:
hspec-discover:hspec-discover
build-depends:
Expand All @@ -183,14 +180,11 @@ test-suite remote
, hnix-store-remote
, hnix-store-tests
, bytestring
, cereal
, crypton
, some > 1.0.5 && < 2
, text
, time
, hspec
, QuickCheck
, unordered-containers

test-suite remote-io
import: tests
Expand Down
53 changes: 48 additions & 5 deletions hnix-store-remote/src/Data/Serializer/Example.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ import Data.ByteString (ByteString)
import Data.Int (Int8)
import Data.GADT.Show (GShow(..), defaultGshowsPrec)
import Data.Kind (Type)
import Data.Type.Equality
import Data.Serialize.Get (getInt8)
import Data.Serialize.Put (putInt8)
import Data.Type.Equality (TestEquality(..), (:~:)(Refl))
import Data.Serialize.Get (Get, getInt8)
import Data.Serialize.Put (Putter, PutM, putInt8)
import Data.Serializer
( Serializer(..)
, GetSerializerError
, runGetS
, runPutS
, transformGetError
, transformPutError
)
import Data.Some (Some(..))
import GHC.Generics
import System.Nix.Store.Remote.Serialize.Prim (getBool, putBool, getEnum, putEnum)
import GHC.Generics (Generic)

import Test.QuickCheck (Arbitrary(..), oneof)

Expand Down Expand Up @@ -274,3 +280,40 @@ cmdSRest = Serializer
else lift (putInt8 i)
Some (Cmd_Bool b) -> putS opcode OpCode_Bool >> lift (putBool b)
}

-- Primitives helpers

getInt :: Integral a => Get a
getInt = fromIntegral <$> getInt8

putInt :: Integral a => Putter a
putInt = putInt8 . fromIntegral

-- | Deserialize @Bool@ from integer
getBool :: Get Bool
getBool = (getInt :: Get Int8) >>= \case
0 -> pure False
1 -> pure True
x -> fail $ "illegal bool value " ++ show x

-- | Serialize @Bool@ into integer
putBool :: Putter Bool
putBool True = putInt (1 :: Int8)
putBool False = putInt (0 :: Int8)

-- | Utility toEnum version checking bounds using Bounded class
toEnumCheckBounds :: Enum a => Int -> Either String a
toEnumCheckBounds = \case
x | x < minBound -> Left $ "enum out of min bound " ++ show x
x | x > maxBound -> Left $ "enum out of max bound " ++ show x
x | otherwise -> Right $ toEnum x

-- | Deserialize @Enum@ to integer
getEnum :: Enum a => Get a
getEnum =
toEnumCheckBounds <$> getInt
>>= either fail pure

-- | Serialize @Enum@ to integer
putEnum :: Enum a => Putter a
putEnum = putInt . fromEnum
185 changes: 0 additions & 185 deletions hnix-store-remote/src/System/Nix/Store/Remote/Serialize.hs

This file was deleted.

Loading

0 comments on commit 6d306c5

Please sign in to comment.