summaryrefslogtreecommitdiff
path: root/src/Hakyll/Aeson.hs
diff options
context:
space:
mode:
authorChris Wells <chris@mathematicaster.org>2025-02-10 12:48:38 -0600
committerChris Wells <chris@mathematicaster.org>2025-02-10 12:48:38 -0600
commit0c677070edf9978cb85a0b521f7e6e0ef6d6b491 (patch)
tree1bf0a024ea2c1ccc9c21c68b916e76017bafd310 /src/Hakyll/Aeson.hs
parent75ac829ee7d661d758f7828e609e28ee4e05d038 (diff)
downloadmain-0c677070edf9978cb85a0b521f7e6e0ef6d6b491.tar
main-0c677070edf9978cb85a0b521f7e6e0ef6d6b491.tar.gz
main-0c677070edf9978cb85a0b521f7e6e0ef6d6b491.tar.bz2
main-0c677070edf9978cb85a0b521f7e6e0ef6d6b491.tar.lz
main-0c677070edf9978cb85a0b521f7e6e0ef6d6b491.tar.xz
main-0c677070edf9978cb85a0b521f7e6e0ef6d6b491.tar.zst
main-0c677070edf9978cb85a0b521f7e6e0ef6d6b491.zip
A lot of work on aliases and the like
Diffstat (limited to 'src/Hakyll/Aeson.hs')
-rw-r--r--src/Hakyll/Aeson.hs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/Hakyll/Aeson.hs b/src/Hakyll/Aeson.hs
new file mode 100644
index 0000000..f4090d5
--- /dev/null
+++ b/src/Hakyll/Aeson.hs
@@ -0,0 +1,42 @@
+module Hakyll.Aeson
+ ( compileJSON
+ , jsonCompiler
+ , compileYAML
+ , yamlCompiler
+ , JSONWriter (..)
+ , YAMLWriter (..)
+ ) where
+
+import Prolog
+import Hakyll
+import Hakyll.Error
+import Data.Aeson as A
+import Data.Yaml qualified as Y
+
+
+compileJSON :: (FromJSON a, StringConv s LazyByteString, Traversable t, MonadError [String] m) => t s -> m (t a)
+compileJSON = traverse $ liftErr . A.eitherDecode . toSL
+
+jsonCompiler :: FromJSON a => Compiler (Item a)
+jsonCompiler = getResourceLBS >>= compileJSON
+
+compileYAML :: (FromJSON a, StringConv s StrictByteString, Traversable t, MonadThrow m) => t s -> m (t a)
+compileYAML = traverse $ Y.decodeThrow . toSL
+
+yamlCompiler :: FromJSON a => Compiler (Item a)
+yamlCompiler = getResourceLBS >>= compileYAML
+
+
+newtype JSONWriter a = JSONWriter { runJSONWriter :: a }
+ deriving stock (Generic, Show)
+ deriving newtype (FromJSON, ToJSON, Binary)
+
+instance ToJSON a => Writable (JSONWriter a) where
+ write p = A.encodeFile p . runJSONWriter . itemBody
+
+newtype YAMLWriter a = YAMLWriter { runYAMLWriter :: a }
+ deriving stock (Generic, Show)
+ deriving newtype (FromJSON, ToJSON, Binary)
+
+instance ToJSON a => Writable (YAMLWriter a) where
+ write p = Y.encodeFile p . runYAMLWriter . itemBody