packages feed

hwm-0.5.0 / src / HWM / Core / Parsing.hs

{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE NoImplicitPrelude #-}module HWM.Core.Parsing  ( parseField,    fromByteString,    firstWord,    removeHead,    unconsM,    sepBy,    fromToString,    SourceText,    genUrl,    Parse (..),    parseOptions,    parsePkgString,    ParseCLI (..),    flag,    ignoreSpaces,    parseLS,  )whereimport Data.ByteString.Char8 (unpack)import Data.Char (isSeparator)import Data.Text  ( break,    drop,    intercalate,    pack,    singleton,    splitOn,    strip,    uncons,  )import qualified Data.Text as Timport Options.Applicative (Mod, Parser, help, long, short, strOption, switch)import Options.Applicative.Builder (OptionFields)import Relude hiding  ( break,    drop,    head,    intercalate,    isPrefixOf,    null,    uncons,    words,  )type SourceText = Textflag :: Char -> String -> String -> Parser Boolflag s l h = switch (long l <> short s <> help h)parseOptions :: Mod OptionFields Text -> Parser [Text]parseOptions x = fmap (>>= parseLS) (many (strOption x))parseLS :: Text -> [Text]parseLS = map T.strip . T.splitOn ","parseField :: SourceText -> (SourceText, SourceText)parseField = second (strip . drop 1) . breakAt (== ':')firstWord :: SourceText -> (SourceText, SourceText)firstWord = breakAt isSeparatorfromByteString :: ByteString -> SourceTextfromByteString = pack . unpackignoreSpaces :: SourceText -> SourceTextignoreSpaces = T.filter (not . isSeparator)breakAt :: (Char -> Bool) -> SourceText -> (SourceText, SourceText)breakAt f = bimap strip strip . break f . stripsepBy :: (MonadFail m, Parse a) => SourceText -> SourceText -> m [a]sepBy sep = traverse parse . splitOn sep . ignoreSpacesremoveHead :: Char -> SourceText -> (Bool, SourceText)removeHead should txt = maybe (False, txt) has (uncons txt)  where    has (x, xs)      | x == should = (True, xs)      | otherwise = (False, txt)unconsM :: (MonadFail m) => String -> SourceText -> m (SourceText, SourceText)unconsM m x = first singleton <$> maybe (fail $ m <> "<>: " <> toString x) pure (uncons x)fromToString :: (ToString a) => a -> SourceTextfromToString = pack . toStringgenUrl :: Text -> [Text] -> TextgenUrl domain = intercalate "/" . (domain :)class Parse a where  parse :: (MonadFail m) => Text -> m aclass ParseCLI a where  parseCLI :: Parser ainstance Parse Int where  parse t =    maybe (fail $ "Could not parse Int: '" <> toString t <> "'!") pure (readMaybe $ toString t)parsePkgString :: Text -> (Text, Text)parsePkgString input =  let base = T.takeWhile (/= '@') input      (namePart, versionPart) = T.breakOnEnd "-" base   in if T.null versionPart || T.null namePart        then (base, "")        else (T.dropEnd 1 namePart, versionPart)