-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
154 additions
and
516 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
lib/wallet/api/http/Cardano/Wallet/Api/Types/RestorationMode.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE InstanceSigs #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module Cardano.Wallet.Api.Types.RestorationMode | ||
( RestorationMode (..) | ||
) where | ||
|
||
import Prelude | ||
|
||
import Data.Aeson | ||
( FromJSON (parseJSON) | ||
, KeyValue ((.=)) | ||
, ToJSON (toJSON) | ||
, Value (..) | ||
, object | ||
, (.:) | ||
) | ||
import Data.Aeson.Types | ||
( Parser | ||
) | ||
import Data.Quantity | ||
( Quantity (Quantity) | ||
) | ||
import Data.Time | ||
( UTCTime | ||
) | ||
import Data.Time.Format.ISO8601 | ||
( iso8601ParseM | ||
, iso8601Show | ||
) | ||
import Data.Word | ||
( Word32 | ||
) | ||
import GHC.Generics | ||
( Generic | ||
) | ||
|
||
import qualified Data.Text as T | ||
|
||
data RestorationMode | ||
= RestoreFromGenesis | ||
| RestoreFromSlot (Quantity "slot" Word32) | ||
| RestoreFromTip | ||
| RestoreFromDate UTCTime | ||
| RestoreFromBlock (Quantity "block" Word32) | ||
deriving (Eq, Show, Generic) | ||
|
||
instance ToJSON RestorationMode where | ||
toJSON :: RestorationMode -> Value | ||
toJSON = \case | ||
RestoreFromGenesis -> object [ "unit" .= String "genesis" ] | ||
RestoreFromSlot (Quantity s) -> | ||
object | ||
[ "unit" .= String "slot" | ||
, "quantity" .= s | ||
] | ||
RestoreFromTip -> object [ "unit" .= String "tip" ] | ||
RestoreFromDate t -> object | ||
[ "unit" .= String "time" | ||
, "value" .= String (T.pack $ iso8601Show t) | ||
] | ||
RestoreFromBlock (Quantity b) -> | ||
object | ||
[ "unit" .= String "block" | ||
, "quantity" .= b | ||
] | ||
|
||
instance FromJSON RestorationMode where | ||
parseJSON :: Value -> Parser RestorationMode | ||
parseJSON = \case | ||
Object o -> do | ||
unit <- o .: "unit" | ||
case unit of | ||
String "genesis" -> pure RestoreFromGenesis | ||
String "slot" -> do | ||
s <- o .: "quantity" | ||
pure $ RestoreFromSlot (Quantity s) | ||
String "tip" -> pure RestoreFromTip | ||
String "time" -> do | ||
t <- o .: "value" | ||
case iso8601ParseM (T.unpack t) of | ||
Nothing -> fail "Invalid date format" | ||
Just t' -> pure $ RestoreFromDate t' | ||
String "block" -> do | ||
b <- o .: "quantity" | ||
pure $ RestoreFromBlock (Quantity b) | ||
_ -> fail "Invalid restoration unit" | ||
_ -> fail "Invalid restoration mode" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
lib/wallet/test/data/Cardano/Wallet/Api/RestorationMode.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.