Skip to content

Commit

Permalink
Core, Remote: handcrafted code clean-up (#134)
Browse files Browse the repository at this point in the history
`brittany` was used.

Then all changes passed through manual supervision.

Then handcrafted code cleanup was done.

All changes are pure lambda code refactoring,
there should be no changes to the functionality.
  • Loading branch information
Anton-Latukha committed Feb 3, 2021
1 parent ff200aa commit 792c76b
Show file tree
Hide file tree
Showing 30 changed files with 791 additions and 663 deletions.
2 changes: 1 addition & 1 deletion hnix-store-core/Setup.hs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import Distribution.Simple
import Distribution.Simple
main = defaultMain
9 changes: 5 additions & 4 deletions hnix-store-core/src/System/Nix/Base32.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{-|
Description: Implementation of Nix's base32 encoding.
-}
module System.Nix.Base32 (
encode
module System.Nix.Base32
( encode
, decode
) where
)
where

import System.Nix.Internal.Base32
import System.Nix.Internal.Base32
22 changes: 10 additions & 12 deletions hnix-store-core/src/System/Nix/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
Description : Build related types
Maintainer : srk <srk@48.io>
|-}
module System.Nix.Build (
BuildMode(..)
module System.Nix.Build
( BuildMode(..)
, BuildStatus(..)
, BuildResult(..)
, buildSuccess
) where
)
where

import Data.Time (UTCTime)
import Data.Text (Text)
import Data.Time ( UTCTime )
import Data.Text ( Text )

-- keep the order of these Enums to match enums from reference implementations
-- src/libstore/store-api.hh
Expand Down Expand Up @@ -49,12 +50,9 @@ data BuildResult = BuildResult
startTime :: !UTCTime
, -- Stop time of this build
stopTime :: !UTCTime
} deriving (Eq, Ord, Show)
}
deriving (Eq, Ord, Show)

buildSuccess :: BuildResult -> Bool
buildSuccess BuildResult{..} =
status `elem`
[ Built
, Substituted
, AlreadyValid
]
buildSuccess BuildResult {..} =
status `elem` [Built, Substituted, AlreadyValid]
27 changes: 15 additions & 12 deletions hnix-store-core/src/System/Nix/Derivation.hs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
{-# LANGUAGE OverloadedStrings #-}

module System.Nix.Derivation (
parseDerivation
module System.Nix.Derivation
( parseDerivation
, buildDerivation
) where
)
where

import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Lazy.Builder as Text.Lazy (Builder)
import qualified Data.Text.Lazy.Builder as Text.Lazy.Builder
import qualified Data.Attoparsec.Text.Lazy as Text.Lazy (Parser)
import Nix.Derivation (Derivation)
import qualified Nix.Derivation as Derivation
import System.Nix.StorePath (StorePath)
import qualified System.Nix.StorePath as StorePath
import Data.Text ( Text )
import qualified Data.Text as Text
import qualified Data.Text.Lazy.Builder as Text.Lazy
( Builder )
import qualified Data.Text.Lazy.Builder as Text.Lazy.Builder
import qualified Data.Attoparsec.Text.Lazy as Text.Lazy
( Parser )
import Nix.Derivation ( Derivation )
import qualified Nix.Derivation as Derivation
import System.Nix.StorePath ( StorePath )
import qualified System.Nix.StorePath as StorePath



Expand Down
9 changes: 5 additions & 4 deletions hnix-store-core/src/System/Nix/Hash.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{-|
Description : Cryptographic hashes for hnix-store.
-}
module System.Nix.Hash (
Hash.Digest
module System.Nix.Hash
( Hash.Digest

, Hash.HashAlgorithm(..)
, Hash.ValidAlgo(..)
Expand All @@ -16,6 +16,7 @@ module System.Nix.Hash (
, Hash.BaseEncoding(..)
, Hash.encodeInBase
, Hash.decodeBase
) where
)
where

import qualified System.Nix.Internal.Hash as Hash
import qualified System.Nix.Internal.Hash as Hash
59 changes: 32 additions & 27 deletions hnix-store-core/src/System/Nix/Internal/Base32.hs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module System.Nix.Internal.Base32 where


import Data.Maybe (fromMaybe)
import Data.ByteString (ByteString)
import qualified Data.ByteString as Bytes
import qualified Data.ByteString.Char8 as Bytes.Char8
import Data.Bool ( bool )
import Data.Maybe ( fromMaybe )
import Data.ByteString ( ByteString )
import qualified Data.ByteString as Bytes
import qualified Data.ByteString.Char8 as Bytes.Char8
import qualified Data.Text
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import Data.Text (Text)
import Data.Bits (shiftR)
import Data.Word (Word8)
import Data.List (unfoldr)
import Numeric (readInt)
import Data.Vector ( Vector )
import qualified Data.Vector as Vector
import Data.Text ( Text )
import Data.Bits ( shiftR )
import Data.Word ( Word8 )
import Data.List ( unfoldr )
import Numeric ( readInt )


-- omitted: E O U T
Expand All @@ -32,7 +33,7 @@ encode c = Data.Text.pack $ map char32 [nChar - 1, nChar - 2 .. 0]
-- the - 1 inside of it.
nChar = fromIntegral $ ((Bytes.length c * 8 - 1) `div` 5) + 1

byte = Bytes.index c . fromIntegral
byte = Bytes.index c . fromIntegral

-- May need to switch to a more efficient calculation at some
-- point.
Expand All @@ -52,30 +53,34 @@ encode c = Data.Text.pack $ map char32 [nChar - 1, nChar - 2 .. 0]
-- | Decode Nix's base32 encoded text
decode :: Text -> Either String ByteString
decode what =
if Data.Text.all (`elem` digits32) what
then unsafeDecode what
else Left "Invalid base32 string"
bool
(Left "Invalid Base32 string")
(unsafeDecode what)
(Data.Text.all (`elem` digits32) what)

-- | Decode Nix's base32 encoded text
-- Doesn't check if all elements match `digits32`
unsafeDecode :: Text -> Either String ByteString
unsafeDecode what =
case readInt 32
(`elem` digits32)
(\c -> fromMaybe (error "character not in digits32")
$ Vector.findIndex (==c) digits32)
(Data.Text.unpack what)
case
readInt
32
(`elem` digits32)
(\c -> fromMaybe (error "character not in digits32")
$ Vector.findIndex (== c) digits32
)
(Data.Text.unpack what)
of
[(i, _)] -> Right $ padded $ integerToBS i
x -> Left $ "Can't decode: readInt returned " ++ show x
where
padded x
| Bytes.length x < decLen = x `Bytes.append` bstr
| otherwise = x
where
bstr = Bytes.Char8.pack $ take (decLen - Bytes.length x) (cycle "\NUL")
where
padded x
| Bytes.length x < decLen = x `Bytes.append` bstr
| otherwise = x
where
bstr = Bytes.Char8.pack $ take (decLen - Bytes.length x) (cycle "\NUL")

decLen = Data.Text.length what * 5 `div` 8
decLen = Data.Text.length what * 5 `div` 8

-- | Encode an Integer to a bytestring
-- Similar to Data.Base32String (integerToBS) without `reverse`
Expand Down
Loading

0 comments on commit 792c76b

Please sign in to comment.