Types.purs 2.9 KB
Newer Older
1 2
module Gargantext.Types where

3 4
import Data.Argonaut ( class DecodeJson, decodeJson, class EncodeJson, encodeJson
                     , jsonEmptyObject, (:=), (~>), (.?), (.??) )
5
import Data.Maybe (Maybe(..))
6
import Data.Either (Either(..))
7
import Prim.Row (class Union)
James Laver's avatar
James Laver committed
8 9
import URI.Query (Query)
import Gargantext.Prelude
10

11
data TermSize = MonoTerm | MultiTerm
12

13 14
data Term = Term String TermList

15
derive instance eqTermSize :: Eq TermSize
16

James Laver's avatar
James Laver committed
17 18 19 20
-- | Converts a data structure to a query string
class ToQuery a where
  toQuery :: a -> Query

21
instance showTermSize :: Show TermSize where
22 23 24
  show MonoTerm  = "MonoTerm"
  show MultiTerm = "MultiTerm"

25 26 27 28
readTermSize :: String -> Maybe TermSize
readTermSize "MonoTerm"  = Just MonoTerm
readTermSize "MultiTerm" = Just MultiTerm
readTermSize _           = Nothing
29

30 31
termSizes :: Array { desc :: String, mval :: Maybe TermSize }
termSizes = [ { desc: "All types",        mval: Nothing        }
32 33 34 35 36
            , { desc: "One-word terms",   mval: Just MonoTerm  }
            , { desc: "Multi-word terms", mval: Just MultiTerm }
            ]

data TermList = GraphTerm | StopTerm | CandidateTerm
37
-- TODO use generic JSON instance
38 39

derive instance eqTermList :: Eq TermList
40
derive instance ordTermList :: Ord TermList
41

42
instance encodeJsonTermList :: EncodeJson TermList where
43 44 45
  encodeJson GraphTerm     = encodeJson "GraphTerm"
  encodeJson StopTerm      = encodeJson "StopTerm"
  encodeJson CandidateTerm = encodeJson "CandidateTerm"
46

47
instance decodeJsonTermList :: DecodeJson TermList where
48 49 50
  decodeJson json = do
    s <- decodeJson json
    case s of
51 52 53
      "GraphTerm"     -> pure GraphTerm
      "StopTerm"      -> pure StopTerm
      "CandidateTerm" -> pure CandidateTerm
54
      _               -> Left "Unexpected list name"
55 56 57 58 59 60 61 62 63

type ListTypeId = Int

listTypeId :: TermList -> ListTypeId
listTypeId GraphTerm     = 1
listTypeId StopTerm      = 2
listTypeId CandidateTerm = 3

instance showTermList :: Show TermList where
64 65 66
  show GraphTerm     = "GraphTerm"
  show StopTerm      = "StopTerm"
  show CandidateTerm = "CandidateTerm"
67

68 69 70 71 72 73
-- TODO: Can we replace the show instance above with this?
termListName :: TermList -> String
termListName GraphTerm = "Graph List"
termListName StopTerm = "Stop List"
termListName CandidateTerm = "Candidate List"

74
readTermList :: String -> Maybe TermList
75 76 77 78
readTermList "GraphTerm"     = Just GraphTerm
readTermList "StopTerm"      = Just StopTerm
readTermList "CandidateTerm" = Just CandidateTerm
readTermList _               = Nothing
79 80 81 82 83 84 85 86

termLists :: Array { desc :: String, mval :: Maybe TermList }
termLists = [ { desc: "All terms",   mval: Nothing      }
            , { desc: "Graph terms",   mval: Just GraphTerm   }
            , { desc: "Stop terms",  mval: Just StopTerm  }
            , { desc: "Candidate terms", mval: Just CandidateTerm }
            ]

87 88 89
-- | Proof that row `r` is a subset of row `s`
class Optional (r :: # Type) (s :: # Type)
instance optionalInstance :: Union r t s => Optional r s