summaryrefslogtreecommitdiff
path: root/Portage/Use.hs
blob: 0f29f08e84d6a7b9b69b7c0d0a278f5ac555bdd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{-# LANGUAGE CPP #-}

module Portage.Use (
  -- * main structures
  UseFlag(..),
  Use(..),
  dispUses,
  -- * helpers
  mkUse,
  mkNotUse,
  mkQUse
  ) where

import qualified Text.PrettyPrint as Disp
import Text.PrettyPrint ((<>))
import qualified Distribution.Text as DT

#if MIN_VERSION_base(4,11,0)
import Prelude hiding ((<>))
#endif

-- | Use variable modificator
data UseFlag = UseFlag Use           -- ^ no modificator
             | E UseFlag             -- ^ = modificator (Equiv    mark)
             | Q UseFlag             -- ^ ? modificator (Question mark)
             | X UseFlag             -- ^ ! modificator (eXclamation mark)
             | N UseFlag             -- ^ - modificator 
             deriving (Eq,Show,Ord,Read)

mkUse :: Use -> UseFlag
mkUse  = UseFlag 

mkNotUse :: Use -> UseFlag
mkNotUse = N . UseFlag

mkQUse :: Use -> UseFlag
mkQUse = Q . UseFlag

instance DT.Text UseFlag where
  disp = showModificator
  parse = error "instance DT.Text UseFlag: not implemented"

showModificator :: UseFlag -> Disp.Doc
showModificator (UseFlag u) = DT.disp u
showModificator (X u)     = Disp.char '!' <> DT.disp u
showModificator (Q u)     = DT.disp u <> Disp.char '?'
showModificator (E u)     = DT.disp u <> Disp.char '='
showModificator (N u)     = Disp.char '-' <> DT.disp u

dispUses :: [UseFlag] -> Disp.Doc
dispUses [] = Disp.empty
dispUses us = Disp.brackets $ Disp.hcat $ (Disp.punctuate (Disp.text ", ")) $ map DT.disp  us

newtype Use = Use String
    deriving (Eq, Read, Show)

instance Ord Use where
    compare (Use a) (Use b) = case (a,b) of
        ("test", "test") -> EQ
        ("test", _)      -> LT
        (_, "test")      -> GT
        (_, _)           -> a `compare` b

instance DT.Text Use where
    disp (Use u) = Disp.text u
    parse = error "instance DT.Text Use: not implemented"