shrub/pkg/hs/urbit-king/lib/Ur/Noun.hs

58 lines
1.3 KiB
Haskell
Raw Normal View History

2020-01-23 07:16:09 +03:00
{-|
Noun Library
This module just re-exports things from submodules.
-}
2020-01-23 05:58:22 +03:00
module Ur.Noun
( module Urbit.Atom
2019-08-13 01:51:37 +03:00
, module Data.Word
2020-01-23 05:58:22 +03:00
, module Ur.Noun.Conversions
, module Ur.Noun.Convert
, module Ur.Noun.Core
, module Ur.Noun.Cue
, module Ur.Noun.Jam
, module Ur.Noun.Tank
, module Ur.Noun.TH
, module Ur.Noun.Tree
2019-07-12 04:16:40 +03:00
, _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
import Urbit.Atom
2020-01-23 05:58:22 +03:00
import Ur.Noun.Conversions
import Ur.Noun.Convert
import Ur.Noun.Core
import Ur.Noun.Cue
import Ur.Noun.Jam
import Ur.Noun.Tank
import Ur.Noun.TH
2020-01-23 07:16:09 +03:00
import Ur.Noun.Tree
--------------------------------------------------------------------------------
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