bpath (empty) → 0.1.0
raw patch · 7 files changed
+266/−0 lines, 7 filesdep +basedep +bpathdep +bytestring
Dependencies added: base, bpath, bytestring, containers, megaparsec, template-haskell, th-lift-instances
Files
- CHANGELOG.md +5/−0
- LICENSE +11/−0
- README.md +5/−0
- bpath.cabal +76/−0
- src/Path.hs +27/−0
- src/Path/Internal.hs +138/−0
- test/Spec.hs +4/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# bpath++## 0.1.0++Initial release
+ LICENSE view
@@ -0,0 +1,11 @@+Copyright (c) 2020 Poscat++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,5 @@+# bpath++[](http://hackage.haskell.org/package/bpath)++A minimal type unix path library. If you need to do IO consider using `path-io` instead.
+ bpath.cabal view
@@ -0,0 +1,76 @@+cabal-version: 3.0+name: bpath+version: 0.1.0+synopsis: A minimal typed unix path library+description: A minimal typed unix path library+category: System+license: BSD-3-Clause+license-file: LICENSE+author: Poscat+maintainer: Poscat <poscat@mail.poscat.moe>+copyright: Copyright (c) Poscat 2020+stability: alpha+homepage: https://github.com/poscat0x04/bpath+bug-reports: https://github.com/poscat0x04/bpath/issues+extra-doc-files:+ CHANGELOG.md+ README.md++tested-with: GHC ==8.10.3++common common-attrs+ build-depends: base >=4.10 && <5+ default-language: Haskell2010+ default-extensions:+ NoStarIsType+ BangPatterns+ ConstraintKinds+ DataKinds+ DeriveFoldable+ DeriveFunctor+ DeriveGeneric+ DeriveTraversable+ DuplicateRecordFields+ EmptyCase+ FlexibleContexts+ FlexibleInstances+ FunctionalDependencies+ InstanceSigs+ KindSignatures+ LambdaCase+ MultiWayIf+ OverloadedStrings+ PartialTypeSignatures+ PatternSynonyms+ RecordWildCards+ ScopedTypeVariables+ TupleSections+ TypeApplications+ TypeFamilies+ TypeOperators+ UnicodeSyntax+ ViewPatterns++library+ import: common-attrs+ build-depends:+ , bytestring >=0.10.12 && <0.12+ , containers ^>=0.6.2+ , megaparsec ^>=9.0.1+ , template-haskell ^>=2.16.0+ , th-lift-instances ^>=0.1.18++ exposed-modules: Path+ other-modules: Path.Internal+ hs-source-dirs: src++test-suite bpath-test+ import: common-attrs+ type: exitcode-stdio-1.0+ build-depends: bpath+ hs-source-dirs: test+ main-is: Spec.hs++source-repository head+ type: git+ location: https://github.com/poscat0x04/bpath
+ src/Path.hs view
@@ -0,0 +1,27 @@+module Path+ ( -- * The basic path type+ Path,+ PathType (..),++ -- * Converting between 'ByteString' and 'Path'+ parseAbs,+ parseRel,+ fromAbs,+ fromRel,++ -- * Operations on paths+ (</>),+ stripPrefix,+ isPrefixOf,+ parent,+ filename,++ -- * Template Haskell stuff+ mkAbs,+ mkRel,+ absp,+ relp,+ )+where++import Path.Internal
+ src/Path/Internal.hs view
@@ -0,0 +1,138 @@+{-# LANGUAGE DeriveLift #-}+{-# LANGUAGE DerivingStrategies #-}+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE QuasiQuotes #-}++module Path.Internal where++import Data.ByteString (ByteString)+import Data.Foldable+import Data.Maybe+import Data.Sequence+import Data.String+import Data.Void+import GHC.Generics (Generic)+import Instances.TH.Lift ()+import Language.Haskell.TH+import Language.Haskell.TH.Quote+import Language.Haskell.TH.Syntax+import Text.Megaparsec++type Parser = Parsec Void ByteString++data PathSeg+ = Parent+ | PathSeg {-# UNPACK #-} !ByteString+ deriving (Show, Eq, Lift, Generic)++fromPathSeg :: PathSeg -> ByteString+fromPathSeg Parent = ".."+fromPathSeg (PathSeg p) = p++pathSeg :: Parser (Maybe PathSeg)+pathSeg = try parentP <|> try dot <|> normalSeg+ where+ parentP = do+ _ <- single 46+ _ <- single 46+ pure (Just Parent)+ normalSeg = Just . PathSeg <$> takeWhile1P Nothing (/= 47)+ dot = do+ _ <- single 46+ pure Nothing++pathSeg' :: Parser (Maybe PathSeg)+pathSeg' = pathSeg <|> pure Nothing++relpath :: Parser (Path 'Rel)+relpath = do+ h <- pathSeg+ t <- many $ single 47 *> pathSeg'+ pure $ Path $ fromList $ catMaybes $ h : t++abspath :: Parser (Path 'Abs)+abspath = do+ _ <- single 47+ l <- sepBy pathSeg' $ single 47+ pure $ Path $ fromList $ catMaybes l++data PathType+ = Abs+ | Rel+ deriving (Show, Eq)++-- | A canonicalized file path+newtype Path (t :: PathType) = Path+ { unPath :: Seq PathSeg+ }+ deriving stock (Generic, Lift)+ deriving newtype (Eq, Show)++fromRel :: Path 'Rel -> ByteString+fromRel (fmap fromPathSeg . unPath -> l)+ | Empty <- l = "."+ | x :<| xs <- l = foldl' (\p s -> p <> "/" <> s) x xs++fromAbs :: Path 'Abs -> ByteString+fromAbs (fmap fromPathSeg . unPath -> l)+ | Empty <- l = "/"+ | x :<| xs <- l = "/" <> foldl' (\p s -> p <> "/" <> s) x xs++hush :: Either a b -> Maybe b+hush (Left _) = Nothing+hush (Right b) = Just b++parseRel :: ByteString -> Maybe (Path 'Rel)+parseRel = hush . runParser relpath ""++parseAbs :: ByteString -> Maybe (Path 'Abs)+parseAbs = hush . runParser abspath ""++mkAbs :: ByteString -> Q Exp+mkAbs = lift . fromMaybe (error "illformed absolute path") . parseAbs++mkRel :: ByteString -> Q Exp+mkRel = lift . fromMaybe (error "illformed relative path") . parseRel++qq :: (ByteString -> Q Exp) -> QuasiQuoter+qq quoteExp' =+ QuasiQuoter+ { quoteExp = quoteExp' . fromString,+ quotePat = \_ ->+ fail "illegal QuasiQuote (allowed as expression only, used as a pattern)",+ quoteType = \_ ->+ fail "illegal QuasiQuote (allowed as expression only, used as a type)",+ quoteDec = \_ ->+ fail "illegal QuasiQuote (allowed as expression only, used as a declaration)"+ }++absp :: QuasiQuoter+absp = qq mkAbs++relp :: QuasiQuoter+relp = qq mkRel++(</>) :: Path t -> Path 'Rel -> Path t+(Path p1) </> (Path p2) = Path (p1 <> p2)++stripPrefix :: Path t -> Path t -> Maybe (Path 'Rel)+stripPrefix (Path p1) (Path p2) =+ Path <$> stripPrefix' p1 p2+ where+ stripPrefix' Empty p = pure p+ stripPrefix' _ Empty = Nothing+ stripPrefix' (x :<| xs) (y :<| ys)+ | x == y = stripPrefix' xs ys+ | otherwise = Nothing++isPrefixOf :: Path t -> Path t -> Bool+isPrefixOf p1 p2 = isJust $ stripPrefix p1 p2++parent :: Path t -> Path t+parent (Path Empty) = Path Empty+parent (Path (xs :|> _)) = Path xs++filename :: Path t -> Path 'Rel+filename (Path Empty) = Path Empty+filename (Path (Empty :|> x)) = Path $ pure x+filename (Path (_ :<| xs)) = filename $ Path xs
+ test/Spec.hs view
@@ -0,0 +1,4 @@+module Main where++main :: IO ()+main = putStrLn "unimplemented"