cleans up read instance considerably

This commit is contained in:
Aaron Greenspan
2020-01-05 23:42:43 -07:00
parent f7522e6678
commit a3d2811253

View File

@@ -14,8 +14,6 @@ import System.Directory
import System.FilePath
import Yesod.Core
import Data.Text (isSuffixOf)
import Constants
import Lib.Semver
import Lib.Types.Semver
@@ -80,13 +78,16 @@ instance KnownSymbol a => Show (Extension a) where
show e@(Extension file) = file <.> extension e
instance KnownSymbol a => Read (Extension a) where
readsPrec _ s = [(Extension . take (m - n - 1) $ s, "") | toS ext' `isSuffixOf` toS s]
readsPrec _ s = [(Extension fileName, "") | ("" <.> fileExt) == ("" <.> ext')]
where
m = length s
(fileName, fileExt) = splitExtension s
ext' = extension (def :: Extension a)
n = if length ext' == 0
then -1
else length ext'
withPeriod :: String -> String
withPeriod word@(a:_) = case a of
'.' -> word
_ -> "." <> word
withPeriod word = word
instance KnownSymbol a => PathPiece (Extension a) where
fromPathPiece = readMaybe . toS