From e89047f07a96749e1ede8f73f1ad1ac7e4a4f943 Mon Sep 17 00:00:00 2001 From: Taylor Fausak Date: Sat, 29 May 2021 11:49:20 +0000 Subject: [PATCH] Put string instances together --- src/lib/Witch/Instances.hs | 36 ++++++++++++++++--------------- src/test/Main.hs | 44 +++++++++++++++++++------------------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/src/lib/Witch/Instances.hs b/src/lib/Witch/Instances.hs index c88470c..b042126 100644 --- a/src/lib/Witch/Instances.hs +++ b/src/lib/Witch/Instances.hs @@ -1049,15 +1049,6 @@ instance From.From ShortByteString.ShortByteString ByteString.ByteString where -- Text --- | Uses 'Text.pack'. Some 'Char' values cannot be represented in 'Text.Text' --- and will be replaced with @'\\xFFFD'@. -instance From.From String Text.Text where - from = Text.pack - --- | Uses 'Text.unpack'. -instance From.From Text.Text String where - from = Text.unpack - -- | Uses 'LazyText.fromStrict'. instance From.From Text.Text LazyText.Text where from = LazyText.fromStrict @@ -1068,6 +1059,25 @@ instance From.From Text.Text ByteString.ByteString where -- LazyText +-- | Uses 'LazyText.toStrict'. +instance From.From LazyText.Text Text.Text where + from = LazyText.toStrict + +-- | Uses 'LazyText.encodeUtf8'. +instance From.From LazyText.Text LazyByteString.ByteString where + from = LazyText.encodeUtf8 + +-- String + +-- | Uses 'Text.pack'. Some 'Char' values cannot be represented in 'Text.Text' +-- and will be replaced with @'\\xFFFD'@. +instance From.From String Text.Text where + from = Text.pack + +-- | Uses 'Text.unpack'. +instance From.From Text.Text String where + from = Text.unpack + -- | Uses 'LazyText.pack'. Some 'Char' values cannot be represented in -- 'LazyText.Text' and will be replaced with @'\\xFFFD'@. instance From.From String LazyText.Text where @@ -1077,14 +1087,6 @@ instance From.From String LazyText.Text where instance From.From LazyText.Text String where from = LazyText.unpack --- | Uses 'LazyText.toStrict'. -instance From.From LazyText.Text Text.Text where - from = LazyText.toStrict - --- | Uses 'LazyText.encodeUtf8'. -instance From.From LazyText.Text LazyByteString.ByteString where - from = LazyText.encodeUtf8 - -- TryFromException -- | Uses @coerce@. diff --git a/src/test/Main.hs b/src/test/Main.hs index 4bef51a..fe11f9a 100644 --- a/src/test/Main.hs +++ b/src/test/Main.hs @@ -1568,6 +1568,28 @@ main = runTestTTAndExit $ "Witch" ~: , f (ShortByteString.pack [0x00]) ~?= ByteString.pack [0x00] , f (ShortByteString.pack [0x0f, 0xf0]) ~?= ByteString.pack [0x0f, 0xf0] ] + , "From Text LazyText" ~: + let f = Witch.from @Text.Text @LazyText.Text in + [ f (Text.pack "") ~?= LazyText.pack "" + , f (Text.pack "a") ~?= LazyText.pack "a" + , f (Text.pack "ab") ~?= LazyText.pack "ab" + ] + , "From Text ByteString" ~: + let f = Witch.from @Text.Text @ByteString.ByteString in + [ f (Text.pack "") ~?= ByteString.pack [] + , f (Text.pack "a") ~?= ByteString.pack [0x61] + ] + , "From LazyText Text" ~: + let f = Witch.from @LazyText.Text @Text.Text in + [ f (LazyText.pack "") ~?= Text.pack "" + , f (LazyText.pack "a") ~?= Text.pack "a" + , f (LazyText.pack "ab") ~?= Text.pack "ab" + ] + , "From LazyText LazyByteString" ~: + let f = Witch.from @LazyText.Text @LazyByteString.ByteString in + [ f (LazyText.pack "") ~?= LazyByteString.pack [] + , f (LazyText.pack "a") ~?= LazyByteString.pack [0x61] + ] , "From String Text" ~: let f = Witch.from @String @Text.Text in [ f "" ~?= Text.pack "" @@ -1580,17 +1602,6 @@ main = runTestTTAndExit $ "Witch" ~: , f (Text.pack "a") ~?= "a" , f (Text.pack "ab") ~?= "ab" ] - , "From Text LazyText" ~: - let f = Witch.from @Text.Text @LazyText.Text in - [ f (Text.pack "") ~?= LazyText.pack "" - , f (Text.pack "a") ~?= LazyText.pack "a" - , f (Text.pack "ab") ~?= LazyText.pack "ab" - ] - , "From Text ByteString" ~: - let f = Witch.from @Text.Text @ByteString.ByteString in - [ f (Text.pack "") ~?= ByteString.pack [] - , f (Text.pack "a") ~?= ByteString.pack [0x61] - ] , "From String LazyText" ~: let f = Witch.from @String @LazyText.Text in [ f "" ~?= LazyText.pack "" @@ -1603,17 +1614,6 @@ main = runTestTTAndExit $ "Witch" ~: , f (LazyText.pack "a") ~?= "a" , f (LazyText.pack "ab") ~?= "ab" ] - , "From LazyText Text" ~: - let f = Witch.from @LazyText.Text @Text.Text in - [ f (LazyText.pack "") ~?= Text.pack "" - , f (LazyText.pack "a") ~?= Text.pack "a" - , f (LazyText.pack "ab") ~?= Text.pack "ab" - ] - , "From LazyText LazyByteString" ~: - let f = Witch.from @LazyText.Text @LazyByteString.ByteString in - [ f (LazyText.pack "") ~?= LazyByteString.pack [] - , f (LazyText.pack "a") ~?= LazyByteString.pack [0x61] - ] , "From Integer Day" ~: let f = Witch.from @Integer @Time.Day in [ f 0 ~?= Time.ModifiedJulianDay 0