From 1b9a59e9d713bea4424c17fd2f090ea435a15a84 Mon Sep 17 00:00:00 2001 From: Taylor Fausak Date: Sun, 18 Apr 2021 11:28:02 -0400 Subject: [PATCH] Fix linting and formatting --- src/lib/Witch/Instances.hs | 79 ++++++++++++++++--------------- src/lib/Witch/TryCastException.hs | 12 +++-- src/test/Main.hs | 8 ++-- 3 files changed, 51 insertions(+), 48 deletions(-) diff --git a/src/lib/Witch/Instances.hs b/src/lib/Witch/Instances.hs index 81df6d9..35b7f2a 100644 --- a/src/lib/Witch/Instances.hs +++ b/src/lib/Witch/Instances.hs @@ -148,8 +148,9 @@ instance TryCast.TryCast Int.Int32 Natural.Natural where tryCast = maybeTryCast fromNonNegativeIntegral instance TryCast.TryCast Int.Int32 Float where - tryCast = maybeTryCast $ \s -> - if -maxFloat <= s && s <= maxFloat then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat + then Just $ fromIntegral s + else Nothing instance Cast.Cast Int.Int32 Double where cast = fromIntegral @@ -190,14 +191,14 @@ instance TryCast.TryCast Int.Int64 Natural.Natural where tryCast = maybeTryCast fromNonNegativeIntegral instance TryCast.TryCast Int.Int64 Float where - tryCast = maybeTryCast $ \s -> - if -maxFloat <= s && s <= maxFloat then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat + then Just $ fromIntegral s + else Nothing instance TryCast.TryCast Int.Int64 Double where - tryCast = maybeTryCast $ \s -> - if -maxDouble <= s && s <= maxDouble - then Just $ fromIntegral s - else Nothing + tryCast = maybeTryCast $ \s -> if -maxDouble <= s && s <= maxDouble + then Just $ fromIntegral s + else Nothing -- Int @@ -235,16 +236,16 @@ instance TryCast.TryCast Int Natural.Natural where tryCast = maybeTryCast fromNonNegativeIntegral instance TryCast.TryCast Int Float where - tryCast = maybeTryCast $ \s -> - if -maxFloat <= s && s <= maxFloat then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat + then Just $ fromIntegral s + else Nothing instance TryCast.TryCast Int Double where tryCast = maybeTryCast $ \s -> - if toInteger (maxBound :: Int) <= maxDouble + if (toInteger (maxBound :: Int) <= maxDouble) + || (-maxDouble <= s && s <= maxDouble) then Just $ fromIntegral s - else if -maxDouble <= s && s <= maxDouble - then Just $ fromIntegral s - else Nothing + else Nothing -- Integer @@ -287,14 +288,14 @@ instance TryCast.TryCast Integer Natural.Natural where maybeTryCast $ \s -> if s < 0 then Nothing else Just $ fromInteger s instance TryCast.TryCast Integer Float where - tryCast = maybeTryCast $ \s -> - if -maxFloat <= s && s <= maxFloat then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast $ \s -> if -maxFloat <= s && s <= maxFloat + then Just $ fromIntegral s + else Nothing instance TryCast.TryCast Integer Double where - tryCast = maybeTryCast $ \s -> - if -maxDouble <= s && s <= maxDouble - then Just $ fromIntegral s - else Nothing + tryCast = maybeTryCast $ \s -> if -maxDouble <= s && s <= maxDouble + then Just $ fromIntegral s + else Nothing -- Word8 @@ -414,8 +415,8 @@ instance Cast.Cast Word.Word32 Integer where cast = fromIntegral instance TryCast.TryCast Word.Word32 Float where - tryCast = maybeTryCast $ \s -> - if s <= maxFloat then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast + $ \s -> if s <= maxFloat then Just $ fromIntegral s else Nothing instance Cast.Cast Word.Word32 Double where cast = fromIntegral @@ -456,12 +457,12 @@ instance Cast.Cast Word.Word64 Integer where cast = fromIntegral instance TryCast.TryCast Word.Word64 Float where - tryCast = maybeTryCast $ \s -> - if s <= maxFloat then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast + $ \s -> if s <= maxFloat then Just $ fromIntegral s else Nothing instance TryCast.TryCast Word.Word64 Double where - tryCast = maybeTryCast $ \s -> - if s <= maxDouble then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast + $ \s -> if s <= maxDouble then Just $ fromIntegral s else Nothing -- Word @@ -499,14 +500,14 @@ instance Cast.Cast Word Integer where cast = fromIntegral instance TryCast.TryCast Word Float where - tryCast = maybeTryCast $ \s -> - if s <= maxFloat then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast + $ \s -> if s <= maxFloat then Just $ fromIntegral s else Nothing instance TryCast.TryCast Word Double where tryCast = maybeTryCast $ \s -> - if toInteger (maxBound :: Word) <= maxDouble + if (toInteger (maxBound :: Word) <= maxDouble) || (s <= maxDouble) then Just $ fromIntegral s - else if s <= maxDouble then Just $ fromIntegral s else Nothing + else Nothing -- Natural @@ -544,12 +545,12 @@ instance Cast.Cast Natural.Natural Integer where cast = fromIntegral instance TryCast.TryCast Natural.Natural Float where - tryCast = maybeTryCast $ \s -> - if s <= maxFloat then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast + $ \s -> if s <= maxFloat then Just $ fromIntegral s else Nothing instance TryCast.TryCast Natural.Natural Double where - tryCast = maybeTryCast $ \s -> - if s <= maxDouble then Just $ fromIntegral s else Nothing + tryCast = maybeTryCast + $ \s -> if s <= maxDouble then Just $ fromIntegral s else Nothing -- Float @@ -594,8 +595,8 @@ instance TryCast.TryCast Float Natural.Natural where tryCast = tryCastVia @Integer instance TryCast.TryCast Float Rational where - tryCast = maybeTryCast $ \s -> - if isNaN s || isInfinite s then Nothing else Just $ toRational s + tryCast = maybeTryCast + $ \s -> if isNaN s || isInfinite s then Nothing else Just $ toRational s instance Cast.Cast Float Double where cast = realToFrac @@ -643,8 +644,8 @@ instance TryCast.TryCast Double Natural.Natural where tryCast = tryCastVia @Integer instance TryCast.TryCast Double Rational where - tryCast = maybeTryCast $ \s -> - if isNaN s || isInfinite s then Nothing else Just $ toRational s + tryCast = maybeTryCast + $ \s -> if isNaN s || isInfinite s then Nothing else Just $ toRational s instance Cast.Cast Double Float where cast = realToFrac @@ -798,7 +799,7 @@ maybeTryCast f s = case f s of tryCastVia :: forall through source target - . (TryCast.TryCast source through, TryCast.TryCast through target) + . (TryCast.TryCast source through, TryCast.TryCast through target) => source -> Either (TryCastException.TryCastException source target) target tryCastVia s = case TryCast.tryCast s of diff --git a/src/lib/Witch/TryCastException.hs b/src/lib/Witch/TryCastException.hs index e91db6e..0f6159e 100644 --- a/src/lib/Witch/TryCastException.hs +++ b/src/lib/Witch/TryCastException.hs @@ -15,11 +15,13 @@ instance , Typeable.Typeable source , Typeable.Typeable target ) => Show (TryCastException source target) where - showsPrec d (TryCastException x) = showParen (d > 10) - $ showString "TryCastException {- " - . shows (Typeable.typeRep (Proxy.Proxy :: Proxy.Proxy (source -> target))) - . showString " -} " - . showsPrec 11 x + showsPrec d (TryCastException x) = + showParen (d > 10) + $ showString "TryCastException {- " + . shows + (Typeable.typeRep (Proxy.Proxy :: Proxy.Proxy (source -> target))) + . showString " -} " + . showsPrec 11 x instance ( Show source diff --git a/src/test/Main.hs b/src/test/Main.hs index a374854..4e0ac30 100644 --- a/src/test/Main.hs +++ b/src/test/Main.hs @@ -24,7 +24,7 @@ import qualified Data.Text.Lazy as LazyText import qualified Data.Word as Word import qualified Numeric.Natural as Natural import qualified Test.Hspec as Hspec -import qualified Witch as Witch +import qualified Witch main :: IO () main = Hspec.hspec . Hspec.describe "Witch" $ do @@ -82,7 +82,7 @@ main = Hspec.hspec . Hspec.describe "Witch" $ do test $ ($$(Witch.liftedFrom @Int.Int16 1) :: Int.Int8) `Hspec.shouldBe` 1 Hspec.describe "liftedInto" $ do - test $ ($$(Witch.liftedInto @Int.Int8 (1 :: Int.Int16))) `Hspec.shouldBe` 1 + test $ $$(Witch.liftedInto @Int.Int8 (1 :: Int.Int16)) `Hspec.shouldBe` 1 Hspec.describe "Instances" $ do @@ -1463,14 +1463,14 @@ main = Hspec.hspec . Hspec.describe "Witch" $ do Hspec.describe "Cast [(k, v)] (Map k v)" $ do let f = Witch.cast @[(Char, Int)] @(Map.Map Char Int) - test $ f [] `Hspec.shouldBe` Map.fromList [] + test $ f [] `Hspec.shouldBe` Map.empty test $ f [('a', 1)] `Hspec.shouldBe` Map.fromList [('a', 1)] test $ f [('a', 1), ('b', 2)] `Hspec.shouldBe` Map.fromList [('a', 1), ('b', 2)] test $ f [('a', 1), ('a', 2)] `Hspec.shouldBe` Map.fromList [('a', 2)] Hspec.describe "Cast (Map k v) [(k, v)]" $ do let f = Witch.cast @(Map.Map Char Int) @[(Char, Int)] - test $ f (Map.fromList []) `Hspec.shouldBe` [] + test $ f Map.empty `Hspec.shouldBe` [] test $ f (Map.fromList [('a', 1)]) `Hspec.shouldBe` [('a', 1)] test $ f (Map.fromList [('a', 1), ('b', 2)]) `Hspec.shouldBe` [('a', 1), ('b', 2)]