diff --git a/bower.json b/bower.json index ce1fabc..ff58e0b 100644 --- a/bower.json +++ b/bower.json @@ -15,7 +15,8 @@ "purescript-functions": "^3.0.0", "purescript-foreign": "^4.0.0", "purescript-foreign-generic": "^4.1.0", - "purescript-unsafe-coerce": "^3.0.0" + "purescript-unsafe-coerce": "^3.0.0", + "purescript-argonaut-core": "^3.1.0" }, "devDependencies": { "purescript-console": "^3.0.0", diff --git a/src/Data/YAML/Foreign/Decode.purs b/src/Data/YAML/Foreign/Decode.purs index 9765b30..bbe6fa3 100644 --- a/src/Data/YAML/Foreign/Decode.purs +++ b/src/Data/YAML/Foreign/Decode.purs @@ -1,12 +1,14 @@ -module Data.YAML.Foreign.Decode (parseYAML, readYAMLGeneric) where +module Data.YAML.Foreign.Decode (parseYAML, readYAMLGeneric, parseYAMLToJson) where import Data.Foreign (F, Foreign, ForeignError(..), fail) -import Data.Foreign.Generic.Class (class GenericDecode) import Data.Foreign.Generic (genericDecode) +import Data.Foreign.Generic.Class (class GenericDecode) import Data.Foreign.Generic.Types (Options) import Data.Function.Uncurried (Fn3, runFn3) import Data.Generic.Rep (class Generic) -import Prelude ((>=>), (<<<), pure) +import Prelude ((>=>), (<<<), pure, (>>=)) +import Unsafe.Coerce (unsafeCoerce) +import Data.Argonaut.Core (Json) foreign import parseYAMLImpl :: forall r. Fn3 (String -> r) (Foreign -> r) String r @@ -14,6 +16,10 @@ foreign import parseYAMLImpl :: forall r. Fn3 (String -> r) (Foreign -> r) Strin parseYAML :: String -> F Foreign parseYAML yaml = runFn3 parseYAMLImpl (fail <<< JSONError) pure yaml +-- | Attempt to parse a YAML string, returning the result as Json +parseYAMLToJson :: String -> F Json +parseYAMLToJson yaml = parseYAML yaml >>= pure <<< unsafeCoerce + -- | Automatically generate a YAML parser for your data from a generic instance. readYAMLGeneric :: forall a rep. (Generic a rep) => (GenericDecode rep) => Options -> String -> F a readYAMLGeneric opts = parseYAML >=> genericDecode opts