From 00c6216259d8a7b131307953ba5000d2b5dc564b Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 13 Jun 2020 00:06:18 +0200 Subject: compiler: trivial code simplifications Following HLint's advice. --- compiler/src/Files.hs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'compiler/src/Files.hs') diff --git a/compiler/src/Files.hs b/compiler/src/Files.hs index c769815..40149e1 100644 --- a/compiler/src/Files.hs +++ b/compiler/src/Files.hs @@ -30,6 +30,7 @@ module Files import Data.List (isPrefixOf, length, subsequences, sortOn) import Data.Function ((&)) +import Data.Functor ((<&>)) import Data.Text (pack) import Data.Aeson (ToJSON) import qualified Data.Aeson as JSON @@ -53,7 +54,7 @@ type LocalPath = String type WebPath = String -- | Reversed path component list -data Path = Path [FileName] deriving Show +newtype Path = Path [FileName] deriving Show instance ToJSON Path where toJSON = JSON.String . pack . webPath @@ -120,7 +121,7 @@ isHidden = hiddenName . nodeName -- | DFS with intermediate dirs first. flattenDir :: FSNode -> [FSNode] flattenDir file@File{} = [file] -flattenDir dir@Dir{items} = dir:(concatMap flattenDir items) +flattenDir dir@Dir{items} = dir:concatMap flattenDir items -- | Filters a dir tree. The root is always returned. filterDir :: (FSNode -> Bool) -> AnchoredFSNode -> AnchoredFSNode @@ -133,7 +134,7 @@ filterDir cond (AnchoredFSNode anchor root) = filter cond items & map filterNode & Dir path canonicalPath readDirectory :: LocalPath -> IO AnchoredFSNode -readDirectory root = mkNode (Path []) >>= return . AnchoredFSNode root +readDirectory root = AnchoredFSNode root <$> mkNode (Path []) where mkNode :: Path -> IO FSNode mkNode path = @@ -151,10 +152,10 @@ readDirectory root = mkNode (Path []) >>= return . AnchoredFSNode root mkDirNode :: Path -> FilePath -> IO FSNode mkDirNode path canonicalPath = - (listDirectory $ localPath (root /> path)) - >>= mapM (mkNode . ((>= return . sortOn nodeName - >>= return . Dir path canonicalPath + listDirectory (localPath (root /> path)) + >>= mapM (mkNode . (path sortOn nodeName + <&> Dir path canonicalPath copyTo :: FilePath -> AnchoredFSNode -> IO () copyTo target AnchoredFSNode{anchor, root} = copyNode root -- cgit v1.2.3 From 34b90f08a21fbe3f1928e16a8ea48f1fc7453e4e Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 15 Jun 2020 05:34:33 +0200 Subject: compiler/Files: simplify subPaths computation Ignoring subsequences that aren't rooted --- compiler/src/Files.hs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'compiler/src/Files.hs') diff --git a/compiler/src/Files.hs b/compiler/src/Files.hs index 40149e1..1f14e7f 100644 --- a/compiler/src/Files.hs +++ b/compiler/src/Files.hs @@ -28,7 +28,7 @@ module Files ) where -import Data.List (isPrefixOf, length, subsequences, sortOn) +import Data.List (isPrefixOf, length, sortOn) import Data.Function ((&)) import Data.Functor ((<&>)) import Data.Text (pack) @@ -81,7 +81,10 @@ fileName (Path (name:_)) = Just name fileName _ = Nothing subPaths :: Path -> [Path] -subPaths (Path path) = map Path $ subsequences path +subPaths (Path path) = map Path $ subpaths path + where + subpaths [] = [] + subpaths full@(_:r) = full : subpaths r pathLength :: Path -> Int pathLength (Path path) = Data.List.length path -- cgit v1.2.3 From ce2210e6deff1d981186b6d7ddb1176f27e41f49 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 13 Jun 2020 03:41:39 +0200 Subject: compiler: make GalleryIndex loadable from JSON --- compiler/src/Files.hs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'compiler/src/Files.hs') diff --git a/compiler/src/Files.hs b/compiler/src/Files.hs index 1f14e7f..023546b 100644 --- a/compiler/src/Files.hs +++ b/compiler/src/Files.hs @@ -20,7 +20,7 @@ module Files ( FileName, LocalPath, WebPath, Path(..) , (), (), (<.>) , fileName, subPaths, pathLength - , localPath, webPath + , localPath, webPath, fromWebPath , FSNode(..), AnchoredFSNode(..) , nodeName, isHidden, flattenDir, filterDir , readDirectory, copyTo @@ -31,8 +31,8 @@ module Files import Data.List (isPrefixOf, length, sortOn) import Data.Function ((&)) import Data.Functor ((<&>)) -import Data.Text (pack) -import Data.Aeson (ToJSON) +import Data.Text (pack, unpack) +import Data.Aeson (ToJSON, FromJSON) import qualified Data.Aeson as JSON import System.Directory @@ -59,8 +59,11 @@ newtype Path = Path [FileName] deriving Show instance ToJSON Path where toJSON = JSON.String . pack . webPath +instance FromJSON Path where + parseJSON = JSON.withText "Path" (return . fromWebPath . unpack) + instance Eq Path where - (Path left) == (Path right) = left == right + left == right = webPath left == webPath right () :: Path -> Path -> Path (Path l) (Path r) = Path (r ++ l) @@ -95,6 +98,9 @@ localPath (Path path) = System.FilePath.joinPath $ reverse path webPath :: Path -> WebPath webPath (Path path) = System.FilePath.Posix.joinPath $ reverse path +fromWebPath :: WebPath -> Path +fromWebPath = Path . reverse . System.FilePath.Posix.splitDirectories + data FSNode = File -- cgit v1.2.3