1 {-# LANGUAGE UndecidableInstances #-}
2 {-# LANGUAGE NoMonomorphismRestriction #-}
3 {-# OPTIONS_GHC -Wno-missing-signatures #-}
5 module Examples.Ex04 where
7 import Control.Monad.Classes qualified as MC
8 import Data.Map.Strict as Map
11 import Symantic qualified as Sym
14 "post" </> capturePathSegment @PostName "post" <.> response @Post @'[PlainText]
15 <+> "page" </> capturePathSegment @PageName "page" <.> response @Page @'[PlainText]
16 <+> pathSegment "lorem"
19 -- ( PostName -> (a :~: a, Post)
20 -- , PageName -> (a :~: a, Page) )
22 -- MC.MonadReader Model m =>
23 -- endpoint ~ m Text =>
25 -- (Either (PostName, Sym.Endpoint endpoint (m Post))
26 -- (PageName, Sym.Endpoint endpoint (m Page)))
28 content = contentPost :!: contentPage :!: contentOther
30 contentPost n = Sym.endpoint do
32 return $ modelPosts Map.! n
33 contentPage = Sym.endpoint . \case
34 CapturedExtra (Left (_n, p)) -> do
36 CapturedExtra (Right n) -> do
38 return $ modelPages Map.! n
39 contentOther = return "ipsum"
41 instance MimeEncodable Post PlainText where
42 mimeEncode (Post t) = mimeEncode @_ @PlainText t
43 instance MimeEncodable Page PlainText where
44 mimeEncode (Page t) = mimeEncode @_ @PlainText t
46 instance MC.MonadReader Model m => Capturable PostName (Compiler m) where
47 capturePathSegment _n =
51 [ Output{outputPath = [unPostName name], outputExts = [], outputData = name}
52 | name <- Map.keys (modelPosts model)
54 instance MC.MonadReader Model m => Capturable PageName (Compiler m) where
55 -- Keep the 'Page' to avoid looking it up in 'contentPage'.
56 type Captured PageName (Compiler m) = CapturedExtra PageName Page
57 capturePathSegment _n =
62 { outputPath = [unPageName name]
64 , outputData = CapturedExtra (Left (name, page))
66 | (name, page) <- Map.toList (modelPages model)
71 { modelPosts :: Map PostName Post
72 , modelPages :: Map PageName Page
78 [ (PostName "post1", Post "post-model-1")
79 , (PostName "post2", Post "post-model-2")
83 [ (PageName "page1", Page "page-model-1")
84 , (PageName "page2", Page "page-model-2")
89 data PostName = PostName {unPostName :: PathSegment}
90 deriving (Show, Eq, Ord)
93 data PageName = PageName {unPageName :: PathSegment}
94 deriving (Show, Eq, Ord)
97 data Post = Post {unPost :: Text}
101 data Page = Page {unPage :: Text}