Giter Site home page Giter Site logo

teamplayer3 / postcard-bindgen Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 2.0 255 KB

A crate which generates bindings for the postcard binary format for other languages than Rust.

License: Apache License 2.0

Rust 98.62% JavaScript 0.93% TypeScript 0.45%

postcard-bindgen's People

Contributors

brychanrobot avatar kylecharters avatar renovate[bot] avatar teamplayer3 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

postcard-bindgen's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

cargo
postcard-bindgen-core/Cargo.toml
  • genco 0.17.8
  • convert_case 0.6.0
  • heapless 0.8.0
postcard-bindgen-derive/Cargo.toml
  • quote 1.0.35
  • serde_derive_internals 0.29
  • syn 2.0.50
  • serde 1
  • proc-macro2 1.0
  • genco 0.17.8
  • convert_case 0.6.0
  • regex 1.10.3
  • regex-macro 0.2.0
postcard-bindgen/Cargo.toml
  • postcard 1.0.8
  • serde 1.0
github-actions
.github/workflows/rust.yml
  • actions/checkout v4
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • actions/checkout v4
  • Swatinem/rust-cache v2
npm
postcard-bindgen/test-bindings-proj/package.json
  • tslint ^6.1.3
  • typescript ^5.0.0
  • @types/node ^20.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Encoding floats

I've noticed serialize format checking for a float field runs !Number.isInteger() on the value, but that that doesn't quite work correctly. Numbers like 1.0 will return true, causing encoding to fail. I wonder if that check should be removed entirely and just leave the check for the number type. It isn't really a problem to have an integer in a float field. The problem would be if we tried to pass a float in an integer field.

How to handle arrays?

First, nice project, and thanks for producing it.

Would the following be easy to support? Or is there another way to go about it?

#[derive(Serialize, PostcardBindings)]
struct Test {
    name: u8,
    other: u16,
    values: [u32; 32],
}
10 |     values: [u32; 32],
   |             ^^^^^^^^^ the trait `GenJsBinding` is not implemented for `[u32; 32]

Bindings for heapless::Vec

heapless::Vec support appears to be included when you enable the heapless feature, but I'm still having trouble with it.

impl<T: GenJsBinding, const N: usize> GenJsBinding for heapless::Vec<T, N> {
    fn get_type() -> JsType {
        JsType::Array(ArrayMeta {
            items_type: Box::new(T::get_type()),
        })
    }
}

When I try to compile generate_bindings!(heapless::Vec<EncoderReading, 10>) I get

error[E0277]: the trait bound `heapless::Vec<EncoderReading, 10>: JsBindings` is not satisfied
  --> js-bindings/src/main.rs:10:28
   |
10 |         generate_bindings!(heapless::Vec<common_structs::EncoderReading, 10>),
   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `JsBindings` is not implemented for `heapless::Vec<EncoderReading, 10>`
   |
   = help: the trait `JsBindings` is implemented for `EncoderReading`

Is there something I'm doing wrong here?

adjust js generated enum style

Use the first enum style like in this wasm proposal.

type MyEnum =
  | { tag: 'A' }
  | { tag: 'B', value: number }
  | { tag: 'C', value: [number, number] }
  | { tag: 'D', value: { x: number, y: number }
  ;

It allows writing switch cases:

switch (enum.tag) {
    case 'A': ...; break;
    case 'B': .... 
}

What to do with UnitTypes

UnitType in rust:

struct T;

Problem with typescript: microsoft/TypeScript#47486
-> empty interfaces match to any type -> mostly not allowed with eslint

Not working ts decl:

export type T = {}
declare function test_func(t: T): void

test_func([23]) // no error

Adjust struct naming

Use structure types as in rust structures.

NewType as in new type idiom.

Match to serde ast style

// unit struct     -> Style::Unit
struct A;

// new type struct -> Style::Newtype
struct B(u8);

// tuple struct    -> Style::Tuple
struct C(u8, u16);

// struct (c-like) -> Style::Struct
struct D {
    a: u8,
    b: u16,
}

// enum
enum E {
    A,            // unit struct                     -> unit variant     -> Style::Unit
    B(u8),        // new type struct/ tuple struct   -> new type variant -> Style::Newtype
    C(u8, u16),   // tuple struct                    -> tple variant     -> Style::Tuple
    D {           // struct (c-like)                 -> struct variant   -> Style::Struct
        a: u8,
        b: u16
    }
}

Line separator missing in js/ts output

Maybe isolated.

Tried with the basic Test example of the readme, js output is fine until the end of serializer class, then a few \n /;are missing.
*.d.ts file do not contains any line separator.

Any idea where it might come from?

// Previous code is fine
const serialize_FOO = (s, v) => {
    s.serialize_number(U8_BYTES, false, v.name);
    s.serialize_number(U16_BYTES, false, v.other)
} const deserialize_FOO = (d) => ({ name: d.deserialize_number(U8_BYTES, false), other: d.deserialize_number(U16_BYTES, false) }) const is_FOO = (v) => (typeof v === "object" && typeof v.name === "number" && typeof v.other === "number") module.exports.serialize = (type, value) => {
    if (!(typeof type === "string")) { throw "type must be a string" } const s = new Serializer() switch (type) {
        case "FOO": if (is_FOO(value)) { serialize_FOO(s, value) } else throw "value has wrong format";
            break
    } return s.finish()
} module.exports.deserialize = (type, bytes) => { if (!(typeof type === "string")) { throw "type must be a string" } const d = new Deserializer(bytes) switch (type) { case "FOO": return deserialize_FOO(d) } }

Everything works fine once linebreak are added manually.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.