diff --git a/source/library/Witch/Encoding.hs b/source/library/Witch/Encoding.hs index dd461c3..f53995f 100644 --- a/source/library/Witch/Encoding.hs +++ b/source/library/Witch/Encoding.hs @@ -18,3 +18,6 @@ type UTF_16BE = Tagged.Tagged "UTF-16BE" -- | type UTF_32LE = Tagged.Tagged "UTF-32LE" + +-- | +type UTF_32BE = Tagged.Tagged "UTF-32BE" diff --git a/source/library/Witch/Instances.hs b/source/library/Witch/Instances.hs index e3cec0c..01d859e 100644 --- a/source/library/Witch/Instances.hs +++ b/source/library/Witch/Instances.hs @@ -1486,6 +1486,56 @@ instance From.From String (Encoding.UTF_32LE ByteString.ByteString) where instance From.From String (Encoding.UTF_32LE LazyByteString.ByteString) where from = Utility.via @LazyText.Text +-- UTF-32BE + +-- | Uses 'Text.decodeUtf32BE'. +instance TryFrom.TryFrom (Encoding.UTF_32BE ByteString.ByteString) Text.Text where + tryFrom = Utility.eitherTryFrom $ tryEvaluate @Text.UnicodeException . Text.decodeUtf32BE . From.from + +-- | Converts via 'Text.Text'. +instance TryFrom.TryFrom (Encoding.UTF_32BE 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_32BE ByteString.ByteString) String where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @Text.Text + +-- | Uses 'LazyText.decodeUtf32BE'. +instance TryFrom.TryFrom (Encoding.UTF_32BE LazyByteString.ByteString) LazyText.Text where + tryFrom = Utility.eitherTryFrom $ tryEvaluate @Text.UnicodeException . LazyText.decodeUtf32BE . From.from + +-- | Converts via 'LazyText.Text'. +instance TryFrom.TryFrom (Encoding.UTF_32BE 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_32BE LazyByteString.ByteString) String where + tryFrom = Utility.eitherTryFrom $ fmap (Utility.into @String) . Utility.tryInto @LazyText.Text + +-- | Uses 'Text.encodeUtf32BE'. +instance From.From Text.Text (Encoding.UTF_32BE ByteString.ByteString) where + from = From.from . Text.encodeUtf32BE + +-- | Converts via 'ByteString.ByteString'. +instance From.From Text.Text (Encoding.UTF_32BE LazyByteString.ByteString) where + from = fmap From.from . Utility.into @(Encoding.UTF_32BE ByteString.ByteString) + +-- | Uses 'LazyText.encodeUtf32BE'. +instance From.From LazyText.Text (Encoding.UTF_32BE LazyByteString.ByteString) where + from = From.from . LazyText.encodeUtf32BE + +-- | Converts via 'LazyByteString.ByteString'. +instance From.From LazyText.Text (Encoding.UTF_32BE ByteString.ByteString) where + from = fmap From.from . Utility.into @(Encoding.UTF_32BE LazyByteString.ByteString) + +-- | Converts via 'Text.Text'. +instance From.From String (Encoding.UTF_32BE ByteString.ByteString) where + from = Utility.via @Text.Text + +-- | Converts via 'LazyText.Text'. +instance From.From String (Encoding.UTF_32BE LazyByteString.ByteString) where + from = Utility.via @LazyText.Text + -- realFloatToRational :: diff --git a/source/test-suite/Main.hs b/source/test-suite/Main.hs index 4d68bf0..a15ee28 100644 --- a/source/test-suite/Main.hs +++ b/source/test-suite/Main.hs @@ -2369,6 +2369,75 @@ spec = describe "Witch" $ do it "works" $ do f "a" `shouldBe` Tagged.Tagged (LazyByteString.pack [0x61, 0x00, 0x00, 0x00]) + describe "TryFrom (UTF_32BE ByteString) Text" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32BE ByteString.ByteString) @Text.Text + it "works" $ do + f (Tagged.Tagged (ByteString.pack [])) `shouldBe` Just (Text.pack "") + f (Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x00, 0x24])) `shouldBe` Just (Text.pack "\x24") + f (Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x00, 0xa3])) `shouldBe` Just (Text.pack "\xa3") + f (Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x20, 0xac])) `shouldBe` Just (Text.pack "\x20ac") + f (Tagged.Tagged (ByteString.pack [0x00, 0x01, 0x03, 0x48])) `shouldBe` Just (Text.pack "\x10348") + f (Tagged.Tagged (ByteString.pack [0x00])) `shouldBe` Nothing + + describe "TryFrom (UTF_32BE ByteString) LazyText" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32BE ByteString.ByteString) @LazyText.Text + it "works" $ do + f (Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x00, 0x61])) `shouldBe` Just (LazyText.pack "a") + + describe "TryFrom (UTF_32BE ByteString) String" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32BE ByteString.ByteString) @String + it "works" $ do + f (Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x00, 0x61])) `shouldBe` Just "a" + + describe "TryFrom (UTF_32BE LazyByteString) LazyText" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32BE LazyByteString.ByteString) @LazyText.Text + it "works" $ do + f (Tagged.Tagged (LazyByteString.pack [0x00, 0x00, 0x00, 0x61])) `shouldBe` Just (LazyText.pack "a") + + describe "TryFrom (UTF_32BE LazyByteString) Text" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32BE LazyByteString.ByteString) @Text.Text + it "works" $ do + f (Tagged.Tagged (LazyByteString.pack [0x00, 0x00, 0x00, 0x61])) `shouldBe` Just (Text.pack "a") + + describe "TryFrom (UTF_32BE LazyByteString) String" $ do + let f = hush . Witch.tryFrom @(Encoding.UTF_32BE LazyByteString.ByteString) @String + it "works" $ do + f (Tagged.Tagged (LazyByteString.pack [0x00, 0x00, 0x00, 0x61])) `shouldBe` Just "a" + + describe "From Text (UTF_32BE ByteString)" $ do + let f = Witch.from @Text.Text @(Encoding.UTF_32BE ByteString.ByteString) + it "works" $ do + f (Text.pack "") `shouldBe` Tagged.Tagged (ByteString.pack []) + f (Text.pack "\x24") `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x00, 0x24]) + f (Text.pack "\xa3") `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x00, 0xa3]) + f (Text.pack "\x20ac") `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x20, 0xac]) + f (Text.pack "\x10348") `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0x01, 0x03, 0x48]) + + describe "From Text (UTF_32BE LazyByteString)" $ do + let f = Witch.from @Text.Text @(Encoding.UTF_32BE LazyByteString.ByteString) + it "works" $ do + f (Text.pack "a") `shouldBe` Tagged.Tagged (LazyByteString.pack [0x00, 0x00, 0x00, 0x61]) + + describe "From LazyText (UTF_32BE LazyByteString)" $ do + let f = Witch.from @LazyText.Text @(Encoding.UTF_32BE LazyByteString.ByteString) + it "works" $ do + f (LazyText.pack "a") `shouldBe` Tagged.Tagged (LazyByteString.pack [0x00, 0x00, 0x00, 0x61]) + + describe "From LazyText (UTF_32BE ByteString)" $ do + let f = Witch.from @LazyText.Text @(Encoding.UTF_32BE ByteString.ByteString) + it "works" $ do + f (LazyText.pack "a") `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x00, 0x61]) + + describe "From String (UTF_32BE ByteString)" $ do + let f = Witch.from @String @(Encoding.UTF_32BE ByteString.ByteString) + it "works" $ do + f "a" `shouldBe` Tagged.Tagged (ByteString.pack [0x00, 0x00, 0x00, 0x61]) + + describe "From String (UTF_32BE LazyByteString)" $ do + let f = Witch.from @String @(Encoding.UTF_32BE LazyByteString.ByteString) + it "works" $ do + f "a" `shouldBe` Tagged.Tagged (LazyByteString.pack [0x00, 0x00, 0x00, 0x61]) + newtype Age = Age Int.Int8 deriving (Eq, Show)