aboutsummaryrefslogtreecommitdiffstats
path: root/app/Issue/TypedValue.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2023-10-17 11:56:11 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2023-10-17 11:56:11 +0200
commitab9f65916c81fd82f05befc0679c45fe05f26531 (patch)
tree950431c14e9663712f51ac17ebe9833132664d3d /app/Issue/TypedValue.hs
parentb24f614f0f6aa8363b12f007a44a5d4bc41ec739 (diff)
make sort type-aware
Diffstat (limited to 'app/Issue/TypedValue.hs')
-rw-r--r--app/Issue/TypedValue.hs33
1 files changed, 33 insertions, 0 deletions
diff --git a/app/Issue/TypedValue.hs b/app/Issue/TypedValue.hs
new file mode 100644
index 0000000..9af04bb
--- /dev/null
+++ b/app/Issue/TypedValue.hs
@@ -0,0 +1,33 @@
+module Issue.TypedValue
+ ( Type (..),
+ cast,
+ castDef,
+ )
+where
+
+import Data.Maybe (fromMaybe)
+import Data.Text (Text)
+import Data.Text qualified as T
+import Data.Time.Calendar (Day)
+import Text.Read (readMaybe)
+
+data Type a where
+ Date :: Type Day
+ Int :: Type Int
+ String :: Type Text
+
+cast :: (forall a. Ord a => a -> a -> b) -> (Text -> Text -> Maybe b)
+cast eq x y
+ | Just x' <- castTo Date x, Just y' <- castTo Date y = Just (eq x' y')
+ | Just _ <- castTo Date x, Nothing <- castTo Date y = Nothing
+ | Just x' <- castTo Int x, Just y' <- castTo Int y = Just (eq x' y')
+ | Just _ <- castTo Int x, Nothing <- castTo Int y = Nothing
+ | otherwise = Just (eq x y)
+
+castDef :: b -> (forall a. Ord a => a -> a -> b) -> (Text -> Text -> b)
+castDef def eq x y = fromMaybe def (cast eq x y)
+
+castTo :: Type a -> Text -> Maybe a
+castTo Date = readMaybe . T.unpack
+castTo Int = readMaybe . T.unpack
+castTo String = Just