Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: blueprint-serde crate #429

Merged
merged 10 commits into from
Nov 5, 2024
Merged

feat: blueprint-serde crate #429

merged 10 commits into from
Nov 5, 2024

Conversation

Serial-ATA
Copy link
Contributor

@Serial-ATA Serial-ATA commented Nov 1, 2024

This crates lets us convert to and from the Field type easily with serde's Serialize and Deserialize traits. This will allow for custom types in job parameters, as well as making it easier overall to convert between these types.

The crate adds two public functions:

  • to_field - Convert any Serialize type into a Field
  • from_field - Attempt to convert a Field into a DeserializeOwned type

Field::Struct example:

use gadget_blueprint_serde::{new_bounded_string, BoundedVec, Field};
use serde::Serialize;

#[derive(Serialize)]
struct Person {
    name: String,
    age: u8,
}

let person = Person {
    name: String::from("John"),
    age: 40,
};

let expected = Field::Struct(
    new_bounded_string("Person"),
    Box::new(BoundedVec(vec![
        (
            new_bounded_string("name"),
            Field::String(new_bounded_string("John")),
        ),
        (new_bounded_string("age"), Field::Uint8(40)),
    ])),
);

let field = gadget_blueprint_serde::to_field(person).unwrap();
assert_eq!(expected, field);

While mostly useful for Field::Struct, one can also do:

let age: u8 = gadget_blueprint_serde::from_field(Field::Uint8(40)).unwrap();

Or conversion for any primitive type, if they wanted.

@drewstone
Copy link
Contributor

Will this be compatible with no_std?

@Serial-ATA
Copy link
Contributor Author

Yeah, that's the plan. I haven't made it #![no_std] yet just in case, but I don't imagine it'll need std at all.

@drewstone
Copy link
Contributor

drewstone commented Nov 1, 2024

We could have it be both too with the std feature. Just trying to keep us in the flow of doing so when we can, I.e it is probably more performant std but if it supports no-std then eventually we can use it in WASM.

@Serial-ATA
Copy link
Contributor Author

I can add an std feature just in case. I can't imagine us needing it though, none of the dependencies nor any of the de/serialization logic need std.

@Serial-ATA Serial-ATA marked this pull request as ready for review November 4, 2024 16:43
This crates lets us convert to and from the `Field` type easily with `serde`'s `Serialize` and `Deserialize` traits.
This will allow for custom types in job parameters, as well as making it easier overall to convert between these types.

The crate adds two public functions:

* `to_field` - Convert any `Serialize` type into a `Field`
* `from_field` - Attempt to convert a `Field` into a `DeserializeOwned` type

While mostly useful for `Field::Struct`, one can also do:

```rust
let age: u8 = gadget_blueprint_serde::from_field(Field::Uint8(40)).unwrap();
```

Or conversion for any primitive type, if they wanted.
Copy link
Contributor

@shekohex shekohex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, good job!

@@ -0,0 +1,104 @@
#![cfg_attr(feature = "std", no_std)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some top-level crate docs could be useful, with some examples and how to use them.

@drewstone drewstone merged commit c6d9ffe into main Nov 5, 2024
13 checks passed
@drewstone drewstone deleted the serial/blueprint-serde branch November 5, 2024 07:41
@webb-spider webb-spider bot mentioned this pull request Nov 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Completed ✅
Development

Successfully merging this pull request may close these issues.

3 participants