shrub/pkg/king/lib/Noun.hs

51 lines
1.1 KiB
Haskell
Raw Normal View History

module Noun
2019-08-13 01:51:37 +03:00
( module Noun.Atom
, module Data.Word
2019-07-12 04:16:40 +03:00
, module Noun.Conversions
2019-08-13 01:51:37 +03:00
, module Noun.Convert
, module Noun.Core
2019-07-12 04:16:40 +03:00
, module Noun.Cue
2019-08-13 01:51:37 +03:00
, module Noun.Jam
, module Noun.Tank
2019-07-12 04:16:40 +03:00
, module Noun.TH
, _Cue
2019-08-22 02:49:08 +03:00
, LoadErr(..)
2019-07-16 03:01:45 +03:00
, loadFile
2019-07-12 04:16:40 +03:00
) where
import ClassyPrelude
import Control.Lens
2019-08-13 01:51:37 +03:00
import Data.Word
2019-07-12 04:16:40 +03:00
import Noun.Atom
2019-07-12 22:24:44 +03:00
import Noun.Conversions
import Noun.Convert
import Noun.Core
2019-07-12 04:16:40 +03:00
import Noun.Cue
2019-07-12 22:24:44 +03:00
import Noun.Jam
2019-08-13 01:51:37 +03:00
import Noun.Tank
2019-07-12 04:16:40 +03:00
import Noun.TH
--------------------------------------------------------------------------------
2019-07-12 04:16:40 +03:00
_Cue :: Prism' ByteString Noun
_Cue = prism' jamBS (eitherToMaybe . cueBS)
where
2019-07-12 04:16:40 +03:00
eitherToMaybe (Left _) = Nothing
eitherToMaybe (Right x) = Just x
2019-07-16 03:01:45 +03:00
2019-08-22 02:49:08 +03:00
data LoadErr
= FileErr IOException
| CueErr DecodeErr
| ParseErr [Text] Text
deriving (Show)
instance Exception LoadErr
2019-07-16 03:01:45 +03:00
loadFile :: a. FromNoun a => FilePath -> IO (Either LoadErr a)
2019-08-22 02:49:08 +03:00
loadFile pax = try $ do
byt <- try (readFile pax) >>= either (throwIO . FileErr) pure
non <- cueBS byt & either (throwIO . CueErr) pure
res <- fromNounErr non & either (throwIO . uncurry ParseErr) pure
pure res