module Text.RSS.Types where
import Control.Exception.Safe
import Data.Semigroup
import Data.Set
import Data.Singletons.Prelude.List
import Data.Text hiding (map)
import Data.Time.Clock
import Data.Time.LocalTime ()
import Data.Version
import Data.Vinyl.Core
import GHC.Generics hiding ((:+:))
import Text.Read
import URI.ByteString
data RssException = InvalidBool Text
| InvalidDay Text
| InvalidHour Int
| InvalidInt Text
| InvalidURI URIParseError
| InvalidVersion Text
| InvalidProtocol Text
| InvalidTime Text
| MissingElement Text
deriving instance Eq RssException
deriving instance Generic RssException
deriving instance Show RssException
instance Exception RssException where
displayException (InvalidBool t) = "Invalid bool: " ++ unpack t
displayException (InvalidDay t) = "Invalid day: " ++ unpack t
displayException (InvalidHour i) = "Invalid hour: " ++ show i
displayException (InvalidInt t) = "Invalid int: " ++ unpack t
displayException (InvalidURI t) = "Invalid URI reference: " ++ show t
displayException (InvalidVersion t) = "Invalid version: " ++ unpack t
displayException (InvalidProtocol t) = "Invalid Protocol: expected \"xml-rpc\", \"soap\" or \"http-post\", got \"" ++ unpack t ++ "\""
displayException (InvalidTime t) = "Invalid time: " ++ unpack t
displayException (MissingElement t) = "Missing element: " ++ unpack t
data RssURI = forall a . RssURI (URIRef a)
instance Eq RssURI where
RssURI a@URI{} == RssURI b@URI{} = a == b
RssURI a@RelativeRef{} == RssURI b@RelativeRef{} = a == b
_ == _ = False
instance Ord RssURI where
RssURI a@URI{} `compare` RssURI b@URI{} = a `compare` b
RssURI a@RelativeRef{} `compare` RssURI b@RelativeRef{} = a `compare` b
RssURI a@RelativeRef{} `compare` RssURI b@URI{} = LT
_ `compare` _ = GT
instance Show RssURI where
show (RssURI a@URI{}) = show a
show (RssURI a@RelativeRef{}) = show a
withRssURI :: (forall a . URIRef a -> b) -> RssURI -> b
withRssURI f (RssURI a) = f a
data RssCategory = RssCategory
{ categoryDomain :: Text
, categoryName :: Text
}
deriving instance Eq RssCategory
deriving instance Generic RssCategory
deriving instance Ord RssCategory
deriving instance Show RssCategory
data RssEnclosure = RssEnclosure
{ enclosureUrl :: RssURI
, enclosureLength :: Int
, enclosureType :: Text
}
deriving instance Eq RssEnclosure
deriving instance Generic RssEnclosure
deriving instance Ord RssEnclosure
deriving instance Show RssEnclosure
data RssSource = RssSource
{ sourceUrl :: RssURI
, sourceName :: Text
}
deriving instance Eq RssSource
deriving instance Generic RssSource
deriving instance Ord RssSource
deriving instance Show RssSource
data RssGuid = GuidText Text | GuidUri RssURI
deriving(Eq, Generic, Ord, Show)
data RssItem (extensions :: [*]) = RssItem
{ itemTitle :: Text
, itemLink :: Maybe RssURI
, itemDescription :: Text
, itemAuthor :: Text
, itemCategories :: [RssCategory]
, itemComments :: Maybe RssURI
, itemEnclosure :: [RssEnclosure]
, itemGuid :: Maybe RssGuid
, itemPubDate :: Maybe UTCTime
, itemSource :: Maybe RssSource
, itemExtensions :: RssItemExtensions extensions
}
deriving instance (Eq (RssItemExtensions e)) => Eq (RssItem e)
deriving instance (Generic (RssItemExtensions e)) => Generic (RssItem e)
deriving instance (Ord (RssItemExtensions e)) => Ord (RssItem e)
deriving instance (Show (RssItemExtensions e)) => Show (RssItem e)
type RssItem' = RssItem '[]
data RssTextInput = RssTextInput
{ textInputTitle :: Text
, textInputDescription :: Text
, textInputName :: Text
, textInputLink :: RssURI
}
deriving instance Eq RssTextInput
deriving instance Generic RssTextInput
deriving instance Ord RssTextInput
deriving instance Show RssTextInput
data CloudProtocol = ProtocolXmlRpc | ProtocolSoap | ProtocolHttpPost
deriving(Eq, Generic, Ord, Show)
data RssCloud = RssCloud
{ cloudUri :: RssURI
, cloudRegisterProcedure :: Text
, cloudProtocol :: CloudProtocol
}
deriving instance Eq RssCloud
deriving instance Generic RssCloud
deriving instance Ord RssCloud
deriving instance Show RssCloud
data RssImage = RssImage
{ imageUri :: RssURI
, imageTitle :: Text
, imageLink :: RssURI
, imageWidth :: Maybe Int
, imageHeight :: Maybe Int
, imageDescription :: Text
}
deriving instance Eq RssImage
deriving instance Generic RssImage
deriving instance Ord RssImage
deriving instance Show RssImage
newtype Hour = Hour Int
deriving(Eq, Generic, Ord, Read, Show)
instance Bounded Hour where
minBound = Hour 0
maxBound = Hour 23
instance Enum Hour where
fromEnum (Hour h) = fromEnum h
toEnum i = if i >= 0 && i < 24 then Hour i else error $ "Invalid hour: " <> show i
asHour :: MonadThrow m => Int -> m Hour
asHour i
| i >= 0 && i < 24 = return $ Hour i
| otherwise = throwM $ InvalidHour i
data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
deriving(Bounded, Enum, Eq, Generic, Ord, Read, Show)
asDay :: MonadThrow m => Text -> m Day
asDay t = maybe (throwM $ InvalidDay t) return . readMaybe $ unpack t
data RssDocument (extensions :: [*]) = RssDocument
{ documentVersion :: Version
, channelTitle :: Text
, channelLink :: RssURI
, channelDescription :: Text
, channelItems :: [RssItem extensions]
, channelLanguage :: Text
, channelCopyright :: Text
, channelManagingEditor :: Text
, channelWebmaster :: Text
, channelPubDate :: Maybe UTCTime
, channelLastBuildDate :: Maybe UTCTime
, channelCategories :: [RssCategory]
, channelGenerator :: Text
, channelDocs :: Maybe RssURI
, channelCloud :: Maybe RssCloud
, channelTtl :: Maybe Int
, channelImage :: Maybe RssImage
, channelRating :: Text
, channelTextInput :: Maybe RssTextInput
, channelSkipHours :: Set Hour
, channelSkipDays :: Set Day
, channelExtensions :: RssChannelExtensions extensions
}
deriving instance (Eq (RssChannelExtensions e), Eq (RssItemExtensions e)) => Eq (RssDocument e)
deriving instance (Generic (RssChannelExtensions e), Generic (RssItemExtensions e)) => Generic (RssDocument e)
deriving instance (Ord (RssChannelExtensions e), Ord (RssItemExtensions e)) => Ord (RssDocument e)
deriving instance (Show (RssChannelExtensions e), Show (RssItemExtensions e)) => Show (RssDocument e)
type RssDocument' = RssDocument '[]
data family RssChannelExtension extensionTag :: *
data family RssItemExtension extensionTag :: *
data family RssChannelExtensions (extensionTags :: [*]) :: *
data instance RssChannelExtensions a = RssChannelExtensions { rssChannelExtension :: Rec RssChannelExtension a }
deriving instance (Eq (Rec RssChannelExtension a)) => Eq (RssChannelExtensions a)
deriving instance (Generic (Rec RssChannelExtension a)) => Generic (RssChannelExtensions a)
deriving instance (Ord (Rec RssChannelExtension a)) => Ord (RssChannelExtensions a)
deriving instance (Show (Rec RssChannelExtension a)) => Show (RssChannelExtensions a)
data family RssItemExtensions (extensionTags :: [*]) :: *
data instance RssItemExtensions (a :: [*]) = RssItemExtensions { rssItemExtension :: Rec RssItemExtension a }
deriving instance (Eq (Rec RssItemExtension a)) => Eq (RssItemExtensions a)
deriving instance (Generic (Rec RssItemExtension a)) => Generic (RssItemExtensions a)
deriving instance (Ord (Rec RssItemExtension a)) => Ord (RssItemExtensions a)
deriving instance (Show (Rec RssItemExtension a)) => Show (RssItemExtensions a)