module Language.Haskell.LSP.Types.Progress where

import qualified Data.Aeson as A
import Data.Text (Text)

-- | A token used to report progress back or return partial results for a
-- specific request.
-- @since 0.17.0.0
data ProgressToken
    = ProgressNumericToken Int
    | ProgressTextToken Text
    deriving (Int -> ProgressToken -> ShowS
[ProgressToken] -> ShowS
ProgressToken -> String
(Int -> ProgressToken -> ShowS)
-> (ProgressToken -> String)
-> ([ProgressToken] -> ShowS)
-> Show ProgressToken
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ProgressToken] -> ShowS
$cshowList :: [ProgressToken] -> ShowS
show :: ProgressToken -> String
$cshow :: ProgressToken -> String
showsPrec :: Int -> ProgressToken -> ShowS
$cshowsPrec :: Int -> ProgressToken -> ShowS
Show, ReadPrec [ProgressToken]
ReadPrec ProgressToken
Int -> ReadS ProgressToken
ReadS [ProgressToken]
(Int -> ReadS ProgressToken)
-> ReadS [ProgressToken]
-> ReadPrec ProgressToken
-> ReadPrec [ProgressToken]
-> Read ProgressToken
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ProgressToken]
$creadListPrec :: ReadPrec [ProgressToken]
readPrec :: ReadPrec ProgressToken
$creadPrec :: ReadPrec ProgressToken
readList :: ReadS [ProgressToken]
$creadList :: ReadS [ProgressToken]
readsPrec :: Int -> ReadS ProgressToken
$creadsPrec :: Int -> ReadS ProgressToken
Read, ProgressToken -> ProgressToken -> Bool
(ProgressToken -> ProgressToken -> Bool)
-> (ProgressToken -> ProgressToken -> Bool) -> Eq ProgressToken
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ProgressToken -> ProgressToken -> Bool
$c/= :: ProgressToken -> ProgressToken -> Bool
== :: ProgressToken -> ProgressToken -> Bool
$c== :: ProgressToken -> ProgressToken -> Bool
Eq, Eq ProgressToken
Eq ProgressToken
-> (ProgressToken -> ProgressToken -> Ordering)
-> (ProgressToken -> ProgressToken -> Bool)
-> (ProgressToken -> ProgressToken -> Bool)
-> (ProgressToken -> ProgressToken -> Bool)
-> (ProgressToken -> ProgressToken -> Bool)
-> (ProgressToken -> ProgressToken -> ProgressToken)
-> (ProgressToken -> ProgressToken -> ProgressToken)
-> Ord ProgressToken
ProgressToken -> ProgressToken -> Bool
ProgressToken -> ProgressToken -> Ordering
ProgressToken -> ProgressToken -> ProgressToken
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ProgressToken -> ProgressToken -> ProgressToken
$cmin :: ProgressToken -> ProgressToken -> ProgressToken
max :: ProgressToken -> ProgressToken -> ProgressToken
$cmax :: ProgressToken -> ProgressToken -> ProgressToken
>= :: ProgressToken -> ProgressToken -> Bool
$c>= :: ProgressToken -> ProgressToken -> Bool
> :: ProgressToken -> ProgressToken -> Bool
$c> :: ProgressToken -> ProgressToken -> Bool
<= :: ProgressToken -> ProgressToken -> Bool
$c<= :: ProgressToken -> ProgressToken -> Bool
< :: ProgressToken -> ProgressToken -> Bool
$c< :: ProgressToken -> ProgressToken -> Bool
compare :: ProgressToken -> ProgressToken -> Ordering
$ccompare :: ProgressToken -> ProgressToken -> Ordering
$cp1Ord :: Eq ProgressToken
Ord)

instance A.ToJSON ProgressToken where
    toJSON :: ProgressToken -> Value
toJSON (ProgressNumericToken Int
i) = Int -> Value
forall a. ToJSON a => a -> Value
A.toJSON Int
i
    toJSON (ProgressTextToken Text
t) = Text -> Value
forall a. ToJSON a => a -> Value
A.toJSON Text
t

instance A.FromJSON ProgressToken where
    parseJSON :: Value -> Parser ProgressToken
parseJSON (A.String Text
t) = ProgressToken -> Parser ProgressToken
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ProgressToken -> Parser ProgressToken)
-> ProgressToken -> Parser ProgressToken
forall a b. (a -> b) -> a -> b
$ Text -> ProgressToken
ProgressTextToken Text
t
    parseJSON (A.Number Scientific
i) = Int -> ProgressToken
ProgressNumericToken (Int -> ProgressToken) -> Parser Int -> Parser ProgressToken
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> Parser Int
forall a. FromJSON a => Value -> Parser a
A.parseJSON (Scientific -> Value
A.Number Scientific
i)
    parseJSON Value
v = String -> Parser ProgressToken
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser ProgressToken) -> String -> Parser ProgressToken
forall a b. (a -> b) -> a -> b
$ String
"Invalid progress token: " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Value -> String
forall a. Show a => a -> String
show Value
v