Skip to content

Commit 14610e1

Browse files
committed
Improve download size parsing
1 parent fb3af46 commit 14610e1

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/NOM/Parser.hs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import Data.Attoparsec.ByteString.Char8 (
1414
double,
1515
endOfLine,
1616
isEndOfLine,
17+
skipSpace,
1718
takeTill,
19+
try,
1820
)
1921
import NOM.Builds (
2022
Derivation (..),
@@ -80,10 +82,28 @@ planDownloads =
8082
, string "these " *> (decimal :: Parser Int) *> string " paths"
8183
]
8284
*> string " will be fetched ("
83-
*> double
85+
*> byteSize
8486
)
85-
<*> (string " MiB download, " *> double)
86-
<*> (string " MiB unpacked):" *> endOfLine *> (fromList <$> many planDownloadLine))
87+
<*> (" download, " *> byteSize)
88+
<*> (" unpacked):" *> endOfLine *> (fromList <$> many planDownloadLine))
89+
90+
byteSize :: Parser Double
91+
byteSize = do
92+
num <- double
93+
skipSpace
94+
unit <- anyChar
95+
_ <- try (string "iB")
96+
power <- case unit of
97+
'K' -> pure 1
98+
'M' -> pure 2
99+
'G' -> pure 3
100+
'T' -> pure 4
101+
'P' -> pure 5
102+
'E' -> pure 6
103+
'Z' -> pure 7
104+
'Y' -> pure 8
105+
x -> fail $ "Unknown unit: " <> [x] <> "iB"
106+
pure $ num * (1024 ** power)
87107

88108
planDownloadLine :: Parser StorePath
89109
planDownloadLine = indent *> storePathByteStringParser <* endOfLine

test/Property.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ main = do
4545
assertEqual
4646
"result matches"
4747
( PlanDownloads
48-
134.19
49-
1863.82
48+
(134.19 * 1024 ** 2)
49+
(1863.82 * 1024 ** 2)
5050
(singleton (StorePath "60zb5dndaw1fzir3s69sy3xhy19gll1p" "ghc-8.8.2"))
5151
)
5252
result2

0 commit comments

Comments
 (0)