diff --git a/source/library/Witch/Encoding.hs b/source/library/Witch/Encoding.hs index 141b6e4..dd461c3 100644 --- a/source/library/Witch/Encoding.hs +++ b/source/library/Witch/Encoding.hs @@ -15,3 +15,6 @@ type UTF_16LE = Tagged.Tagged "UTF-16LE" -- | type UTF_16BE = Tagged.Tagged "UTF-16BE" + +-- | +type UTF_32LE = Tagged.Tagged "UTF-32LE" diff --git a/source/library/Witch/Instances.hs b/source/library/Witch/Instances.hs index 90de460..e3cec0c 100644 --- a/source/library/Witch/Instances.hs +++ b/source/library/Witch/Instances.hs @@ -1436,6 +1436,56 @@ instance From.From String (Encoding.UTF_16BE ByteString.ByteString) where instance From.From String (Encoding.UTF_16BE LazyByteString.ByteString) where from = Utility.via @LazyText.Text +-- UTF-32LE + +-- | Uses 'Text.decodeUtf32LE'. +instance TryFrom.TryFrom (Encoding.UTF_32LE ByteString.ByteString) Text.Text where + tryFrom = Utility.eitherTryFrom $ tryEvaluate @Text.UnicodeException . Text.decodeUtf32LE . From.from + +-- | Converts via 'Text.Text'. +instance TryFrom.TryFrom (Encoding.UTF_32LE ByteString.ByteString) LazyText.Text where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @LazyText.Text) . Utility.tryInto @Text.Text + +-- | Converts via 'Text.Text'. +instance TryFrom.TryFrom (Encoding.UTF_32LE ByteString.ByteString) String where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @Text.Text + +-- | Uses 'LazyText.decodeUtf32LE'. +instance TryFrom.TryFrom (Encoding.UTF_32LE LazyByteString.ByteString) LazyText.Text where + tryFrom = Utility.eitherTryFrom $ tryEvaluate @Text.UnicodeException . LazyText.decodeUtf32LE . From.from + +-- | Converts via 'LazyText.Text'. +instance TryFrom.TryFrom (Encoding.UTF_32LE LazyByteString.ByteString) Text.Text where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @Text.Text) . Utility.tryInto @LazyText.Text + +-- | Converts via 'LazyText.Text'. +instance TryFrom.TryFrom (Encoding.UTF_32LE LazyByteString.ByteString) String where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @LazyText.Text + +-- | Uses 'Text.encodeUtf32LE'. +instance From.From Text.Text (Encoding.UTF_32LE ByteString.ByteString) where + from = From.from . Text.encodeUtf32LE + +-- | Converts via 'ByteString.ByteString'. +instance From.From Text.Text (Encoding.UTF_32LE LazyByteString.ByteString) where + from = fmap From.from . Utility.into @(Encoding.UTF_32LE ByteString.ByteString) + +-- | Uses 'LazyText.encodeUtf32LE'. +instance From.From LazyText.Text (Encoding.UTF_32LE LazyByteString.ByteString) where + from = From.from . LazyText.encodeUtf32LE + +-- | Converts via 'LazyByteString.ByteString'. +instance From.From LazyText.Text (Encoding.UTF_32LE ByteString.ByteString) where + from = fmap From.from . Utility.into @(Encoding.UTF_32LE LazyByteString.ByteString) + +-- | Converts via 'Text.Text'. +instance From.From String (Encoding.UTF_32LE ByteString.ByteString) where + from = Utility.via @Text.Text + +-- | Converts via 'LazyText.Text'. +instance From.From String (Encoding.UTF_32LE LazyByteString.ByteString) where + from = Utility.via @LazyText.Text + -- realFloatToRational :: diff --git a/source/test-suite/Main.hs b/source/test-suite/Main.hs index e1269a9..4d68bf0 100644 --- a/source/test-suite/Main.hs +++ b/source/test-suite/Main.hs @@ -2300,6 +2300,75 @@ spec = describe "Witch" $ do it "works" $ do f "a" `shouldBe` Tagged.Tagged (LazyByteString.pack [0x00, 0x61]) + describe "TryFrom (UTF_32LE ByteString) Text" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32LE ByteString.ByteString) @Text.Text + it "works" $ do + f (Tagged.Tagged (ByteString.pack [])) `shouldBe` Just (Text.pack "") + f (Tagged.Tagged (ByteString.pack [0x24, 0x00, 0x00, 0x00])) `shouldBe` Just (Text.pack "\x24") + f (Tagged.Tagged (ByteString.pack [0xa3, 0x00, 0x00, 0x00])) `shouldBe` Just (Text.pack "\xa3") + f (Tagged.Tagged (ByteString.pack [0xac, 0x20, 0x00, 0x00])) `shouldBe` Just (Text.pack "\x20ac") + f (Tagged.Tagged (ByteString.pack [0x48, 0x03, 0x01, 0x00])) `shouldBe` Just (Text.pack "\x10348") + f (Tagged.Tagged (ByteString.pack [0x00])) `shouldBe` Nothing + + describe "TryFrom (UTF_32LE ByteString) LazyText" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32LE ByteString.ByteString) @LazyText.Text + it "works" $ do + f (Tagged.Tagged (ByteString.pack [0x61, 0x00, 0x00, 0x00])) `shouldBe` Just (LazyText.pack "a") + + describe "TryFrom (UTF_32LE ByteString) String" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32LE ByteString.ByteString) @String + it "works" $ do + f (Tagged.Tagged (ByteString.pack [0x61, 0x00, 0x00, 0x00])) `shouldBe` Just "a" + + describe "TryFrom (UTF_32LE LazyByteString) LazyText" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32LE LazyByteString.ByteString) @LazyText.Text + it "works" $ do + f (Tagged.Tagged (LazyByteString.pack [0x61, 0x00, 0x00, 0x00])) `shouldBe` Just (LazyText.pack "a") + + describe "TryFrom (UTF_32LE LazyByteString) Text" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32LE LazyByteString.ByteString) @Text.Text + it "works" $ do + f (Tagged.Tagged (LazyByteString.pack [0x61, 0x00, 0x00, 0x00])) `shouldBe` Just (Text.pack "a") + + describe "TryFrom (UTF_32LE LazyByteString) String" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32LE LazyByteString.ByteString) @String + it "works" $ do + f (Tagged.Tagged (LazyByteString.pack [0x61, 0x00, 0x00, 0x00])) `shouldBe` Just "a" + + describe "From Text (UTF_32LE ByteString)" $ do + let f = Witch.from @Text.Text @(Encoding.UTF_32LE ByteString.ByteString) + it "works" $ do + f (Text.pack "") `shouldBe` Tagged.Tagged (ByteString.pack []) + f (Text.pack "\x24") `shouldBe` Tagged.Tagged (ByteString.pack [0x24, 0x00, 0x00, 0x00]) + f (Text.pack "\xa3") `shouldBe` Tagged.Tagged (ByteString.pack [0xa3, 0x00, 0x00, 0x00]) + f (Text.pack "\x20ac") `shouldBe` Tagged.Tagged (ByteString.pack [0xac, 0x20, 0x00, 0x00]) + f (Text.pack "\x10348") `shouldBe` Tagged.Tagged (ByteString.pack [0x48, 0x03, 0x01, 0x00]) + + describe "From Text (UTF_32LE LazyByteString)" $ do + let f = Witch.from @Text.Text @(Encoding.UTF_32LE LazyByteString.ByteString) + it "works" $ do + f (Text.pack "a") `shouldBe` Tagged.Tagged (LazyByteString.pack [0x61, 0x00, 0x00, 0x00]) + + describe "From LazyText (UTF_32LE LazyByteString)" $ do + let f = Witch.from @LazyText.Text @(Encoding.UTF_32LE LazyByteString.ByteString) + it "works" $ do + f (LazyText.pack "a") `shouldBe` Tagged.Tagged (LazyByteString.pack [0x61, 0x00, 0x00, 0x00]) + + describe "From LazyText (UTF_32LE ByteString)" $ do + let f = Witch.from @LazyText.Text @(Encoding.UTF_32LE ByteString.ByteString) + it "works" $ do + f (LazyText.pack "a") `shouldBe` Tagged.Tagged (ByteString.pack [0x61, 0x00, 0x00, 0x00]) + + describe "From String (UTF_32LE ByteString)" $ do + let f = Witch.from @String @(Encoding.UTF_32LE ByteString.ByteString) + it "works" $ do + f "a" `shouldBe` Tagged.Tagged (ByteString.pack [0x61, 0x00, 0x00, 0x00]) + + describe "From String (UTF_32LE LazyByteString)" $ do + let f = Witch.from @String @(Encoding.UTF_32LE LazyByteString.ByteString) + it "works" $ do + f "a" `shouldBe` Tagged.Tagged (LazyByteString.pack [0x61, 0x00, 0x00, 0x00]) + newtype Age = Age Int.Int8 deriving (Eq, Show)