Skip to content

No std - #151

Closed
SimonIT wants to merge 276 commits into
dcSpark:masterfrom
ATACAMA-Project:no_std
Closed

No std#151
SimonIT wants to merge 276 commits into
dcSpark:masterfrom
ATACAMA-Project:no_std

Conversation

@SimonIT

@SimonIT SimonIT commented Mar 10, 2023

Copy link
Copy Markdown
Contributor

We want to use cddl-codegen to generate some projects for constrained devices. So no_std would be really useful for this.
Needs primetype/cbor_event#13

rooooooooob and others added 30 commits April 6, 2020 22:10
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).
Instead of defaulting to `index_n` for the `nth` field when there is no
specified name, we now try harder to generate field names using other
information like the type of the field so for example if we have:
`foo = [keyhash, scripthash, scripthash]`
we will generate something like:
```rust
struct Foo {
    keyhash: Keyhash,
    scripthash: Scripthash,
    scripthash2: Scripthash,
}
```
which also applies to constructors/setters or anywhere else the fields
would be referred to.

Array support is handled too so `bar = [0, [input]]` would be:
```rust
struct Bar {
    inputs: Inputs,
}
```
since the 0 isn't stored as we only need it for serialization
Generated code cleanup + Optional field setters
Now all field are represented directly inside of the main code,
so instead of both `lib.rs` having
```rust
struct Foo(groups::Foo);
```
and `groups.rs` having
```rust
struct Foo {
    x: u32,
    y: u32,
}
```
we now have only `lib.rs` having the group structure, but also having the
wasm exposure and other exposed functionality that the `lib.rs` had
before.

This simplifies in-library client code (wasm interface is unchanged)
as instead of `self.0.x.0.y.0` we can just write `self.x.y` and be much
more readable.

This existed before as plain groups and their usage used to be
code-generated in separate steps, as well as a desire to be generic for
both array vs map representations. This does not matter anymore though
as the two (group vs concrete representation) had already became coupled
(and rightfully so) along the way.
Instead of having to do `x.data` everywhere we can directly access
the tagged data as `x`.

This means we no longer store anything pertaining to tags anywhere
except the serializaiton code, making any use from within the generated
rust library much less ugly and more obvious, as well as reducing
overall code size. We also no longer need to generate those redundant
`UntaggedFoo` structs which are contained within the tagged `Foo`, as we
can directly generate `Foo` and store the tag information solely within
the serialization code without relying on having
`TaggedData<UntaggedFoo>` within `Foo`.
Remove concept of the groups module, simplifying generated code.
Group choices as map representation serialization support was added when the last refactor in PR dcSpark#2
Current issues:
* primitives in type choices cause issues with rust syntax
* repeated types within a choice (ie with differences in occurneces/size
  limits/ets) cause issues
* Isues with byte strings inside type choices
* Not directly related, but we don't support CDDL's null which is used
  in the motivating use-cases in shelley.cddl's proposed changes
variant names were causing issues resulting in rust code that would not
compile in specific situations.

Issues fixed:
* Having multiple choices with same base type:
  (ie `foo = bytes .size 4 / bytes .size 6`)
  would result in duplicate functions/variants,
  now we append 2, 3, etc for repeats
* Using bytes/bstr was broken entirely and would just treat it as an
  an identifier called bytes/bstr
* Using primitives (or the aforementioned bytes/bstr) would give invalid
  enum variants. Now they should be properly formatted.
* Using arrays in variants should give valid fields/variants/etc.
* Generated code should now be proper rust format for constructors
  and enum names/variant names, etc.
Now works correctly for serializing arrays, tagged types, and
primitives. Some refactoring was also done to improve how plain group
generation was done.
This is instead of generating unneccessary group choice variants that
simply wrap the single field.

The variant will also use the field name (if provided), or else type
name as the variant name, allowing a way to have control over variant
names by making all group choices a separate group and then putting them
as an identifier in the choice.
Extends RustType to take on fixed-value ie a fixed u32, text, or null.
This allows us to have null as type choices or as field types while
refactoring the existing code for fixed uint values to make it easier to
extend.

The only issue we have is that now `RustType::for_member()` can fail if
it is `RustType::Fixed``. This should probably be refactored to return
an error type or something, or split RustType off into something like:
```
enum FieldType {
    RustType(RustType),
    Fixed(FixedValue),
}
```
Merged with old single-table-field struct generation, except now we
generate automatic ones rom inlining as well.

Also how arrays are serialized is done by just calling serialize()
rather than duplicating the serializing logic in two places (Doh!).
needed for tx metdata

also moved check for plain groups not having choices to only when we're
trying to generate new functions for them which happens when they're in
an enum or inlined into another group choice.

Before there was a problem with `address` as it had multiple choices but
was never used here so it made no sense to have the check for all plain
groups.
Also tested tags within type choices
SebastienGllmt and others added 28 commits July 13, 2023 01:41
* Nested directory/mod support for multifile inputs

CDDL input directories can now contain folders which will correspond to
nested modules in the generated code. e.g. inputs/foo/bar.cddl will
create a bar module inside of the foo module.

You can also name files inside of the folders as mod.cddl to have them
be the root module instead of a submodule e.g. inputs/foo/mod.cddl would
be the same as inputs/foo.cddl


* common import override

useful for projects like CML that, once generated, have common imports
like error, serialization, etc in a certain location which might be used
by other crates/modules using cddl-codegen

this lets us avoid a lot of imports/module changes by hand in this case
* Document _CDDL_CODEGEN_RAW_BYTES_TYPE_ + trait export

The `RawBytesEncoding` trait will now be exported in `serialization.rs`
when it's used in the CDDL input.

Documented `_CDDL_CODEGEN_RAW_BYTES_TYPE_` usage in the README too.

* README.md update for typo + clarification

* fix tests

* comment in docs folder instead
Together these fix dcSpark#194 and dcSpark#145

* NoVariantMatchedWithCauses deser error variant

Addresses dcSpark#194

This helps avoid any type/group choice from eating any errors on
variants as instead of receiving a NoVariantMatched you will receive one
with errors as to why each variant failed.

This drastically helps debugging and also works for nested choices as
well.

* Avoid try-all on enums with non-overlapping CBOR

Fixes dcSpark#145

When all variants have non-overlapping first CBOR type we can avoid
brute-force trying all possible variants for type/group choices and
instead branch on raw.cbor_type() to only try the variant that makes
sense.
# Extern macro option for WASM boilerplate

* --wasm-cbor-json-api-macro

Override for the CBOR to/from bytes API + to/from JSON

* --wasm-conversions-macro

Override for the conversion traits between wasm/rust types. (e.g. From
between them + AsRef for the rust one)

This will help compatibility for CML and make changing any details of
these only require changing those macros instead of needing to
regenerate the library again.

This also significantly decreases the amount of repetitive boilerplate
junk making reading the files clearer.
…#207)

* Fix for fixed value enum variants without preserve-encodings

* cargo fmt
* Deserialize for array groups with optional fields

Fixes dcSpark#203

Fixes dcSpark#154 as this issue popped up while making sure optional fields
were very well supported.

* Optional array fields with preserve-encodings=true
Bumps [once_cell](https://github.com/matklad/once_cell) from 1.17.1 to 1.18.0.
- [Changelog](https://github.com/matklad/once_cell/blob/master/CHANGELOG.md)
- [Commits](matklad/once_cell@v1.17.1...v1.18.0)

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

Signed-off-by: dependabot[bot] <support@github.com>
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.
# Conflicts:
#	Cargo.toml
#	src/generation.rs
#	static/error.rs
#	tests/core/tests.rs
#	tests/deser_test
#	tests/external_rust_raw_bytes_def
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.

9 participants