File: Line.hs

package info (click to toggle)
git-annex 8.20210223-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 68,764 kB
  • sloc: haskell: 70,359; javascript: 9,103; sh: 1,304; makefile: 212; perl: 136; ansic: 44
file content (38 lines) | stat: -rw-r--r-- 1,226 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{- line based log files
 -
 - Copyright 2019 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module Logs.Line where

import Common

import qualified Data.Attoparsec.ByteString.Lazy as A
import Data.Attoparsec.ByteString.Char8 (isEndOfLine)
import qualified Data.DList as D

{- Applies a parser to each line of a log file.
 -
 - If the parser fails to parse a line, that line is skipped, instead of
 - the overall parse failing. This is generally a good idea in case a newer
 - version of git-annex somehow changed the format of the log file.
 -
 - Any combination of \r and \n are taken to be the end of the line.
 - (Some versions of git-annex on Windows wrote \r into git-annex branch
 - files, and multiple \r's sometimes accumulated.)
 -
 - The parser does not itself need to avoid parsing beyond the end of line;
 - this is implemented only pass the content of a line to the parser.
 -}
parseLogLines :: A.Parser a -> A.Parser [a]
parseLogLines parser = go D.empty
  where
	go dl = do
		line <- A.takeTill isEndOfLine
		A.skipWhile isEndOfLine
		let dl' = case A.parseOnly parser line of
			Left _ -> dl
			Right v -> D.snoc dl v
		(A.endOfInput *> return (D.toList dl')) <|> go dl'