Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Basic encoding and decoding

  • Loading branch information...
commit a7c2fd459a5491b69f16b39f2e19e91320cc9600 1 parent 58f6baa
@snoyberg authored
View
195 Data/Object/Yaml.hs
@@ -0,0 +1,195 @@
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE CPP #-}
+{-# LANGUAGE DeriveDataTypeable #-}
+module Data.Object.Yaml
+ ( -- * Definition of 'YamlObject'
+ YamlScalar (..)
+ , YamlObject
+ -- * Encoding/decoding
+ , encode
+ , encodeFile
+ , decode
+ , decodeFile
+#if TEST
+ , testSuite
+#endif
+ ) where
+
+import qualified Text.Libyaml as Y
+import Text.Libyaml hiding (encode, decode, encodeFile, decodeFile)
+import Data.Object
+import Data.ByteString (ByteString)
+import System.IO.Unsafe (unsafePerformIO)
+import Control.Exception (throw, Exception, SomeException (..))
+import Data.Typeable (Typeable)
+-- debugging purposes import Debug.Trace
+import Control.Failure
+import Control.Applicative ((<$>))
+
+#if TEST
+import Test.Framework (testGroup, Test)
+import Test.Framework.Providers.HUnit
+--import Test.Framework.Providers.QuickCheck (testProperty)
+import Test.HUnit hiding (Test, path)
+--import Test.QuickCheck
+
+import Control.Monad (join)
+#endif
+
+-- | Equality depends on 'value' and 'tag', not 'style'.
+data YamlScalar = YamlScalar
+ { value :: ByteString
+ , tag :: Tag
+ , style :: Style
+ }
+ deriving (Show)
+instance Eq YamlScalar where
+ (YamlScalar v t _) == (YamlScalar v' t' _) = v == v' && t == t'
+
+type YamlObject = Object YamlScalar YamlScalar
+
+encode :: YamlObject -> ByteString
+encode yo = either throw id $ unsafePerformIO $ Y.encode ge $ Phase1 yo
+
+encodeFile :: MonadFailure YamlException m
+ => FilePath
+ -> YamlObject
+ -> IO (m ())
+encodeFile fp yo = try <$> (Y.encodeFile fp ge $ Phase1 yo)
+
+data GenState = Phase1 YamlObject
+ | Phase2 YamlObject
+ | Phase3 YamlObject GenState
+ | Phase4
+ | Phase5
+ | Phase6
+ | PhaseSeq [YamlObject] GenState
+ | PhaseMap [(YamlScalar, YamlObject)] GenState
+ | PhaseMap' YamlObject [(YamlScalar, YamlObject)] GenState
+ deriving (Eq, Show)
+
+{- Debugging purposes
+ge' gs =
+ let res = ge gs
+ in traceShow res res
+-}
+ge :: GenState -> Maybe (Event, GenState)
+ge (Phase1 yo) = Just (EventStreamStart, Phase2 yo)
+ge (Phase2 yo) = Just (EventDocumentStart, Phase3 yo Phase4)
+ge (Phase3 (Scalar (YamlScalar v t s)) n) = Just (EventScalar v t s, n)
+ge (Phase3 (Sequence yos) n) = Just (EventSequenceStart, PhaseSeq yos n)
+ge (Phase3 (Mapping pairs) n) = Just (EventMappingStart, PhaseMap pairs n)
+ge Phase4 = Just (EventDocumentEnd, Phase5)
+ge Phase5 = Just (EventStreamEnd, Phase6)
+ge Phase6 = Nothing
+ge (PhaseSeq [] n) = Just (EventSequenceEnd, n)
+ge (PhaseSeq (yo:yos) n) = ge $ Phase3 yo $ PhaseSeq yos n
+ge (PhaseMap [] n) = Just (EventMappingEnd, n)
+ge (PhaseMap ((k, v):pairs) n) = ge $ Phase3 (Scalar k) $ PhaseMap' v pairs n
+ge (PhaseMap' v pairs n) = ge $ Phase3 v $ PhaseMap pairs n
+
+decode :: MonadFailure YamlException m => ByteString -> m YamlObject
+decode bs = (try $ unsafePerformIO $ Y.decode bs pf Parse1) >>= unParseComplete
+
+decodeFile :: MonadFailure YamlException m => FilePath -> IO (m YamlObject)
+decodeFile fp = do
+ res <- Y.decodeFile fp pf Parse1
+ let res' = try res
+ return $ res' >>= unParseComplete
+
+data ParseState =
+ Parse1
+ | Parse2
+ | Parse3 (YamlObject -> ParseState)
+ | Parse4 YamlObject
+ | Parse5 YamlObject
+ | ParseComplete (Either YamlException YamlObject)
+ | ParseSeq (YamlObject -> ParseState) ([YamlObject] -> [YamlObject])
+ | ParseMap (YamlObject -> ParseState)
+ ([(YamlScalar, YamlObject)] -> [(YamlScalar, YamlObject)])
+ | ParseMap' (YamlObject -> ParseState)
+ ([(YamlScalar, YamlObject)] -> [(YamlScalar, YamlObject)])
+ YamlScalar
+ | ParseException ParseException
+
+unParseComplete :: MonadFailure YamlException m
+ => ParseState
+ -> m YamlObject
+unParseComplete (ParseComplete (Left e)) = failure e
+unParseComplete (ParseComplete (Right yo)) = return yo
+unParseComplete p = failure $ YamlOtherException $ SomeException
+ $ IncompleteParse p
+
+instance Show ParseState where
+ show Parse1{} = "Parse1"
+ show Parse2{} = "Parse2"
+ show Parse3{} = "Parse3"
+ show Parse4{} = "Parse4"
+ show Parse5{} = "Parse5"
+ show ParseComplete{} = "ParseComplete"
+ show ParseSeq{} = "ParseSeq"
+ show ParseMap{} = "ParseMap"
+ show ParseMap'{} = "ParseMap'"
+ show (ParseException e) = "ParseException " ++ show e
+
+-- debugging purposes only pf' p e = traceShow (p, e) $ pf p e
+
+pf :: ParseState -> Event -> Either ParseState ParseState
+pf Parse1 EventStreamStart = Right Parse2
+pf Parse2 EventDocumentStart = Right $ Parse3 Parse4
+pf (Parse3 n) (EventScalar v t s) = Right $ n $ Scalar $ YamlScalar v t s
+pf (Parse3 n) EventSequenceStart = Right $ ParseSeq n id
+pf (Parse3 n) EventMappingStart = Right $ ParseMap n id
+pf (Parse4 yo) EventDocumentEnd = Right $ Parse5 yo
+pf (Parse5 yo) EventStreamEnd = Left $ ParseComplete $ Right yo
+pf (ParseSeq n front) EventSequenceEnd = Right $ n $ Sequence $ front []
+pf (ParseSeq n front) e = pf (Parse3 helper) e where
+ helper yo = ParseSeq n $ front . (:) yo
+pf (ParseMap n front) EventMappingEnd = Right $ n $ Mapping $ front []
+pf (ParseMap n front) e = pf (Parse3 helper) e where
+ helper (Scalar ys) = ParseMap' n front ys
+ helper _ = ParseException NonScalarKey
+pf (ParseMap' n front ys) e = pf (Parse3 helper) e where
+ helper yo = ParseMap n $ front . (:) (ys, yo)
+pf Parse2 EventStreamEnd =
+ Left $ ParseComplete $ Left YamlPrematureEventStreamEnd
+pf p e =
+ Left $ ParseComplete $ Left $ YamlOtherException
+ $ SomeException $ InvalidParseState p e
+
+data ParseException = InvalidParseState ParseState Event
+ | NonScalarKey
+ | IncompleteParse ParseState
+ deriving (Show, Typeable)
+instance Exception ParseException
+
+#if TEST
+mkScalar :: String -> YamlScalar
+mkScalar s = YamlScalar (cs s) StrTag Folded
+
+sample :: YamlObject
+sample = Sequence
+ [ Scalar $ mkScalar "foo"
+ , Mapping
+ [ (mkScalar "bar1", Scalar $ mkScalar "bar2")
+ ]
+ ]
+
+testSuite :: Test
+testSuite = testGroup "Data.Object.Yaml"
+ [ testCase "encode/decode" caseEncodeDecode
+ , testCase "encode/decode file" caseEncodeDecodeFile
+ ]
+
+caseEncodeDecode :: Assertion
+caseEncodeDecode = do
+ out <- decode $ encode sample
+ out @?= sample
+
+caseEncodeDecodeFile :: Assertion
+caseEncodeDecodeFile = do
+ let fp = "tmp.yaml"
+ join $ encodeFile fp sample
+ out <- join $ decodeFile fp
+ out @?= sample
+#endif
View
25 LICENSE
@@ -0,0 +1,25 @@
+The following license covers this documentation, and the source code, except
+where otherwise indicated.
+
+Copyright 2008, Michael Snoyman. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
43 data-object-yaml.cabal
@@ -0,0 +1,43 @@
+name: data-object-yaml
+version: 0.2.0
+license: BSD3
+license-file: LICENSE
+author: Michael Snoyman <michael@snoyman.com>
+maintainer: Michael Snoyman <michael@snoyman.com>
+synopsis: Serialize data to and from Yaml files
+description: Provides high level conversions based on the data-object package. Parsing and emitting is handled by the yaml package, which in turn uses the libyaml C library.
+category: Web
+stability: unstable
+cabal-version: >= 1.2
+build-type: Simple
+homepage: http://github.com/snoyberg/data-object-yaml
+
+flag buildtests
+ description: Build the executable to run unit tests
+ default: False
+
+library
+ build-depends: base >= 4 && < 5,
+ data-object >= 0.2.0 && < 0.3,
+ bytestring >= 0.9.1.4 && < 0.10,
+ text >= 0.5 && < 0.6,
+ convertible-text >= 0.2.0 && < 0.3,
+ attempt >= 0.2.0 && < 0.3,
+ failure >= 0.0.0 && < 0.1,
+ yaml >= 0.2.0 && < 0.3
+ exposed-modules: Data.Object.Yaml
+ ghc-options: -Wall
+
+executable runtests
+ if flag(buildtests)
+ Buildable: True
+ cpp-options: -DTEST
+ build-depends: test-framework,
+ test-framework-quickcheck,
+ test-framework-hunit,
+ HUnit,
+ QuickCheck >= 1 && < 2
+ else
+ Buildable: False
+ ghc-options: -Wall
+ main-is: runtests.hs
View
8 runtests.hs
@@ -0,0 +1,8 @@
+import Test.Framework (defaultMain)
+
+import qualified Data.Object.Yaml
+
+main :: IO ()
+main = defaultMain
+ [ Data.Object.Yaml.testSuite
+ ]
Please sign in to comment.
Something went wrong with that request. Please try again.