3 | module Language.LSP.Message.Utils
5 | import public Data.OneOf
7 | import Language.JSON.Interfaces
8 | import Language.LSP.Message.Derive
18 | MkNull == MkNull = True
22 | compare MkNull MkNull = EQ
26 | show MkNull = "null"
30 | toJSON MkNull = JNull
34 | fromJSON JNull = pure MkNull
35 | fromJSON _ = neutral
39 | toEither : OneOf [a, b] -> Either a b
40 | toEither (Here x) = Left x
41 | toEither (There (Here x)) = Right x
45 | fromEither : Either a b -> OneOf [a, b]
46 | fromEither (Left x) = make x
47 | fromEither (Right x) = make x
50 | toMaybe : OneOf [a, Null] -> Maybe a
51 | toMaybe (Here x) = Just x
52 | toMaybe (There (Here MkNull)) = Nothing
55 | fromMaybe : Maybe a -> OneOf [a, Null]
56 | fromMaybe (Just x) = make x
57 | fromMaybe Nothing = make MkNull
60 | ConstraintList : (Type -> Type) -> List Type -> Type
61 | ConstraintList f [] = ()
62 | ConstraintList f (x :: xs) = (f x, ConstraintList f xs)
65 | ConstraintList ToJSON as => ToJSON (OneOf as) where
66 | toJSON (Here x) = toJSON x
67 | toJSON (There x) = toJSON x
70 | {as : _} -> ConstraintList FromJSON as => FromJSON (OneOf as) where
73 | fromJSON {as = []} @{()} v = Nothing
74 | fromJSON {as = (x :: xs)} @{(c, cs)} v = (There <$> fromJSON v) <|> (Here <$> fromJSON v)