@@ -22,13 +22,16 @@ import Distribution.Client.IndexUtils.Timestamp (Timestamp)
22
22
import Distribution.Client.Types.RepoName (RepoName (.. ))
23
23
24
24
import Distribution.FieldGrammar.Described
25
- import Distribution.Parsec (Parsec (.. ))
25
+ import Distribution.Parsec (Parsec (.. ), parsecLeadingCommaList )
26
26
import Distribution.Pretty (Pretty (.. ))
27
27
28
28
import qualified Data.Map.Strict as Map
29
29
import qualified Distribution.Compat.CharParsing as P
30
30
import qualified Text.PrettyPrint as Disp
31
31
32
+ -- $setup
33
+ -- >>> import Distribution.Parsec
34
+
32
35
-------------------------------------------------------------------------------
33
36
-- Total index state
34
37
-------------------------------------------------------------------------------
@@ -44,42 +47,50 @@ instance NFData TotalIndexState
44
47
instance Pretty TotalIndexState where
45
48
pretty (TIS IndexStateHead m)
46
49
| not (Map. null m)
47
- = Disp. hsep
48
- [ pretty rn <<>> Disp. colon <<> > pretty idx
50
+ = Disp. hsep $ Disp. punctuate Disp. comma
51
+ [ pretty rn Disp. <+ > pretty idx
49
52
| (rn, idx) <- Map. toList m
50
53
]
51
54
pretty (TIS def m) = foldl' go (pretty def) (Map. toList m) where
52
- go doc (rn, idx) = doc Disp. <+> pretty rn <<>> Disp. colon <<> > pretty idx
55
+ go doc (rn, idx) = doc <<>> Disp. comma Disp. <+> pretty rn Disp. <+ > pretty idx
53
56
57
+ -- |
58
+ --
59
+ -- >>> simpleParsec "HEAD" :: Maybe TotalIndexState
60
+ -- Just (TIS IndexStateHead (fromList []))
61
+ --
62
+ -- >>> simpleParsec "" :: Maybe TotalIndexState
63
+ -- Just (TIS IndexStateHead (fromList []))
64
+ --
65
+ -- >>> simpleParsec "hackage.haskell.org HEAD" :: Maybe TotalIndexState
66
+ -- Just (TIS IndexStateHead (fromList []))
67
+ --
68
+ -- >>> simpleParsec "2020-02-04T12:34:56Z, hackage.haskell.org HEAD" :: Maybe TotalIndexState
69
+ -- Just (TIS (IndexStateTime (TS 1580819696)) (fromList [(RepoName "hackage.haskell.org",IndexStateHead)]))
70
+ --
71
+ -- >>> simpleParsec "hackage.haskell.org 2020-02-04T12:34:56Z" :: Maybe TotalIndexState
72
+ -- Just (TIS IndexStateHead (fromList [(RepoName "hackage.haskell.org",IndexStateTime (TS 1580819696))]))
73
+ --
54
74
instance Parsec TotalIndexState where
55
- parsec = normalise . foldl' add headTotalIndexState <$> some (single0 <* P. spaces) where
56
- -- hard to do without try
57
- -- 2020-03-21T11:22:33Z looks like it begins with
58
- -- repository name 2020-03-21T11
59
- --
60
- -- To make this easy, we could forbid repository names starting with digit
61
- --
62
- single0 = P. try single1 <|> TokTimestamp <$> parsec
63
- single1 = do
64
- token <- P. munch1 (\ c -> isAlphaNum c || c == ' _' || c == ' -' || c == ' .' )
65
- single2 token <|> single3 token
66
-
67
- single2 token = do
68
- _ <- P. char ' :'
69
- idx <- parsec
70
- return (TokRepo (RepoName token) idx)
71
-
72
- single3 " HEAD" = return TokHead
73
- single3 token = P. unexpected (" Repository " ++ token ++ " without index state (after comma)" )
75
+ parsec = normalise . foldl' add headTotalIndexState <$> parsecLeadingCommaList single0 where
76
+ single0 = startsWithRepoName <|> TokTimestamp <$> parsec
77
+ startsWithRepoName = do
78
+ reponame <- parsec
79
+ -- the "HEAD" is technically a valid reponame...
80
+ if reponame == RepoName " HEAD"
81
+ then return TokHead
82
+ else do
83
+ P. spaces
84
+ TokRepo reponame <$> parsec
74
85
75
86
add :: TotalIndexState -> Tok -> TotalIndexState
76
87
add _ TokHead = headTotalIndexState
77
88
add _ (TokTimestamp ts) = TIS (IndexStateTime ts) Map. empty
78
89
add (TIS def m) (TokRepo rn idx) = TIS def (Map. insert rn idx m)
79
90
80
91
instance Described TotalIndexState where
81
- describe _ = REMunch1 RESpaces1 $ REUnion
82
- [ describe (Proxy :: Proxy RepoName ) <> reChar ' : ' <> ris
92
+ describe _ = reCommaList $ REUnion
93
+ [ describe (Proxy :: Proxy RepoName ) <> RESpaces1 <> ris
83
94
, ris
84
95
]
85
96
where
0 commit comments