Skip to content

Instantly share code, notes, and snippets.

@exarkun
Created February 3, 2023 14:43
Show Gist options
  • Save exarkun/11fdedc5a3854a5f598c07696a0b906e to your computer and use it in GitHub Desktop.
Save exarkun/11fdedc5a3854a5f598c07696a0b906e to your computer and use it in GitHub Desktop.
extensionFromFields :: [(B.ByteString, B.ByteString)] -> Either String URIExtension
extensionFromFields fields = do
let
fieldMap = M.fromList fields
lookup' :: B.ByteString -> Either String B.ByteString
lookup' k = maybe (Left $ "URI extension field missing: " <> k) pure (M.lookup k fieldMap)
uriExtCodecName <- lookup' "codec_name"
uriExtCodecParams <- lookup' "codec_params" >>= parse "codec_params"
uriExtTailCodecParams <- lookup' "tail_codec_params" >>= parse "tail_codec_params"
uriExtSize <- read <$> lookup' "size"
uriExtSegmentSize <- read <$> lookup' "segment_size"
uriExtNeededShares <- read <$> lookup' "needed_shares"
uriExtTotalShares <- read <$> lookup' "total_shares"
uriExtCrypttextHash <- lookup' "crypttext_hash" uriExtTotalShares :: Total uriExtTotalShares
uriExtCryptotextRootHash <- lookup' "crypttext_root_hash" read :: forall a. Read a => String -> a read
uriExtShareRootHash <- lookup' "share_root_hash" lookup' :: ByteString -> Either String ByteString lookup'
pure URIExtension { .. }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment