{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ViewPatterns #-} module Collection where import Data.Text qualified as T import Text.ParserCombinators.ReadP qualified as R import Text.ParserCombinators.ReadPrec qualified as R import Text.Read (Read (..)) newtype Collection = Collection {name :: T.Text} deriving (Read, Eq, Show) data CollectionItem = CollectionItem { collection :: Collection, itemFileName :: FilePath } deriving (Eq) instance Read CollectionItem where readPrec = R.lift $ do (Collection . T.pack -> collection) <- R.munch (/= '/') _ <- R.string "/" itemFileName <- do itemFileName <- R.munch (liftA2 (&&) (/= '.') (/= '/')) fileExt <- R.string ".json" pure (itemFileName <> fileExt) pure CollectionItem {..} instance Show CollectionItem where show (CollectionItem {collection = Collection cn, itemFileName}) = show (cn <> "/" <> T.pack itemFileName)