Permalink
Browse files

Add Portage.PackageId types and parser and pretty printer

Not used yet elsewhere but should replace P2 etc.
The main point is that Portage.PackageId is an instance of the
Cabal.Package class. That allows us to construct 'PackageIndex'es
of Portage.PackageId or things identified by a Portage.PackageId
  • Loading branch information...
1 parent e2ccc12 commit 7b0d2383b17ffdda19f6394afffa6df2f8ef3eb0 @dcoutts dcoutts committed Sep 1, 2008
Showing with 55 additions and 0 deletions.
  1. +55 −0 Portage/PackageId.hs
View
@@ -0,0 +1,55 @@
+-- | Portage package identifiers, which unlike Cabal ones include a category.
+--
+module Portage.PackageId (
+ Category(..),
+ PackageName(..),
+ PackageId(..),
+ ) where
+
+import qualified Distribution.Package as Cabal
+import Distribution.Version
+import Distribution.Text (Text(..))
+
+import qualified Distribution.Compat.ReadP as Parse
+import qualified Text.PrettyPrint as Disp
+import Text.PrettyPrint ((<>))
+import qualified Data.Char as Char ( isAlphaNum )
+
+
+newtype Category = Category String
+ deriving (Eq, Ord, Show, Read)
+
+data PackageName = PackageName Category Cabal.PackageName
+ deriving (Eq, Ord, Show, Read)
+
+data PackageId = PackageId PackageName Version
+ deriving (Eq, Ord, Show, Read)
+
+instance Cabal.Package PackageId where
+ packageId (PackageId (PackageName _ n) v) = Cabal.PackageIdentifier n v
+
+instance Text Category where
+ disp (Category c) = Disp.text c
+ parse = fmap Category (Parse.munch1 categoryChar)
+ where
+ categoryChar c = Char.isAlphaNum c || c == '-'
+
+instance Text PackageName where
+ disp (PackageName category name) =
+ disp category <> Disp.char '/' <> disp name
+
+ parse = do
+ category <- parse
+ Parse.char '/'
+ name <- parse
+ return (PackageName category name)
+
+instance Text PackageId where
+ disp (PackageId name version) =
+ disp name <> Disp.char '-' <> disp version
+
+ parse = do
+ name <- parse
+ Parse.char '-'
+ version <- parse
+ return (PackageId name version)

0 comments on commit 7b0d238

Please sign in to comment.