Derive Macro rquickjs_macro::FromJs[][src]

#[derive(FromJs)]
{
    // Attributes available to this derive:
    #[quickjs]
}

A macro to derive FromJs for an arbitrary structured types

Supported attributes

Macro attributes

AttributeDescription
rename_all = "<rule>"Renames variants and fields by applying renaming rule ("lowercase", "PascalCase", "camelCase", "snake_case", "SCREAMING_SNAKE_CASE", "kebab-case")
bound = "T: Bound"Overrides type paremters bounds
tag, tag = "type"Turns enum representation to internally tagged
content, content = "data"With tag turns enum representation to adjacently tagged
untaggedTurns enum representation to untagged
crate = "rquickjs"Allows rename rquickjs crate

The default enum representation is externally tagged.

Variant attributes

AttributeDescription
rename = "new_name"Renames a variant
skipSkips this variant

If a enum representation is untagged the variants with discriminant will be represented as a numbers.

Field attributes

AttributeDescription
rename = "new_name"Renames a field
default, default = "path"Sets the default for a field
skipSkips this field

Examples

Unit struct

use rquickjs::FromJs;

#[derive(FromJs)]
struct MyUnit;

Tuple struct

#[derive(FromJs)]
struct MyTuple(i32, String);

Struct with fields

#[derive(FromJs)]
struct MyStruct {
    int: i32,
    text: String,
    #[quickjs(skip)]
    skipped: bool,
}

Struct with fields with default values

#[derive(FromJs)]
struct MyStruct {
    #[quickjs(default)]
    int: i32,
    #[quickjs(default = "default_text")]
    text: String,
}

fn default_text() -> String {
    "hello".into()
}

Externally tagged enum

#[derive(FromJs)]
enum Enum {
    A(f32),
    B { s: String },
    C,
}

Internally tagged enum

#[derive(FromJs)]
#[quickjs(tag = "tag")]
enum Enum {
    A(f32),
    B { s: String },
    C,
}

Adjacently tagged enum

#[derive(FromJs)]
#[quickjs(tag = "type", content = "data")]
enum Enum {
    A(f32),
    B { s: String },
    C,
}

Untagged unit enum

#[derive(FromJs)]
#[quickjs(untagged)]
enum MyEnum {
    A,
    B,
}

Untagged unit enum with discriminant

#[derive(FromJs)]
#[quickjs(untagged)]
enum MyEnum {
    A = 1,
    B = 2,
}

Externally tagged tuple enum

#[derive(FromJs)]
enum MyEnum {
    Foo(f64, f64),
    Bar(String),
}

Adjacently tagged tuple enum

#[derive(FromJs)]
#[quickjs(tag, content = "data")]
enum MyEnum {
    Foo(f64, f64),
    Bar(String),
}

Untagged tuple enum

#[derive(FromJs)]
#[quickjs(untagged)]
enum MyEnum {
    Foo(f64, f64),
    Bar(String),
}

Internally tagged enum with fields

#[derive(FromJs)]
#[quickjs(tag = "$")]
enum MyEnum {
    Foo { x: f64, y: f64 },
    Bar { msg: String },
}

Internally tagged enum with fields with defaults

#[derive(FromJs)]
#[quickjs(tag = "$")]
enum MyEnum {
    Foo {
        x: f64,
        #[quickjs(default)]
        y: f64,
    },
    Bar {
        #[quickjs(default = "default_msg")]
        msg: String,
    },
}

fn default_msg() -> String {
    "my message".into()
}

Untagged enum with fields

#[derive(FromJs)]
#[quickjs(untagged)]
enum MyEnum {
    Foo { x: f64, y: f64 },
    Bar { msg: String },
}

Generic newtype-like struct

#[derive(FromJs)]
struct Newtype<T>(pub T);

Generic struct with fields

#[derive(FromJs)]
struct MyStruct<T> {
    pub tag: i32,
    pub value: T,
}

Generic enum

#[derive(FromJs)]
enum MyEnum<T> {
    Foo(T),
    Bar { value: T, tag: i32 },
}