Skip to content

Commit

Permalink
Remove more unused functions from Cardano.Api.TextView
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Jul 7, 2020
1 parent 97b3e95 commit 5712496
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 108 deletions.
94 changes: 0 additions & 94 deletions cardano-api/src/Cardano/Api/TextView.hs
Expand Up @@ -9,8 +9,6 @@ module Cardano.Api.TextView
, TextViewError (..)
, TextViewType (..)
, TextViewDescription (..)
, parseTextView
, renderTextView
, renderTextViewError
, expectTextViewOfType
, textViewJSONConfig
Expand All @@ -19,14 +17,7 @@ module Cardano.Api.TextView

-- * File IO support
, TextViewFileError (..)
, readTextViewFile
, readTextViewFileOfType
, renderTextViewFileError
, writeTextViewFile

-- * Exported for testing.
, rawToMultilineHex
, unRawToMultilineHex
) where

import Cardano.Prelude
Expand All @@ -35,17 +26,12 @@ import Prelude (String)
import Data.Aeson (FromJSON(..), ToJSON(..), object,
withObject, (.=), (.:))
import Data.Aeson.Encode.Pretty (Config(..), defConfig, keyOrder)
import Data.Attoparsec.ByteString (Parser)
import qualified Data.Attoparsec.ByteString.Char8 as Atto
import qualified Data.ByteString.Base16 as Base16
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.Char (isSpace)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text

import Cardano.Binary
import Control.Monad.Trans.Except.Extra


newtype TextViewType
Expand Down Expand Up @@ -120,26 +106,6 @@ renderTextViewError tve =
TextViewAesonDecodeError decErr -> "TextView aeson decode error: " <> textShow decErr
TextViewDecodeError decErr -> "TextView decode error: " <> textShow decErr

-- | Parse a 'TextView' from the external serialised format.
--
-- TODO: Do not use this to parse TextView as TextView is now serialized to JSON
-- Need to remove once the old api has been removed
parseTextView :: ByteString -> Either TextViewError TextView
parseTextView =
first (\str -> TextViewFormatError . Text.pack $ "Cardano.Api.TextView.parseTextView: " ++ str) . Atto.parseOnly pTextView


-- | Render a 'TextView' into the external serialised format.
--
renderTextView :: TextView -> ByteString
renderTextView tv =
BS.unlines $
[ "type: " <> unTextViewType (tvType tv)
, "title: " <> unTextViewDescription (tvDescription tv)
, "cbor-hex:"
]
<> rawToMultilineHex (tvRawCBOR tv)

-- ----------------------------------------------------------------------------

-- | Check that the \"type\" of the 'TextView' is as expected.
Expand Down Expand Up @@ -169,67 +135,7 @@ renderTextViewFileError tvfe =
TextViewFileIOError fp ioExcpt ->
"TextView IO exception at: " <> toS fp <> " Error: " <> textShow ioExcpt

-- | Read a file in the external serialised format for 'TextView'.
--
readTextViewFile :: FilePath -> IO (Either TextViewFileError TextView)
readTextViewFile path =
runExceptT $ do
bs <- handleIOExceptT (TextViewFileIOError path) $
BS.readFile path
firstExceptT (TextViewFileError path) $ hoistEither $
parseTextView bs


-- | Read a file in the external serialised format for 'TextView', but use
-- 'expectTextViewOfType' to check that the file is declared to be of the
-- expected type.
--
readTextViewFileOfType :: TextViewType -> FilePath
-> IO (Either TextViewFileError TextView)
readTextViewFileOfType expectedType path =
runExceptT $ do
tv <- ExceptT $ readTextViewFile path
firstExceptT (TextViewFileError path) $ hoistEither $
expectTextViewOfType expectedType tv
return tv


-- | Write a file in the external serialised format for 'TextView'.
--
writeTextViewFile :: FilePath -> TextView -> IO (Either TextViewFileError ())
writeTextViewFile path tv =
runExceptT $
handleIOExceptT (TextViewFileIOError path) $
BS.writeFile path (renderTextView tv)


-- ----------------------------------------------------------------------------

chunksOf :: ByteString -> [ByteString]
chunksOf bs =
case BS.splitAt 80 bs of
(leading, "") -> [leading]
(leading, trailing) -> leading : chunksOf trailing

pTextView :: Parser TextView
pTextView = do
typ <- Atto.string "type: " *> Atto.takeWhile (/= '\n') <* Atto.endOfLine
title <- Atto.string "title: " *> Atto.takeWhile (/= '\n') <* Atto.endOfLine
hex <- Atto.string "cbor-hex:\n" *> Atto.takeByteString <* Atto.endOfInput
case Base16.decode . BS.concat . map (BS.dropWhile isSpace) $ BS.lines hex of
(raw, "") -> pure $ TextView (TextViewType typ) (TextViewDescription title) raw
(_, err) -> panic $ "pTextView: Base16.deocde failed on " <> textShow err

-- | Convert a raw ByteString to hexadecimal and then line wrap
rawToMultilineHex :: ByteString -> [ByteString]
rawToMultilineHex = map (BS.cons ' ') . chunksOf . Base16.encode

textShow :: Show a => a -> Text
textShow = Text.pack . show

-- | Convert from multiline hexadecimal to a raw ByteString.
unRawToMultilineHex :: ByteString -> Either TextViewError ByteString
unRawToMultilineHex bs =
case Base16.decode $ BS.concat (map (BS.dropWhile isSpace) $ BS.lines bs) of
(raw, "") -> Right raw
(_, err) -> Left $ TextViewFormatError ("unRawToMultilineHex: Unable to decode " <> textShow err)
14 changes: 0 additions & 14 deletions cardano-api/test/Test/Cardano/Api/TextView.hs
Expand Up @@ -18,20 +18,6 @@ import qualified Hedgehog.Range as Range
import Test.Cardano.Config.Gen


-- Test this first. If this fails, others are likely to fail.
prop_roundtrip_multiline_hex :: Property
prop_roundtrip_multiline_hex =
Hedgehog.property $ do
bs <- BS.pack <$> Hedgehog.forAll (Gen.string (Range.linear 0 500) (Gen.element ['\0' .. '\xff']))
Hedgehog.tripping bs (BS.unlines . rawToMultilineHex) unRawToMultilineHex

-- Test this second. If this fails, others are likely to fail.
prop_roundtrip_TextView :: Property
prop_roundtrip_TextView =
Hedgehog.property $ do
tv <- Hedgehog.forAll genTextView
Hedgehog.tripping tv renderTextView parseTextView

prop_roundtrip_shelley_SigningKey_view :: Property
prop_roundtrip_shelley_SigningKey_view =
Hedgehog.property $ do
Expand Down

0 comments on commit 5712496

Please sign in to comment.