Skip to content

Commit

Permalink
[mmzk] (feat) Throw on parse failure functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MMZK1526 committed Jul 18, 2023
1 parent e322b09 commit 1e33a2a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

## 0.3.1.0 -- Unreleased

* Add `parseStringM`, `parseTextM`, and `parseByteStringM` to `IDConv`.
* Instead of returning an `Either`, they throw an exception when the input is
invalid.

* Add unsafe methods to `IDConv`.


Expand Down
6 changes: 6 additions & 0 deletions src/Data/KindID.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ module Data.KindID
, parseString
, parseText
, parseByteString
, parseStringM
, parseTextM
, parseByteStringM
-- * Encoding & decoding (class methods)
, id2String
, id2Text
, id2ByteString
, string2ID
, text2ID
, byteString2ID
, string2IDM
, text2IDM
, byteString2IDM
-- * Type-level & term-level conversion
, toTypeID
, fromTypeID
Expand Down
26 changes: 26 additions & 0 deletions src/Data/KindID/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,32 @@ parseByteString str = do
Just kid -> pure kid
{-# INLINE parseByteString #-}

-- | Parse a 'KindID' from its 'String' representation, throwing an error when
-- the parsing fails. It is 'string2IDM' with concrete type.
parseStringM :: (ToPrefix prefix, ValidPrefix (PrefixSymbol prefix), MonadIO m)
=> String -> m (KindID prefix)
parseStringM = string2IDM
{-# INLINE parseStringM #-}

-- | Parse a 'KindID' from its string representation as a strict 'Text',
-- throwing an error when the parsing fails. It is 'text2IDM' with concrete
-- type.
parseTextM :: (ToPrefix prefix, ValidPrefix (PrefixSymbol prefix), MonadIO m)
=> Text -> m (KindID prefix)
parseTextM = text2IDM
{-# INLINE parseTextM #-}

-- | Parse a 'KindID' from its string representation as a lazy 'ByteString',
-- throwing an error when the parsing fails. It is 'byteString2IDM' with
-- concrete type.
parseByteStringM :: ( ToPrefix prefix
, ValidPrefix (PrefixSymbol prefix)
, MonadIO m )
=> ByteString
-> m (KindID prefix)
parseByteStringM = byteString2IDM
{-# INLINE parseByteStringM #-}

-- | Check if the prefix is valid and the suffix 'UUID' has the correct v7
-- version and variant.
checkKindID :: (ToPrefix prefix, ValidPrefix (PrefixSymbol prefix))
Expand Down
6 changes: 6 additions & 0 deletions src/Data/TypeID.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,19 @@ module Data.TypeID
, parseString
, parseText
, parseByteString
, parseStringM
, parseTextM
, parseByteStringM
-- * Encoding & decoding (class methods)
, id2String
, id2Text
, id2ByteString
, string2ID
, text2ID
, byteString2ID
, string2IDM
, text2IDM
, byteString2IDM
) where

import Data.TypeID.Class
Expand Down
19 changes: 19 additions & 0 deletions src/Data/TypeID/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Data.TypeID.Class
, ResWithErr(..)
) where

import Control.Exception
import Control.Monad.IO.Class
import Data.ByteString.Lazy (ByteString)
import Data.Kind (Type)
Expand Down Expand Up @@ -77,6 +78,24 @@ class IDConv a where
-- | Pretty-print the identifier to a lazy 'ByteString'.
id2ByteString :: a -> ByteString

-- | Parse the identifier from its 'String' representation, throwing an error
-- when the parsing fails.
string2IDM :: MonadIO m => String -> m a
string2IDM = either (liftIO . throwIO) pure . string2ID
{-# INLINE string2IDM #-}

-- | Parse the identifier from its string representation as a strict 'Text',
-- throwing an error when the parsing fails.
text2IDM :: MonadIO m => Text -> m a
text2IDM = either (liftIO . throwIO) pure . text2ID
{-# INLINE text2IDM #-}

-- | Parse the identifier from its string representation as a lazy
-- 'ByteString', throwing an error when the parsing fails.
byteString2IDM :: MonadIO m => ByteString -> m a
byteString2IDM = either (liftIO . throwIO) pure . byteString2ID
{-# INLINE byteString2IDM #-}

-- | Parse the identifier from its 'String' representation, but crashes when
-- the parsing fails.
unsafeString2ID :: String -> a
Expand Down
20 changes: 20 additions & 0 deletions src/Data/TypeID/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,26 @@ parseByteString bs = case second BSL.uncons $ BSL.span (/= 95) bs of
Nothing -> TypeID prefix' <$> decodeUUID suffix
Just err -> Left err

-- | Parse a 'TypeID' from its 'String' representation, throwing an error when
-- the parsing fails. It is 'string2IDM' with concrete type.
parseStringM :: MonadIO m => String -> m TypeID
parseStringM = string2IDM
{-# INLINE parseStringM #-}

-- | Parse a 'TypeID' from its string representation as a strict 'Text',
-- throwing an error when the parsing fails. It is 'text2IDM' with concrete
-- type.
parseTextM :: MonadIO m => Text -> m TypeID
parseTextM = text2IDM
{-# INLINE parseTextM #-}

-- | Parse a 'TypeID' from its string representation as a lazy 'ByteString',
-- throwing an error when the parsing fails. It is 'byteString2IDM' with
-- concrete type.
parseByteStringM :: MonadIO m => ByteString -> m TypeID
parseByteStringM = byteString2IDM
{-# INLINE parseByteStringM #-}

-- | Check if the given prefix is a valid TypeID prefix.
checkPrefix :: Text -> Maybe TypeIDError
checkPrefix prefix
Expand Down

0 comments on commit 1e33a2a

Please sign in to comment.