jget

Crates.iojget
lib.rsjget
version0.4.1
sourcesrc
created_at2024-03-19 11:45:23.05404
updated_at2024-04-04 04:22:15.540187
descriptionRust tools.
homepagehttps://github.com/caizhengxin/jppe-rs/tree/master/crates/jget-rs
repositoryhttps://github.com/caizhengxin/jppe-rs/tree/master/crates/jget-rs
max_upload_size
id1179172
size20,990
jankincai (caizhengxin)

documentation

README

jget

Crates.io Crates.io License

Cargo.toml

[dependencies]
jget = { version = "0.4.1", features = ["derive"] }

Or

[dependencies]
jppe = { version = "0", features = ["derive", "jget"] }

Usage

Simple example

use jget::Jget;


#[derive(Debug, Jget)]
pub enum SimpleEnumExample {
    Read {
        #[jget(get_option)]
        value: u8,
    },
    Write {
        #[jget(get_option)]
        value: u8,
        #[jget(get_option)]
        data: u16,
    }
}


fn main() {
    let value = SimpleEnumExample::Read { value: 1 };
    assert_eq!(value.get_value(), Some(1));
    assert_eq!(value.get_data(), None);

    let value = SimpleEnumExample::Write { value: 1, data: 2 };
    assert_eq!(value.get_value(), Some(1));
    assert_eq!(value.get_data(), Some(2));
}

Struct/Enum example

use jget::Jget;


#[derive(Debug, Jget)]
pub struct SimpleStructExample {
    #[jget(get="body.get_value():Option<u8>, body.get_data():Option<u16>")]
    pub body: SimpleEnumExample,
}


#[derive(Debug, Jget)]
pub enum SimpleEnumExample {
    Read {
        #[jget(get_option)]
        value: u8,
    },
    Write {
        #[jget(get_option)]
        value: u8,
        #[jget(get_option)]
        data: u16,
    }
}


fn main() {
    let value = SimpleStructExample{ body: SimpleEnumExample::Read { value: 1 } };
    assert_eq!(value.get_value(), Some(1));
    assert_eq!(value.get_data(), None);

    let value = SimpleStructExample { body: SimpleEnumExample::Write { value: 1, data: 2 }};
    assert_eq!(value.get_value(), Some(1));
    assert_eq!(value.get_data(), Some(2));
}

Enum/Struct

use jget_derive::Jget;


#[derive(Debug, PartialEq, Eq, Jget)]
pub struct SimpleStructExample {
    pub value: u16,
}


#[derive(Debug, PartialEq, Eq, Jget)]
pub struct SimpleStructExample2 {
    pub value: u16,
    pub data: u16,
}


#[derive(Debug, PartialEq, Eq, Jget)]
pub enum SimpleEnumExample {
    #[jget(get="value:u16")]
    V1(SimpleStructExample),
    #[jget(get="value:u16", get_option="data:Option<u16>")]
    V2(SimpleStructExample2),
}


#[test]
fn test_enum_struct() {
    let value = SimpleEnumExample::V2(SimpleStructExample2 { value: 1, data: 2 });
    assert_eq!(value.get_value(), 1);
    assert_eq!(value.get_data(), Some(2));
}
Commit count: 117

cargo fmt