diff --git a/src/lib/Witch/Instances.hs b/src/lib/Witch/Instances.hs index b042126..7a33c17 100644 --- a/src/lib/Witch/Instances.hs +++ b/src/lib/Witch/Instances.hs @@ -1015,6 +1015,14 @@ instance From.From ByteString.ByteString ShortByteString.ShortByteString where instance TryFrom.TryFrom ByteString.ByteString Text.Text where tryFrom = Utility.eitherTryFrom Text.decodeUtf8' +-- | Converts via 'Text.Text'. +instance TryFrom.TryFrom ByteString.ByteString LazyText.Text where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @LazyText.Text) . Utility.tryInto @Text.Text + +-- | Converts via 'Text.Text'. +instance TryFrom.TryFrom ByteString.ByteString String where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @Text.Text + -- LazyByteString -- | Uses 'LazyByteString.pack'. @@ -1033,6 +1041,14 @@ instance From.From LazyByteString.ByteString ByteString.ByteString where instance TryFrom.TryFrom LazyByteString.ByteString LazyText.Text where tryFrom = Utility.eitherTryFrom LazyText.decodeUtf8' +-- | Converts via 'LazyText.Text'. +instance TryFrom.TryFrom LazyByteString.ByteString Text.Text where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @Text.Text) . Utility.tryInto @LazyText.Text + +-- | Converts via 'LazyText.Text'. +instance TryFrom.TryFrom LazyByteString.ByteString String where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @LazyText.Text + -- ShortByteString -- | Uses 'ShortByteString.pack'. @@ -1057,6 +1073,10 @@ instance From.From Text.Text LazyText.Text where instance From.From Text.Text ByteString.ByteString where from = Text.encodeUtf8 +-- | Converts via 'ByteString.ByteString'. +instance From.From Text.Text LazyByteString.ByteString where + from = Utility.via @ByteString.ByteString + -- LazyText -- | Uses 'LazyText.toStrict'. @@ -1067,6 +1087,10 @@ instance From.From LazyText.Text Text.Text where instance From.From LazyText.Text LazyByteString.ByteString where from = LazyText.encodeUtf8 +-- | Converts via 'LazyByteString.ByteString'. +instance From.From LazyText.Text ByteString.ByteString where + from = Utility.via @LazyByteString.ByteString + -- String -- | Uses 'Text.pack'. Some 'Char' values cannot be represented in 'Text.Text' @@ -1087,6 +1111,14 @@ instance From.From String LazyText.Text where instance From.From LazyText.Text String where from = LazyText.unpack +-- | Converts via 'Text.Text'. +instance From.From String ByteString.ByteString where + from = Utility.via @Text.Text + +-- | Converts via 'LazyText.Text'. +instance From.From String LazyByteString.ByteString where + from = Utility.via @LazyText.Text + -- TryFromException -- | Uses @coerce@. diff --git a/src/test/Main.hs b/src/test/Main.hs index 03f28af..1b2ac2c 100644 --- a/src/test/Main.hs +++ b/src/test/Main.hs @@ -1601,6 +1601,18 @@ main = runTestTTAndExit $ "Witch" ~: , f (ByteString.pack [0x61]) ~?= Just (Text.pack "a") , f (ByteString.pack [0xff]) ~?= Nothing ] + , "TryFrom ByteString LazyText" ~: + let f = hush . Witch.tryFrom @ByteString.ByteString @LazyText.Text in + [ f (ByteString.pack []) ~?= Just (LazyText.pack "") + , f (ByteString.pack [0x61]) ~?= Just (LazyText.pack "a") + , f (ByteString.pack [0xff]) ~?= Nothing + ] + , "TryFrom ByteString String" ~: + let f = hush . Witch.tryFrom @ByteString.ByteString @String in + [ f (ByteString.pack []) ~?= Just "" + , f (ByteString.pack [0x61]) ~?= Just "a" + , f (ByteString.pack [0xff]) ~?= Nothing + ] -- LazyByteString @@ -1628,6 +1640,18 @@ main = runTestTTAndExit $ "Witch" ~: , f (LazyByteString.pack [0x61]) ~?= Just (LazyText.pack "a") , f (LazyByteString.pack [0xff]) ~?= Nothing ] + , "TryFrom LazyByteString Text" ~: + let f = hush . Witch.tryFrom @LazyByteString.ByteString @Text.Text in + [ f (LazyByteString.pack []) ~?= Just (Text.pack "") + , f (LazyByteString.pack [0x61]) ~?= Just (Text.pack "a") + , f (LazyByteString.pack [0xff]) ~?= Nothing + ] + , "TryFrom LazyByteString String" ~: + let f = hush . Witch.tryFrom @LazyByteString.ByteString @String in + [ f (LazyByteString.pack []) ~?= Just "" + , f (LazyByteString.pack [0x61]) ~?= Just "a" + , f (LazyByteString.pack [0xff]) ~?= Nothing + ] -- ShortByteString @@ -1663,6 +1687,11 @@ main = runTestTTAndExit $ "Witch" ~: [ f (Text.pack "") ~?= ByteString.pack [] , f (Text.pack "a") ~?= ByteString.pack [0x61] ] + , "From Text LazyByteString" ~: + let f = Witch.from @Text.Text @LazyByteString.ByteString in + [ f (Text.pack "") ~?= LazyByteString.pack [] + , f (Text.pack "a") ~?= LazyByteString.pack [0x61] + ] -- LazyText @@ -1677,6 +1706,11 @@ main = runTestTTAndExit $ "Witch" ~: [ f (LazyText.pack "") ~?= LazyByteString.pack [] , f (LazyText.pack "a") ~?= LazyByteString.pack [0x61] ] + , "From LazyText ByteString" ~: + let f = Witch.from @LazyText.Text @ByteString.ByteString in + [ f (LazyText.pack "") ~?= ByteString.pack [] + , f (LazyText.pack "a") ~?= ByteString.pack [0x61] + ] -- String @@ -1704,6 +1738,16 @@ main = runTestTTAndExit $ "Witch" ~: , f (LazyText.pack "a") ~?= "a" , f (LazyText.pack "ab") ~?= "ab" ] + , "From String ByteString" ~: + let f = Witch.from @String @ByteString.ByteString in + [ f "" ~?= ByteString.pack [] + , f "a" ~?= ByteString.pack [0x61] + ] + , "From String LazyByteString" ~: + let f = Witch.from @String @LazyByteString.ByteString in + [ f "" ~?= LazyByteString.pack [] + , f "a" ~?= LazyByteString.pack [0x61] + ] -- Day