Skip to content

Added a nix flake for cddl-codegen - #243

Closed
wanderer wants to merge 259 commits into
dcSpark:masterfrom
wanderer:master
Closed

Added a nix flake for cddl-codegen#243
wanderer wants to merge 259 commits into
dcSpark:masterfrom
wanderer:master

Conversation

@wanderer

@wanderer wanderer commented Mar 5, 2025

Copy link
Copy Markdown

No description provided.

rooooooooob and others added 30 commits April 6, 2020 22:10
This allows us to serialize properly these types, as we can't specialize
Vec to cbor_event's Serialize trait as they're in separate external
crates.
This way we can do address.new_address0([], []) instead of
address.new_address0(bytes.new([]), bytes.new([])), etc.
Can't pass generic types (ie no Vec<T>, Array<T>) through wasm boundary
so we need to generate an array-like type for each type that is exposed
as a wasm type.

This is similar to the approach in js-chain-libs, ie PublicKey ->
PublicKeys.
All static structs now live in `static/prelude.rs`.

Tags are now detected and a wrapper type is implemented, however
right now we can't construct it since it will need a refactor of the
type system first.
In order to better support tagged types, arrays, and conversion between
the wasm boundary instead of a simple String type there is now a
composable RustType enum to allow for more information at every stage
and better code generation as a result.

This allows us to not require the tag for paremters while stilly
serialization it for tagged types, as well as not requiring separate
types for vecs of primitive types.
Behavior is the same in this place, but this way we have more control
instead of relying on the to_string of the literal value.
It was this way before, but was accidentally changed in the prior
refactor commit after updating the dependency.
Use case: `address` in `shelley.cddl`

For groups are not defined in terms of array or map representation yet
we can still use them inside of other structures. If they are array then
their elements are inserted into the wrapping array ie:
x = (1, 2) y = [3, x, x, 4] should be [3, 1, 2, 1, 2, 4]
not [3, [1, 2], [1, 2], 4]
Also made group members always point to external types and never to the
group module types.
This is for 2 reasons:
1) All the binary types we have should probably have their own structs
   so we can have a better type system + add in specific code later
   on to them like verifying structure + helpers.

2) We can't expose Vec<Vec<u8>> (or any nested vec) to wasm using
   wasm_bindgen but we can generate for example Scripts from
   `script = bytes` and then using `[script]` somewhere.
Updating the cddl lib allows correct parsing of:
`withdrawals = { * [credential] => coin }`

and now that it parses correctly, we had to support using
generated types as map keys, so they all now have comparison
derives generated automatically.

Also root table constructions were broken so those were fixed too.
1) Properly support optional fields for both arrays/maps
2) Only generate array or map related functions when necessary
3) Store all groups inside `groups.rs` instead of in a module
   in `lib.rs`
Types become CamelCase and fields become snake_case and also handles
cddl weirdness like allowing -, @, $ (sockets).
dependabot Bot and others added 25 commits August 15, 2023 00:02
Bumps [syn](https://github.com/dtolnay/syn) from 2.0.12 to 2.0.16.
- [Release notes](https://github.com/dtolnay/syn/releases)
- [Commits](dtolnay/syn@2.0.12...2.0.16)

---
updated-dependencies:
- dependency-name: syn
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Bumps [clap](https://github.com/clap-rs/clap) from 4.2.4 to 4.3.12.
- [Release notes](https://github.com/clap-rs/clap/releases)
- [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md)
- [Commits](clap-rs/clap@v4.2.4...v4.3.12)

---
updated-dependencies:
- dependency-name: clap
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Bumps [quote](https://github.com/dtolnay/quote) from 1.0.26 to 1.0.31.
- [Release notes](https://github.com/dtolnay/quote/releases)
- [Commits](dtolnay/quote@1.0.26...1.0.31)

---
updated-dependencies:
- dependency-name: quote
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
* Docs: Integrating with other cddl-codegen gen'd libs

New section with tips for generating a library that will depend on
another cddl-codegen'd library e.g. CML.

Also fixes minor isues in other parts of the docs.

* avoid exporting static traits when common dir is overridden

* don't export mod decls with common-import-override

* fix for common export overrides from wasm
json-gen crate now usable as a library for use from other dependent
libraries' json-gen crates.
* draft status of ranges

* finished ranges. fixes for rust update, fixed test cases

* Fixing many edge cases for uint/nint/int ranges

now tests for both preserve-encodings and not with all 3 of those
as well as bounds that go across both
* @used_as_key dsl

Allow marking a type as a key to auto-derive traits e.g. for utils code

Fixes dcSpark#190

* @used_as_key test cases + update docs + recursive tagging + work for enums
* Fix @name not working on single elem group choices

Fixes dcSpark#211
Fixes dcSpark#153

* Add use of @name into test cases (tests compiling)
* fix typos

* fix typo

* fix typo

* fix typos
* Full range check in rest of API

Check ranges and error on incorrect ones in constructors and setters in
all spots.

Additional non-deserialization test checks to check the above.

Migrate few remaining usage of `JsValue` for WASM errors away to `JsError`
to be consistent with the rest of the generated code.

* fixed clippy warnings (did cargo update? doesn't trigger locally and this is not recently changed code)
* Enum length-check fixes + Enum optional field support

Fixes dcSpark#175

Now properly checks all lengths for all variants to ensure that
overlapping types parse the correct variant instead of prematurely
thinking it's a subset of one.

Also fixes having CBORReadLen contributions from previous variants that
tried to parse from contributing to later variant parses (possibly
causing issues if it meant it already hit the limit).

Includes support for optional fields within enums that get inlined.

Tests for both cases.

* preserve-encodings tests for overlapping_inlined + enum_opt_embed_fields
Specifically tests for support for dcSpark#121 for plain groups.

For multi arrays covered by dcSpark#120
For single ones covered by dcSpark#210

We are keeping dcSpark#121 open for now as the case for single non-plain-groups
e.g. `[uint]` is not covered. We've yet to see this used anywhere in
Cardano so it's low priority to fix.
Wrapper JSON Overhaul

Wrapper newtypes will generate custom implementations that will defer to
the inner type's JSON traits instead of deriving to get them.
This gives us much nicer JSON implementations allowing directly `T`
instead of `{ inner: T }`.

Bytes newtypes will have a specialization that serializes to hex
bytestring instead of the `[number]` array that would otherwise be used.

Introduces `@custom_json` comment DSL for newtypes that tell
cddl-codegen to avoid generating/deriving any JSON traits under the
assumption that some custom trait impls will be provided post-generation
by the user.
Extern generics

Allows types defined by `_CDDL_CODEGEN_EXTERN_TYPE_` to be used with
generic arguments.

Generics will be used directly on the rust side, but due to wasm-bindgen
restrictions must be specified for each concrete type like with current
generic support.
* Better error checks/messages for file missing related errors to help
  debugging as the error was incredibly opaque as to where it
originated.

* Imports correctly from the common import override in `serialization.rs`
files instead of relying on the `import super::*;` which only works when
it's not overrided.
* @custom_serialize / @custom_deserialize DSL

e.g. `; @custom_serialize write_hex_bytes`

For specifying externally-provided functions for arbitrary
encodings/cbor details for (de)serialization.

Allowed at both the type-level (affecting everywhere it's used) or at
the field-level (overrides type-level if present).

Example use-case: CML's PlutusData's Bytes (and BigInt)  variant doesn't use arbitrary
CBOR bytes strings but instead follows a specific chunking format. We
used to hand code this but now we can just put this in the DSL.

This is particularily useful for people generating plutus-datum-based
CDDLs. It could also be used to allow for utf8 text (rust API) to be
(de)serialized to bytes to be encodable as a datum.

TODO:
[ ] tests for preserve-encodings
[ ] tests for tagged/otherwise extra encoding details over top of this

* preserve-encodings=true tests

* docs + more test cases + misc fixes

* clarify where the custom_serialize string comes from
* Fix issue with overlapping inlined basic embedded enums

Caused when the inlining would made cddl-codegen think that the types
were not overlapping since it was looking at the stored type not the
actual starting cbor type (e.g. when it was a fixed value).

This would cause a problem as the type matching introduced in dcSpark#199
but only in very specific cases with basic groups starting with fixed
values.

* Fix dcSpark#229

Possibly needs more tests covering combinations with non-basic groups
mixed with basic, tagged basic groups and optional fields (should work
though)

* Fix dcSpark#230 dcSpark#231 dcSpark#232
Any inputs in a `/_CDDL_CODEGEN_EXTERN_DEPS_DIR_/` directory at the root
of the `--input` will now be treated as existing in an external crate.

All types will be ignored for exporting but will still be used in the
intermediate steps to determine what exists, what CBOR types they are,
etc.

This saves users from having to manually replace all `crate::foo::etc` and be
able to directly have it use `foo::etc` when they put their `foo` folder
in the aforementioned external deps folder.

This is very useful e.g. for CML's multi-era crate which refers to types
from `cml-chain`, etc. Before this we needed to manually edit imports,
manually remove module declarations, and manually delete those
files/folders corresponding to the external deps.
* @newtype on array/map wrappers

Previously this only worked on primitive wrappers.

Now you can do e.g. `foo = [* uint] ; @newtype` which was ignored
before.

* cargo fmt

* tests added
This allows us to specify doc-comments for auto-generated types to avoid
users having to manually put in comments afterwards which would be
overwritten every re-gen.

There is support for type-level (after the definition), field-level and
variant-level (for group/type-choices).
Before this was only on the variants which is useless for WASM (it's
wrapped around).
* Handle runs of uppercase letters for name generation

* Check casing in test

* Inline more examples in code comments

* Fix clippy error

* Add missing WASM_BINDGEN_WEAKREF in package

* Fixes for handling other cases

e.g. `shelley_ma`

---------

Co-authored-by: rooooooooob <rooooooooob@users.noreply.github.com>
Co-authored-by: rooooooooob <raftias@gmail.com>
Now you can optionally specify a getter e.g. `foo = uint ; @newtype
custom_getter`.

Previously `@newtype` would always generate one named `get()` but this
isn't always very helpful. Now you can explicitly name it, and if it's
empty, no getter will be made, in which case users can choose to expose
it however they want in their own `utils.rs` files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants