avoid using MonadFail in ParseDuration

There's no instance for Either String, so that makes it not as useful as
it could be, so instead just return an Either String.
This commit is contained in:
Joey Hess 2020-08-15 15:53:35 -04:00
parent ad64079b44
commit 4c58433c48
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
7 changed files with 13 additions and 15 deletions

View file

@ -286,7 +286,7 @@ keyMatchingOptions' =
<> help "match files the repository wants to drop" <> help "match files the repository wants to drop"
<> hidden <> hidden
) )
, globalSetter Limit.addAccessedWithin $ option (str >>= parseDuration) , globalSetter Limit.addAccessedWithin $ option (eitherReader parseDuration)
( long "accessedwithin" ( long "accessedwithin"
<> metavar paramTime <> metavar paramTime
<> help "match files accessed within a time interval" <> help "match files accessed within a time interval"
@ -403,7 +403,7 @@ jobsOption =
timeLimitOption :: [GlobalOption] timeLimitOption :: [GlobalOption]
timeLimitOption = timeLimitOption =
[ globalSetter Limit.addTimeLimit $ option (str >>= parseDuration) [ globalSetter Limit.addTimeLimit $ option (eitherReader parseDuration)
( long "time-limit" <> short 'T' <> metavar paramTime ( long "time-limit" <> short 'T' <> metavar paramTime
<> help "stop after the specified amount of time" <> help "stop after the specified amount of time"
<> hidden <> hidden

View file

@ -39,7 +39,7 @@ optParser _ = AssistantOptions
( long "autostart" ( long "autostart"
<> help "start in known repositories" <> help "start in known repositories"
) )
<*> optional (option (str >>= parseDuration) <*> optional (option (eitherReader parseDuration)
( long "startdelay" <> metavar paramNumber ( long "startdelay" <> metavar paramNumber
<> help "delay before running startup scan" <> help "delay before running startup scan"
)) ))

View file

@ -103,8 +103,8 @@ parseExpire ps = do
return (Just r, parsetime now t) return (Just r, parsetime now t)
parsetime _ "never" = Nothing parsetime _ "never" = Nothing
parsetime now s = case parseDuration s of parsetime now s = case parseDuration s of
Nothing -> giveup $ "bad expire time: " ++ s Right d -> Just (now - durationToPOSIXTime d)
Just d -> Just (now - durationToPOSIXTime d) Left e -> giveup $ "bad expire time: " ++ e
parseActivity :: MonadFail m => String -> m Activity parseActivity :: MonadFail m => String -> m Activity
parseActivity s = case readish s of parseActivity s = case readish s of

View file

@ -81,7 +81,7 @@ optParser desc = FsckOptions
( long "more" <> short 'm' ( long "more" <> short 'm'
<> help "continue an incremental fsck" <> help "continue an incremental fsck"
) )
<|> (ScheduleIncrementalO <$> option (str >>= parseDuration) <|> (ScheduleIncrementalO <$> option (eitherReader parseDuration)
( long "incremental-schedule" <> metavar paramTime ( long "incremental-schedule" <> metavar paramTime
<> help "schedule incremental fscking" <> help "schedule incremental fscking"
)) ))

View file

@ -177,7 +177,7 @@ extractGitConfig configsource r = GitConfig
, annexFsckNudge = getbool (annexConfig "fscknudge") True , annexFsckNudge = getbool (annexConfig "fscknudge") True
, annexAutoUpgrade = toAutoUpgrade $ , annexAutoUpgrade = toAutoUpgrade $
getmaybe (annexConfig "autoupgrade") getmaybe (annexConfig "autoupgrade")
, annexExpireUnused = maybe Nothing Just . parseDuration , annexExpireUnused = either (const Nothing) Just . parseDuration
<$> getmaybe (annexConfig "expireunused") <$> getmaybe (annexConfig "expireunused")
, annexSecureEraseCommand = getmaybe (annexConfig "secure-erase-command") , annexSecureEraseCommand = getmaybe (annexConfig "secure-erase-command")
, annexGenMetaData = getbool (annexConfig "genmetadata") False , annexGenMetaData = getbool (annexConfig "genmetadata") False

View file

@ -46,16 +46,15 @@ parseScheduledActivity :: String -> Either String ScheduledActivity
parseScheduledActivity s = case words s of parseScheduledActivity s = case words s of
("fsck":"self":d:rest) -> qualified $ ScheduledSelfFsck ("fsck":"self":d:rest) -> qualified $ ScheduledSelfFsck
<$> parseSchedule (unwords rest) <$> parseSchedule (unwords rest)
<*> getduration d <*> parseDuration d
("fsck":u:d:rest) -> qualified $ ScheduledRemoteFsck ("fsck":u:d:rest) -> qualified $ ScheduledRemoteFsck
<$> pure (toUUID u) <$> pure (toUUID u)
<*> parseSchedule (unwords rest) <*> parseSchedule (unwords rest)
<*> getduration d <*> parseDuration d
_ -> qualified $ Left "unknown activity" _ -> qualified $ Left "unknown activity"
where where
qualified (Left e) = Left $ e ++ " in \"" ++ s ++ "\"" qualified (Left e) = Left $ e ++ " in \"" ++ s ++ "\""
qualified v = v qualified v = v
getduration d = maybe (Left $ "failed to parse duration \""++d++"\"") Right (parseDuration d)
fromScheduledActivities :: [ScheduledActivity] -> String fromScheduledActivities :: [ScheduledActivity] -> String
fromScheduledActivities = intercalate "; " . map fromScheduledActivity fromScheduledActivities = intercalate "; " . map fromScheduledActivity

View file

@ -19,7 +19,6 @@ module Utility.HumanTime (
import Utility.PartialPrelude import Utility.PartialPrelude
import Utility.QuickCheck import Utility.QuickCheck
import Control.Monad.Fail as Fail (MonadFail(..))
import qualified Data.Map as M import qualified Data.Map as M
import Data.Time.Clock import Data.Time.Clock
import Data.Time.Clock.POSIX (POSIXTime) import Data.Time.Clock.POSIX (POSIXTime)
@ -45,8 +44,8 @@ daysToDuration :: Integer -> Duration
daysToDuration i = Duration $ i * dsecs daysToDuration i = Duration $ i * dsecs
{- Parses a human-input time duration, of the form "5h", "1m", "5h1m", etc -} {- Parses a human-input time duration, of the form "5h", "1m", "5h1m", etc -}
parseDuration :: MonadFail m => String -> m Duration parseDuration :: String -> Either String Duration
parseDuration = maybe parsefail (return . Duration) . go 0 parseDuration d = maybe parsefail (Right . Duration) $ go 0 d
where where
go n [] = return n go n [] = return n
go n s = do go n s = do
@ -56,7 +55,7 @@ parseDuration = maybe parsefail (return . Duration) . go 0
u <- M.lookup c unitmap u <- M.lookup c unitmap
go (n + num * u) rest go (n + num * u) rest
_ -> return $ n + num _ -> return $ n + num
parsefail = Fail.fail "duration parse error; expected eg \"5m\" or \"1h5m\"" parsefail = Left $ "failed to parse duration \"" ++ d ++ "\" (expected eg \"5m\" or \"1h5m\")"
fromDuration :: Duration -> String fromDuration :: Duration -> String
fromDuration Duration { durationSeconds = d } fromDuration Duration { durationSeconds = d }
@ -102,4 +101,4 @@ instance Arbitrary Duration where
arbitrary = Duration <$> nonNegative arbitrary arbitrary = Duration <$> nonNegative arbitrary
prop_duration_roundtrips :: Duration -> Bool prop_duration_roundtrips :: Duration -> Bool
prop_duration_roundtrips d = parseDuration (fromDuration d) == Just d prop_duration_roundtrips d = parseDuration (fromDuration d) == Right d