From aed478153db1c2fa0c2e659a36dcc8dd9eb454ba Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Sun, 14 May 2023 21:34:50 +0800 Subject: [PATCH] Print alternative day if day not found --- app/ghcup/GHCup/OptParse/Common.hs | 4 +++- lib/GHCup/Errors.hs | 7 +++++-- lib/GHCup/Utils.hs | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/ghcup/GHCup/OptParse/Common.hs b/app/ghcup/GHCup/OptParse/Common.hs index 28a23fd..ad05c34 100644 --- a/app/ghcup/GHCup/OptParse/Common.hs +++ b/app/ghcup/GHCup/OptParse/Common.hs @@ -724,7 +724,9 @@ fromVersion' (SetToolTag Latest) tool = do bimap mkTVer Just <$> getLatest dls tool ?? TagNotFound Latest tool fromVersion' (SetToolDay day) tool = do GHCupInfo { _ghcupDownloads = dls } <- lift getGHCupInfo - bimap mkTVer Just <$> getByReleaseDay dls tool day ?? DayNotFound day tool + bimap mkTVer Just <$> case getByReleaseDay dls tool day of + Left ad -> throwE $ DayNotFound day tool ad + Right v -> pure v fromVersion' (SetToolTag LatestPrerelease) tool = do GHCupInfo { _ghcupDownloads = dls } <- lift getGHCupInfo bimap mkTVer Just <$> getLatestPrerelease dls tool ?? TagNotFound LatestPrerelease tool diff --git a/lib/GHCup/Errors.hs b/lib/GHCup/Errors.hs index afb4436..4fb29b2 100644 --- a/lib/GHCup/Errors.hs +++ b/lib/GHCup/Errors.hs @@ -314,12 +314,15 @@ instance HFErrorProject TagNotFound where eDesc _ = "Unable to find a tag of a tool" -- | Unable to find a release day of a tool -data DayNotFound = DayNotFound Day Tool +data DayNotFound = DayNotFound Day Tool (Maybe Day) deriving Show instance Pretty DayNotFound where - pPrint (DayNotFound day tool) = + pPrint (DayNotFound day tool Nothing) = text "Unable to find release date" <+> text (show day) <+> text "of tool" <+> pPrint tool + pPrint (DayNotFound day tool (Just alternateDay)) = + text "Unable to find release date" <+> text (show day) <+> text "of tool" <+> pPrint tool <+> + text "but found an alternative date" <+> text (show alternateDay) instance HFErrorProject DayNotFound where eBase _ = 95 diff --git a/lib/GHCup/Utils.hs b/lib/GHCup/Utils.hs index 147a95b..275d403 100644 --- a/lib/GHCup/Utils.hs +++ b/lib/GHCup/Utils.hs @@ -94,7 +94,8 @@ import qualified Streamly.Prelude as S import Control.DeepSeq (force) import GHC.IO (evaluate) import System.Environment (getEnvironment, setEnv) -import Data.Time (Day) +import Data.Time (Day(..), diffDays, addDays) +import Debug.Trace -- $setup @@ -890,8 +891,17 @@ getTagged tag = to (Map.toDescList . Map.filter (\VersionInfo {..} -> tag `elem` _viTags)) % folding id -getByReleaseDay :: GHCupDownloads -> Tool -> Day -> Maybe (Version, VersionInfo) -getByReleaseDay av tool day = headOf (ix tool % getByReleaseDayFold day) av +getByReleaseDay :: GHCupDownloads -> Tool -> Day -> Either (Maybe Day) (Version, VersionInfo) +getByReleaseDay av tool day = let mvv = fromMaybe mempty $ headOf (ix tool) av + mdv = Map.foldrWithKey (\k vi@VersionInfo{..} m -> + maybe m (\d -> let diff = diffDays d day + in Map.insert (abs diff) (diff, (k, vi)) m) _viReleaseDay) + Map.empty mvv + in case headMay (Map.toAscList mdv) of + Nothing -> Left Nothing + Just (absDiff, (diff, (k, vi))) + | absDiff == 0 -> Right (k, vi) + | otherwise -> Left (Just (addDays diff day)) getByReleaseDayFold :: Day -> Fold (Map.Map Version VersionInfo) (Version, VersionInfo) getByReleaseDayFold day = to (Map.toDescList . Map.filter (\VersionInfo {..} -> Just day == _viReleaseDay)) % folding id