On-demand functions using macros


#1

I started to write some simple ORM for test purposes and i’m very interested in ability to make some dynamic code generation that could replace Ruby’s method_missing.

Pseudo-code to illustrate the idea:

impl UserModel {
	pub fn macro_method (find_by, $($field_name:ident)_and_+) (&self, $($field_value:expr),+) -> SelectQuery {
		self.select_all().where_(
			$(
				self.$field_name().is($field_value),
			)	
		)
	}
}

let query = user_model.find_by_id!(100u);
let query = user_model.find_by_first_name_and_last_name!("Luke", "Skywalker");

What do you think about that? I you not interested please close this issue and never mind.


#2

Why not:

  1. Builder style (methods generated with by a compiler plugin): UserModel.where().first_name("Luke").and().last_name("Skywalker")
  2. Macro style: query!(UserModel where first_name is "Luke" and last_name is "Skywalker") (basically typesafe SQL).