#struct #initialization #loadable #syntax #macro #proc-macro #procedural

macro deserter

Procedural macros to initialize Rust structs from JavaScript-like object syntax

5 releases

0.1.4 May 26, 2024
0.1.3 May 26, 2024
0.1.2 May 26, 2024
0.1.1 May 26, 2024
0.1.0 May 26, 2024

#568 in Procedural macros

Download history 13/week @ 2024-05-31 2/week @ 2024-06-28 14/week @ 2024-07-05 26/week @ 2024-07-26 3/week @ 2024-08-02

105 downloads per month

GPL-3.0 license

16KB
159 lines

Deserter

A set of two procedural macros for initializing structs with a JavaScript-like syntax. In other words, deserializing data with compile-time checking into struct values.

Usage

The #[loadable] attribute can be added on structs which can be loaded, and the load! macro can be used to initialize a loadable struct. All fields which are themselves structs must also be marked #[loadable]:

use deserter::{load, loadable};

#[loadable]
struct ZipCode {
	digits: u32,
}

#[loadable]
struct Address {
	house: u32,
	street: &'static str,
	city: &'static str,
	zip_code: ZipCode,
}

#[loadable]
struct Person {
	name: &'static str,
	age: u32,
	address: Address,
}

fn example() {
	let john = load!(   
		Person {
			name = "john",
			age = 30,
			address = {
				house = 101,
				street = "Main Street",
				city = "New York",
				zip_code = {
					digits = 100200
				}
			}
		}
	);

	// do things with john, it is a `Person`.
}

Dependencies

~305–780KB
~18K SLoC