Notification.hs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. {-# LANGUAGE DerivingStrategies #-}
  2. {-# LANGUAGE OverloadedStrings #-}
  3. {-# LANGUAGE ScopedTypeVariables #-}
  4. module Aria2.Notification
  5. ( NotificationType (..),
  6. Notification,
  7. notificationType,
  8. gid,
  9. )
  10. where
  11. import Aria2.GID (GID)
  12. import Data.Aeson ((.:), FromJSON (..), withObject)
  13. import Data.Text (Text, stripPrefix)
  14. data NotificationType
  15. = OnDownloadStart
  16. | OnDownloadPause
  17. | OnDownloadStop
  18. | OnDownloadComplete
  19. | OnDownloadError
  20. | OnBtDownloadComplete
  21. deriving stock (Eq, Show)
  22. data Notification = Notification !NotificationType {-# UNPACK #-} !GID
  23. deriving stock (Eq, Show)
  24. notificationType :: Notification -> NotificationType
  25. notificationType (Notification t _) = t
  26. gid :: Notification -> GID
  27. gid (Notification _ g) = g
  28. instance FromJSON Notification where
  29. parseJSON = withObject "Notification" go
  30. where
  31. go obj = do
  32. method :: Text <- obj .: "method"
  33. nt <- case stripPrefix "aria2." method of
  34. Nothing -> fail ("Not a valid Aria2 method: " <> show method)
  35. Just methodName -> case methodName of
  36. "onDownloadStart" -> pure OnDownloadStart
  37. "onDownloadPause" -> pure OnDownloadPause
  38. "onDownloadStop" -> pure OnDownloadStop
  39. "onDownloadComplete" -> pure OnDownloadComplete
  40. "onDownloadError" -> pure OnDownloadError
  41. "onBtDownloadComplete" -> pure OnBtDownloadComplete
  42. t -> fail ("Not a valid Aria2 notification: " <> show t)
  43. Notification nt <$> obj .: "gid"