#structs #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

#653 in Procedural macros

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

~280–740KB
~17K SLoC