Types.purs 2.54 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
derive instance eqTermSize :: Eq TermSize
13

14
instance showTermSize :: Show TermSize where
15 16 17
  show MonoTerm  = "MonoTerm"
  show MultiTerm = "MultiTerm"

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

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

data TermList = GraphTerm | StopTerm | CandidateTerm
30
-- TODO use generic JSON instance
31 32

derive instance eqTermList :: Eq TermList
33
derive instance ordTermList :: Ord TermList
34

35
instance encodeJsonTermList :: EncodeJson TermList where
36 37 38
  encodeJson GraphTerm     = encodeJson "GraphTerm"
  encodeJson StopTerm      = encodeJson "StopTerm"
  encodeJson CandidateTerm = encodeJson "CandidateTerm"
39

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

type ListTypeId = Int

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

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

readTermList :: String -> Maybe TermList
62 63 64 65
readTermList "GraphTerm"     = Just GraphTerm
readTermList "StopTerm"      = Just StopTerm
readTermList "CandidateTerm" = Just CandidateTerm
readTermList _               = Nothing
66 67 68 69 70 71 72 73

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

74 75 76
-- | 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