covenant
Copyright(C) MLabs 2025
LicenseApache 2.0
Maintainerkoz@mlabs.city, sean@mlabs.city
Safe HaskellNone
LanguageHaskell2010

Covenant.JSON

Description

JSON serialization and deserialization utilities for the ASG.

Note on Sum Type Encoding:

Unless otherwise noted, a Haskell sum type like:

data Foo = Bar | Baz Int

Is encoded to JSON using {tag: <CTOR NAME>, fields: [<Arg1>, <Arg2>, <ArgN>]}

This is used for all Haskell sum types which do not have LabelOptic instnaces. For those with field names given by such instances, the fields part of the encoded sum is not an array of arguments, but instead a JSON object, with fields whose names correspond to the label optics. Comments make it clear which types are encoded in which way.

Since: 1.3.0

Synopsis

Serialization

data Version Source #

Represents a Covenant version. This is currently just a tag, but may be used in the future to enforce compatibility.

Since: 1.3.0

Constructors

Version 

Fields

Instances

Instances details
Show Version Source #

Since: 1.3.0

Instance details

Defined in Covenant.JSON

Eq Version Source #

Since: 1.3.0

Instance details

Defined in Covenant.JSON

Methods

(==) :: Version -> Version -> Bool #

(/=) :: Version -> Version -> Bool #

Ord Version Source #

Since: 1.3.0

Instance details

Defined in Covenant.JSON

data SerializeErr Source #

The errors that can arise from compileAndSerialize not stemming from IO.

Since: 1.3.0

Constructors

DatatypeConversionFailure String

A datatype was specified in a way that isn't valid.

ASGCompilationFailure CovenantError

The supplied ASG failed to compile.

Instances

Instances details
Show SerializeErr Source # 
Instance details

Defined in Covenant.JSON

Eq SerializeErr Source # 
Instance details

Defined in Covenant.JSON

mkDatatypeInfos :: [DataDeclaration AbstractTy] -> Either String (Map TyName (DatatypeInfo AbstractTy)) Source #

Given a collection of datatype declarations, convert them to DatatypeInfos by generating their base functor and Boehm-Berrarducci encodings. Then add to these all the base functors for the built-in types.

Since: 1.3.0

compileAndSerialize :: FilePath -> [DataDeclaration AbstractTy] -> ASGBuilder a -> Version -> ExceptT SerializeErr IO () Source #

Given a FilePath to write output to, a collection of data declarations, an ASGBuilder and a version tag, compile the ASG, then write it to the given file path in its JSON serialized form, together with the data types.

Since: 1.3.0

Deserialization

data DeserializeErr Source #

The errors that can arise from deserializeAndValidate not stemming from IO.

Since: 1.3.0

Constructors

JSONParseFailure String

The serial form's JSON was not valid. This means that the given file cannot be an ASG.

ASGValidationFail CovenantError

The deserialized JSON corresponds to an ASG, but not a valid one.

Instances

Instances details
Show DeserializeErr Source # 
Instance details

Defined in Covenant.JSON

Eq DeserializeErr Source # 
Instance details

Defined in Covenant.JSON

deserializeAndValidate :: FilePath -> ExceptT DeserializeErr IO ASG Source #

Given a FilePath to a serialized ASG, decode it if possible.

Since: 1.3.0

deserializeAndValidate_ :: FilePath -> IO ASG Source #

Like deserializeAndValidate but runs directly in IO.

Note

This is mostly designed for use in tests, as it has no ability to 'trap' validation or deserialization errors. You most likely want deserializeAndValidate.

Since: 1.3.0