on master help sign in
Added some wrap functions
commit8c4d1344946f97d94f89f68d94ce318cd9dbb9bf authorMichael Snoyman <michael@snoyman.com> dateMon 23 Nov 2009, 20:00 (17 years ago) parentd206726
2 files changed+33−2
Text/Yaml.hs+31−1 yaml.cabal+2−1
Text/Yaml.hs+31−1
@@ -18,15 +18,21 @@
1818
1919module Text.Yaml
2020 (
21+ -- * Exceptions
22+ YamlException (..)
23+ ,
2124 -- * Converting to/from 'TextObject's
2225 encodeText
2326 , decodeText
27+ , decodeFromText
28+ , decodeFromTextWrap
2429 -- * Convert to/from 'ScalarObejct's
2530 , encodeScalar
2631 , decodeScalar
2732 -- * Files
2833 , encodeFile
2934 , decodeFile
35+ , decodeFileFromTextWrap
3036#if TEST
3137 , testSuite
3238#endif
@@ -44,7 +50,7 @@ import Control.Arrow (first)
4450import Control.Monad (liftM, join)
4551import Control.Monad.Trans
4652import Data.Convertible
47−import Control.Monad.Failure
53+import Data.Attempt
4854
4955import Data.Text.Lazy (Text)
5056import qualified Data.Text.Lazy as LT
@@ -94,6 +100,21 @@ decodeText :: (ConvertSuccess a B.ByteString,
94100 -> m TextObject
95101decodeText = unsafePerformIO . decode'
96102
103+decodeFromText :: (ConvertSuccess a B.ByteString,
104+ FromObject x Text Text)
105+ => a
106+ -> Attempt x
107+decodeFromText a = decodeText a >>= fromObject
108+
109+decodeFromTextWrap :: (ConvertSuccess a B.ByteString,
110+ FromObject x Text Text,
111+ MonadFailure YamlException m,
112+ MonadFailure FromObjectException m
113+ )
114+ => a
115+ -> m x
116+decodeFromTextWrap a = decodeText a >>= fromObjectWrap
117+
97118-- Scalar API
98119-- FIXME: this *should* be in a MonadFailure...
99120encodeScalar :: ConvertSuccess B.ByteString a
@@ -131,6 +152,15 @@ decodeFile path = do
131152ltReadFile :: FilePath -> IO Text
132153ltReadFile = fmap LTE.decodeUtf8 . BL.readFile
133154
155+decodeFileFromTextWrap :: (MonadFailure YamlException m,
156+ MonadFailure FromObjectException m,
157+ MonadIO m,
158+ FromObject x Text Text
159+ )
160+ => FilePath
161+ -> m x
162+decodeFileFromTextWrap path = decodeFile path >>= fromObjectWrap
163+
134164-- The real worker functions
135165objectToEvents :: TextObject -> [Event]
136166objectToEvents y = concat
yaml.cabal+2−1
@@ -33,7 +33,8 @@ library
3333 transformers >= 0.1.4.0,
3434 control-monad-failure >= 0.4,
3535 text >= 0.5,
36− convertible >= 1.2.0
36+ convertible >= 1.2.0,
37+ attempt >= 0.0.2
3738 exposed-modules: Text.Yaml
3839 other-modules: Text.Libyaml
3940 ghc-options: -Wall