used DeriveAny to derive Binary & FromJSON instances

This commit is contained in:
flupe 2020-09-27 01:53:13 +02:00
parent dbae6b3623
commit 9598b36d69
7 changed files with 21 additions and 53 deletions

View File

@ -10,7 +10,6 @@ executable site
hs-source-dirs: src hs-source-dirs: src
other-modules: Templates other-modules: Templates
, Types , Types
, Page
, Posts , Posts
, Projects , Projects
, Common , Common

View File

@ -9,16 +9,22 @@ module Common
, module Control.Monad , module Control.Monad
, module Data.Maybe , module Data.Maybe
, module Lucid , module Lucid
, module Data.Binary
, module GHC.Generics
, module Data.Aeson.Types
) where ) where
import Achille import Achille
import Achille.Recipe.Pandoc import Achille.Recipe.Pandoc
import Data.Functor ((<&>)) import Data.Aeson.Types (FromJSON)
import Control.Monad (forM_, when) import GHC.Generics (Generic)
import Data.Sort (sort) import Data.Binary (Binary)
import Data.String (fromString) import Data.Functor ((<&>))
import Data.Text (Text) import Control.Monad (forM_, when)
import Data.Maybe (fromMaybe, mapMaybe) import Data.Sort (sort)
import Data.String (fromString)
import Data.Text (Text)
import Data.Maybe (fromMaybe, mapMaybe)
import System.FilePath import System.FilePath
import Lucid (Html) import Lucid (Html)

View File

@ -5,10 +5,11 @@ import Lucid
import Common import Common
import Templates import Templates
import Config (config, ropts, wopts, SiteConfig(title))
import qualified Posts import qualified Posts
import qualified Projects import qualified Projects
import qualified Visual import qualified Visual
import Config (config, ropts, wopts, SiteConfig(title))
main :: IO () main :: IO ()

View File

@ -1,13 +0,0 @@
{-# LANGUAGE DeriveGeneric #-}
module Page where
import GHC.Generics
import Data.Aeson.Types (FromJSON)
data Page = Page
{ title :: String
, draft :: Maybe Bool
} deriving (Generic, Eq, Show)
instance FromJSON Page

View File

@ -1,5 +1,3 @@
{-# LANGUAGE DeriveGeneric #-}
module Posts (build) where module Posts (build) where
import Data.Aeson.Types (FromJSON) import Data.Aeson.Types (FromJSON)
@ -24,7 +22,7 @@ data PostMeta = PostMeta
{ title :: Text { title :: Text
, draft :: Maybe Bool , draft :: Maybe Bool
, description :: Maybe Text , description :: Maybe Text
} deriving (Generic, Eq, Show) } deriving (Generic, Eq, Show, FromJSON)
data Post = Post data Post = Post
{ postTitle :: Text { postTitle :: Text
@ -33,14 +31,9 @@ data Post = Post
, postDescription :: Maybe Text , postDescription :: Maybe Text
, postContent :: Text , postContent :: Text
, postPath :: FilePath , postPath :: FilePath
} deriving (Generic, Eq, Show) } deriving (Generic, Eq, Show, Binary)
instance FromJSON PostMeta
instance IsTimestamped Post where timestamp = postDate instance IsTimestamped Post where timestamp = postDate
instance Binary Post where
put (Post t d dr desc content path) =
put t >> put d >> put dr >> put desc >> put content >> put path
get = Post <$> get <*> get <*> get <*> get <*> get <*> get
buildPost :: Recipe IO FilePath Post buildPost :: Recipe IO FilePath Post

View File

@ -4,7 +4,6 @@ import Data.Char (digitToInt)
import Common import Common
import Types import Types
import Page
import Config import Config
import Templates import Templates
import Lucid import Lucid

View File

@ -1,16 +1,11 @@
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-} {-# LANGUAGE DuplicateRecordFields #-}
module Types where module Types where
import GHC.Generics
import Data.Aeson.Types (FromJSON)
import Data.Binary (Binary, put, get)
import Data.Time.LocalTime (ZonedTime) import Data.Time.LocalTime (ZonedTime)
import Data.Binary.Instances.Time () import Data.Binary.Instances.Time ()
import Data.Text (Text)
import qualified Data.Map.Strict as Map import qualified Data.Map.Strict as Map
import Common
-- | Full project description -- | Full project description
data Project = Project data Project = Project
@ -19,13 +14,13 @@ data Project = Project
, year :: String , year :: String
, labels :: Map.Map String String , labels :: Map.Map String String
, gallery :: Maybe Bool , gallery :: Maybe Bool
} deriving (Generic, Eq, Show) } deriving (Generic, Eq, Show, FromJSON, Binary)
data TitledPage = TitledPage data TitledPage = TitledPage
{ title :: String { title :: String
, description :: Maybe String , description :: Maybe String
} deriving (Generic, Eq, Show) } deriving (Generic, Eq, Show, FromJSON, Binary)
-- | Book description for the readings page -- | Book description for the readings page
@ -34,16 +29,4 @@ data Book = Book
, author :: Text , author :: Text
, rating :: Maybe Int , rating :: Maybe Int
, completed :: Maybe ZonedTime , completed :: Maybe ZonedTime
} deriving (Generic, Show) } deriving (Generic, Show, FromJSON)
instance FromJSON Project
instance FromJSON TitledPage
instance FromJSON Book
instance Binary Project where
put (Project t s y l g) = put t >> put s >> put y >> put l >> put g
get = Project <$> get <*> get <*> get <*> get <*> get
instance Binary Book where
put (Book t a r c) = put t >> put a >> put r >> put c
get = Book <$> get <*> get <*> get <*> get