Z-YAML 0.1.0.0 → 0.2.0.0
raw patch · 4 files changed
+67/−73 lines, 4 filesdep ~Z-Datadep ~Z-IOPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: Z-Data, Z-IO
API changes (from Hackage documentation)
- Z.Data.YAML.FFI: instance Z.Data.Text.ShowT.ShowT Z.Data.YAML.FFI.Event
- Z.Data.YAML.FFI: instance Z.Data.Text.ShowT.ShowT Z.Data.YAML.FFI.Mark
- Z.Data.YAML.FFI: instance Z.Data.Text.ShowT.ShowT Z.Data.YAML.FFI.MarkedEvent
- Z.Data.YAML.FFI: instance Z.Data.Text.ShowT.ShowT Z.Data.YAML.FFI.Tag
+ Z.Data.YAML.FFI: instance Z.Data.Text.Print.Print Z.Data.YAML.FFI.Event
+ Z.Data.YAML.FFI: instance Z.Data.Text.Print.Print Z.Data.YAML.FFI.Mark
+ Z.Data.YAML.FFI: instance Z.Data.Text.Print.Print Z.Data.YAML.FFI.MarkedEvent
+ Z.Data.YAML.FFI: instance Z.Data.Text.Print.Print Z.Data.YAML.FFI.Tag
+ Z.Data.YAML.FFI: pattern Explicit :: TagRender
+ Z.Data.YAML.FFI: pattern Implicit :: TagRender
+ Z.Data.YAML.FFI: type TagRender = CInt
- Z.Data.YAML: NonStringKey :: Mark -> Mark -> Text -> YAMLParseError
+ Z.Data.YAML: NonStringKey :: MarkedEvent -> YAMLParseError
- Z.Data.YAML: NonStringKeyAlias :: Mark -> Mark -> Anchor -> YAMLParseError
+ Z.Data.YAML: NonStringKeyAlias :: MarkedEvent -> YAMLParseError
- Z.Data.YAML: UnknownAlias :: Mark -> Mark -> Anchor -> YAMLParseError
+ Z.Data.YAML: UnknownAlias :: MarkedEvent -> YAMLParseError
- Z.Data.YAML: decode :: FromValue a => Bytes -> Either YAMLParseException a
+ Z.Data.YAML: decode :: HasCallStack => FromValue a => Bytes -> IO a
- Z.Data.YAML: decodeValue :: Bytes -> Either YAMLParseException Value
+ Z.Data.YAML: decodeValue :: HasCallStack => Bytes -> IO Value
- Z.Data.YAML: encode :: (HasCallStack, ToValue a) => YAMLFormatOpts -> a -> Text
+ Z.Data.YAML: encode :: (HasCallStack, ToValue a) => YAMLFormatOpts -> a -> IO Text
- Z.Data.YAML: encodeValue :: HasCallStack => YAMLFormatOpts -> Value -> Text
+ Z.Data.YAML: encodeValue :: HasCallStack => YAMLFormatOpts -> Value -> IO Text
Files
- Z-YAML.cabal +3/−3
- Z/Data/YAML.hs +28/−30
- Z/Data/YAML/FFI.hsc +11/−10
- cbits/hs_yaml.c +25/−30
Z-YAML.cabal view
@@ -1,6 +1,6 @@ cabal-version: >=1.10 name: Z-YAML-version: 0.1.0.0+version: 0.2.0.0 synopsis: YAML tools description: YAML reading & writing tools for Z project. license: BSD3@@ -28,8 +28,8 @@ , scientific == 0.3.* , transformers == 0.5.* , primitive >= 0.5 && < 0.8- , Z-Data == 0.2.*- , Z-IO == 0.2.*+ , Z-Data == 0.3.*+ , Z-IO == 0.3.* include-dirs: third_party/libyaml/src
Z/Data/YAML.hs view
@@ -11,7 +11,7 @@ The design choice to make things as simple as possible since YAML is a complex format, there're some limitations using this approach: * Does not support complex keys.-* Dose not support multiple doucments in one file.+* Does not support multiple doucments in one file. @ {-# LANGUAGE DeriveGeneric, DeriveAnyClass, DerivingStrategies, TypeApplication #-}@@ -28,7 +28,7 @@ deriving (Show, Generic) deriving anyclass (YAML.FromValue, YAML.ToValue) -> YAML.decode @[Person] "- name: Erik Weisz\n age: 52\n magic: True\n"+> YAML.decode @[Person] "- name: Erik Weisz\\n age: 52\\n magic: True\\n" > Right [Person {name = "Erik Weisz", age = 52, magic = True}] @ @@ -36,7 +36,7 @@ module Z.Data.YAML- ( -- * decode and encode using YAML+ ( -- * Decode and encode using YAML decodeFromFile , decodeValueFromFile , decode@@ -45,14 +45,14 @@ , encodeValueToFile , encode , encodeValue- , YAMLParseError(..) , YAMLParseException(..)- -- * streaming parser and builder+ , YAMLParseError(..)+ -- * Streaming parser and builder , parseSingleDoucment , parseAllDocuments , buildSingleDocument , buildValue- -- * re-exports+ -- * Re-Exports , FromValue(..) , ToValue(..) , Value(..)@@ -66,6 +66,7 @@ import qualified Data.HashMap.Strict as HM import qualified Data.HashSet as HS import qualified Data.Scientific as Sci+import Z.Data.ASCII import qualified Z.Data.Parser as P import qualified Z.Data.Vector as V import qualified Z.Data.Text as T@@ -77,7 +78,6 @@ import Z.Data.CBytes (CBytes) import Z.Data.YAML.FFI import Z.IO-import System.IO.Unsafe -- | Decode a 'FromValue' instance from file. decodeFromFile :: (HasCallStack, FromValue a) => CBytes -> IO a@@ -91,15 +91,15 @@ decodeValueFromFile p = withResource (initFileParser p) parseSingleDoucment -- | Decode a 'FromValue' instance from bytes.-decode :: FromValue a => V.Bytes -> Either YAMLParseException a-decode bs = unsafePerformIO . try . withResource (initParser bs) $ \ src -> do+decode :: HasCallStack => FromValue a => V.Bytes -> IO a+decode bs = withResource (initParser bs) $ \ src -> do r <- convert' <$> parseSingleDoucment src case r of Left e -> throwIO (YAMLConvertException e callStack) Right v -> return v -- | Decode a 'Value' from bytes.-decodeValue :: V.Bytes -> Either YAMLParseException Value-decodeValue bs = unsafePerformIO . try $ withResource (initParser bs) parseSingleDoucment+decodeValue :: HasCallStack => V.Bytes -> IO Value+decodeValue bs = withResource (initParser bs) parseSingleDoucment -- | Encode a 'ToValue' instance to file. encodeToFile :: (HasCallStack, ToValue a) => YAMLFormatOpts -> CBytes -> a -> IO ()@@ -112,24 +112,24 @@ buildSingleDocument sink v -- | Encode a 'ToValue' instance as UTF8 text.-encode :: (HasCallStack, ToValue a) => YAMLFormatOpts -> a -> T.Text-encode opts x = unsafePerformIO . withResource (initEmitter opts) $ \ (p, sink) -> do+encode :: (HasCallStack, ToValue a) => YAMLFormatOpts -> a -> IO T.Text+encode opts x = withResource (initEmitter opts) $ \ (p, sink) -> do buildSingleDocument sink (toValue x) getEmitterResult p -- | Encode a 'Value' as UTF8 text.-encodeValue :: HasCallStack => YAMLFormatOpts -> Value -> T.Text-encodeValue opts v = unsafePerformIO . withResource (initEmitter opts) $ \ (p, sink) -> do+encodeValue :: HasCallStack => YAMLFormatOpts -> Value -> IO T.Text+encodeValue opts v = withResource (initEmitter opts) $ \ (p, sink) -> do buildSingleDocument sink v getEmitterResult p -------------------------------------------------------------------------------- data YAMLParseError- = UnknownAlias Mark Mark Anchor+ = UnknownAlias MarkedEvent | UnexpectedEvent MarkedEvent- | NonStringKey Mark Mark T.Text- | NonStringKeyAlias Mark Mark Anchor+ | NonStringKey MarkedEvent+ | NonStringKeyAlias MarkedEvent | UnexpectedEventEnd deriving (Show, Eq) @@ -194,14 +194,14 @@ (_, mref) <- ask liftIO $ modifyIORef' mref (HM.insert key value) -lookupAlias :: Mark -> Mark -> T.Text -> ParserIO Value-lookupAlias startMark endMark key = do+lookupAlias :: MarkedEvent -> T.Text -> ParserIO Value+lookupAlias me key = do (_, mref) <- ask liftIO $ do m <- readIORef mref case HM.lookup key m of Just v -> return v- _ -> throwIO (UnknownAlias startMark endMark key)+ _ -> throwIO (UnknownAlias me) textToValue :: ScalarStyle -> Tag -> T.Text -> Value textToValue SingleQuoted _ t = String t@@ -222,8 +222,8 @@ <|> (fromInteger <$> (P.bytes "0o" *> octal)) <|> P.scientific - octal = V.foldl' step 0 <$> P.takeWhile1 (\ w -> w >= B.ZERO && w < B.ZERO+8)- step a c = (a `unsafeShiftL` 3) .|. fromIntegral (c - B.ZERO)+ octal = V.foldl' step 0 <$> P.takeWhile1 (\ w -> w >= DIGIT_0 && w < DIGIT_0+8)+ step a c = (a `unsafeShiftL` 3) .|. fromIntegral (c - DIGIT_0) parseValue :: MarkedEvent -> ParserIO Value parseValue me@(MarkedEvent e startMark endMark) =@@ -240,7 +240,7 @@ !v <- parseMapping defineAnchor anchor v return v- EventAlias anchor -> lookupAlias startMark endMark anchor+ EventAlias anchor -> lookupAlias me anchor _ -> throwParserIO (UnexpectedEvent me) parseSequence :: ParserIO Value@@ -268,13 +268,13 @@ k@(String k') -> do defineAnchor anchor k return k'- _ -> throwParserIO (NonStringKey startMark endMark v)+ _ -> throwParserIO (NonStringKey me) EventAlias anchor -> do- m <- lookupAlias startMark endMark anchor+ m <- lookupAlias me anchor case m of String k -> return k- _ -> throwParserIO (NonStringKeyAlias startMark endMark anchor)+ _ -> throwParserIO (NonStringKeyAlias me) e -> throwParserIO (UnexpectedEvent me) value <- parseValue =<< pullEvent@@ -298,7 +298,6 @@ -- | Write a value as a YAML document stream. ----- @since 0.11.2.0 buildSingleDocument :: HasCallStack => Sink Event -> Value -> IO () buildSingleDocument sink v = do push sink EventStreamStart@@ -307,9 +306,8 @@ push sink EventDocumentEnd void $ push sink EventStreamEnd --- | Write a value as a list of 'Event's(without document start\/end, stream start\/end).+-- | Write a value as a stream of 'Event's(without document start\/end, stream start\/end). ----- @since 0.11.2.0 buildValue :: HasCallStack => Sink Event -> Value -> IO () buildValue sink (Array vs) = do push sink (EventSequenceStart "" NoTag AnySequence)
Z/Data/YAML/FFI.hsc view
@@ -49,6 +49,9 @@ , pattern AnyMapping , pattern BlockMapping , pattern FlowMapping + , TagRender+ , pattern Explicit + , pattern Implicit -- * Exception type , LibYAMLException (..) ) where@@ -71,7 +74,7 @@ import qualified Z.IO.FileSystem as FS import qualified Z.Data.Vector as V import qualified Z.Data.Text.Base as T-import Z.Data.Text.ShowT (ShowT)+import Z.Data.Text.Print (Print) import Z.Data.JSON (EncodeJSON, FromValue, ToValue) #include "yaml.h"@@ -90,7 +93,7 @@ | EventMappingStart !Anchor !Tag !MappingStyle | EventMappingEnd deriving (Show, Ord, Eq, Generic)- deriving anyclass (ShowT, EncodeJSON, FromValue, ToValue)+ deriving anyclass (Print, EncodeJSON, FromValue, ToValue) data MarkedEvent = MarkedEvent { markedEvent :: !Event@@ -98,7 +101,7 @@ , endMark :: !Mark } deriving (Show, Ord, Eq, Generic)- deriving anyclass (ShowT, EncodeJSON, FromValue, ToValue)+ deriving anyclass (Print, EncodeJSON, FromValue, ToValue) -- | The pointer position data Mark = Mark @@ -107,7 +110,7 @@ , yamlColumn :: {-# UNPACK #-} !Int } deriving (Show, Ord, Eq, Generic)- deriving anyclass (ShowT, EncodeJSON, FromValue, ToValue)+ deriving anyclass (Print, EncodeJSON, FromValue, ToValue) -- | Style for scalars - e.g. quoted / folded -- @@ -148,7 +151,7 @@ | UriTag T.Text | NoTag deriving (Show, Ord, Eq, Generic)- deriving anyclass (ShowT, EncodeJSON, FromValue, ToValue)+ deriving anyclass (Print, EncodeJSON, FromValue, ToValue) tagToCBytes :: Tag -> CB.CBytes tagToCBytes StrTag = "tag:yaml.org,2002:str"@@ -226,7 +229,6 @@ fclose file) return bio - -- | Parse a single event from YAML parser. peekParserEvent :: HasCallStack => Ptr ParserStruct -> IO (Maybe MarkedEvent) peekParserEvent parser = do@@ -494,7 +496,6 @@ withTag tag = CB.withCBytesUnsafe (tagToCBytes tag) withAnchor anchor = CB.withCBytesUnsafe (CB.fromText anchor) - -- | Whether a tag should be rendered explicitly in the output or left -- implicit. --@@ -503,7 +504,7 @@ pattern Explicit = 0 pattern Implicit = 1 --- | A value for 'formatOptionsRenderTags' that renders no+-- | A value for 'yamlFormatRenderTags' that renders no -- collection tags but all scalar tags (unless suppressed with styles -- 'NoTag or 'PlainNoTag'). --@@ -513,13 +514,13 @@ renderScalarTags (EventMappingStart _ _ _) = Implicit renderScalarTags _ = Implicit --- | A value for 'formatOptionsRenderTags' that renders all+-- | A value for 'yamlFormatRenderTags' that renders all -- tags (except 'NoTag' tag and 'PlainNoTag' style). -- renderAllTags :: Event -> TagRender renderAllTags _ = Explicit --- | A value for 'formatOptionsRenderTags' that renders no+-- | A value for 'yamlFormatRenderTags' that renders no -- tags. -- renderNoTags :: Event -> TagRender
cbits/hs_yaml.c view
@@ -15,31 +15,31 @@ } typedef struct hs_yaml_buf_s {- unsigned char *buf;- size_t size;+ unsigned char *buf;+ size_t size; size_t used; } hs_yaml_buf_t; int buffer_append(void *ext, unsigned char *str, size_t size) {- hs_yaml_buf_t *b = ext;- int new_size, new_used;- unsigned char *tmp;+ hs_yaml_buf_t *b = ext;+ int new_size, new_used;+ unsigned char *tmp; - new_used = b->used + size;- for (new_size = b->size ? b->size : 120; new_size < new_used; new_size *= 2);+ new_used = b->used + size;+ for (new_size = b->size ? b->size : 120; new_size < new_used; new_size *= 2); - if (new_size != b->size) {- tmp = realloc(b->buf, new_size);- if (!tmp) return 0;- b->buf = tmp;- b->size = new_size;- }+ if (new_size != b->size) {+ tmp = realloc(b->buf, new_size);+ if (!tmp) return 0;+ b->buf = tmp;+ b->size = new_size;+ } - memcpy(b->buf + b->used, str, size);- b->used = new_used;+ memcpy(b->buf + b->used, str, size);+ b->used = new_used; - return 1;+ return 1; } yaml_emitter_t* hs_init_yaml_emitter(int canonical, int indent, int width)@@ -104,8 +104,8 @@ int hs_yaml_sequence_start_event_initialize(yaml_event_t *event,- const yaml_char_t *anchor, const yaml_char_t *tag, int implicit,- yaml_sequence_style_t style) {+ const yaml_char_t *anchor, const yaml_char_t *tag, int implicit,+ yaml_sequence_style_t style) { if (*anchor == 0) anchor = NULL; if (*tag == 0) tag = NULL;@@ -114,10 +114,10 @@ int hs_yaml_scalar_event_initialize(yaml_event_t *event,- const yaml_char_t *anchor, const yaml_char_t *tag,- const yaml_char_t *value, int off, int length, - int plain_implicit, int quoted_implicit,- yaml_scalar_style_t style){+ const yaml_char_t *anchor, const yaml_char_t *tag,+ const yaml_char_t *value, int off, int length, + int plain_implicit, int quoted_implicit,+ yaml_scalar_style_t style){ if (*anchor == 0) anchor = NULL; if (*tag == 0) tag = NULL;@@ -126,8 +126,8 @@ int hs_yaml_mapping_start_event_initialize(yaml_event_t *event,- const yaml_char_t *anchor, const yaml_char_t *tag, int implicit,- yaml_mapping_style_t style) {+ const yaml_char_t *anchor, const yaml_char_t *tag, int implicit,+ yaml_mapping_style_t style) { if (*anchor == 0) anchor = NULL; if (*tag == 0) tag = NULL;@@ -135,10 +135,5 @@ } int hs_yaml_document_start(yaml_event_t *e) {- return yaml_document_start_event_initialize- (e,- 0,- 0,- 0,- 1);+ return yaml_document_start_event_initialize(e, 0, 0, 0, 1); }