Types.purs 2.79 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 Gargantext.Prelude
8
import Prim.Row (class Union)
9

10
data TermSize = MonoTerm | MultiTerm
11

12 13
data Term = Term String TermList

14
derive instance eqTermSize :: Eq TermSize
15

16
instance showTermSize :: Show TermSize where
17 18 19
  show MonoTerm  = "MonoTerm"
  show MultiTerm = "MultiTerm"

20 21 22 23
readTermSize :: String -> Maybe TermSize
readTermSize "MonoTerm"  = Just MonoTerm
readTermSize "MultiTerm" = Just MultiTerm
readTermSize _           = Nothing
24

25 26
termSizes :: Array { desc :: String, mval :: Maybe TermSize }
termSizes = [ { desc: "All types",        mval: Nothing        }
27 28 29 30 31
            , { desc: "One-word terms",   mval: Just MonoTerm  }
            , { desc: "Multi-word terms", mval: Just MultiTerm }
            ]

data TermList = GraphTerm | StopTerm | CandidateTerm
32
-- TODO use generic JSON instance
33 34

derive instance eqTermList :: Eq TermList
35
derive instance ordTermList :: Ord TermList
36

37
instance encodeJsonTermList :: EncodeJson TermList where
38 39 40
  encodeJson GraphTerm     = encodeJson "GraphTerm"
  encodeJson StopTerm      = encodeJson "StopTerm"
  encodeJson CandidateTerm = encodeJson "CandidateTerm"
41

42
instance decodeJsonTermList :: DecodeJson TermList where
43 44 45
  decodeJson json = do
    s <- decodeJson json
    case s of
46 47 48
      "GraphTerm"     -> pure GraphTerm
      "StopTerm"      -> pure StopTerm
      "CandidateTerm" -> pure CandidateTerm
49
      _               -> Left "Unexpected list name"
50 51 52 53 54 55 56 57 58

type ListTypeId = Int

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

instance showTermList :: Show TermList where
59 60 61
  show GraphTerm     = "GraphTerm"
  show StopTerm      = "StopTerm"
  show CandidateTerm = "CandidateTerm"
62

63 64 65 66 67 68
-- 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"

69
readTermList :: String -> Maybe TermList
70 71 72 73
readTermList "GraphTerm"     = Just GraphTerm
readTermList "StopTerm"      = Just StopTerm
readTermList "CandidateTerm" = Just CandidateTerm
readTermList _               = Nothing
74 75 76 77 78 79 80 81

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 }
            ]

82 83 84
-- | 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