module GitHUD.Git.Parse.Count (
  getCount
  ) where

import Text.Parsec (parse)
import Text.Parsec.String (Parser)
import Text.Parsec.Char (anyChar)
import Text.Parsec.Prim (many)

getCount :: String -> Int
getCount :: String -> Int
getCount String
numberString =
  (ParseError -> Int) -> (Int -> Int) -> Either ParseError Int -> Int
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
    (Int -> ParseError -> Int
forall a b. a -> b -> a
const Int
0)
    Int -> Int
forall a. a -> a
id
    (Parsec String () Int -> String -> String -> Either ParseError Int
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec String () Int
countParser String
"" String
numberString)

countParser :: Parser Int
countParser :: Parsec String () Int
countParser = do
  String
number <- ParsecT String () Identity Char
-> ParsecT String () Identity String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
anyChar
  if String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
number
    then Int -> Parsec String () Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0
    else Int -> Parsec String () Int
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Int
forall a. Read a => String -> a
read String
number)