Rusteceans of the Fediverse, can you help me with a declarative macro problem?
-
@slatian something like this?
pub trait Table {
fn sql_safe_table_name(&self) -> &'static str;
}
pub trait Field {
fn sql_safe_field_name(&self) -> &'static str;
}
macro_rules! sql_schema {
{
$schema_name:ident,
$(
enum $table_name:ident = $table_value:literal {
$( $field_name:ident = $field_value:literal ),*
}
)*
} => {
pub enum $schema_name {
$( $table_name ),*
}
impl Table for $schema_name {
fn sql_safe_table_name(&self) -> &'static str {
match self {
$( $schema_name::$table_name => $table_value ),*
}
}
}
$(
// it's not possible to concatenate things to identifiers without the use of a proc macro
// so we just re-use $table_name
pub enum $table_name {
$( $field_name ),*
}
impl Field for $table_name {
fn sql_safe_field_name(&self) -> &'static str {
match self {
$( $table_name::$field_name => $field_value ),*
}
}
}
)*
}
}
sql_schema! {
BaseTable,
enum MimeParameter = "mime_parameter" {
Key = "key",
MimeParameterId = "mime_parameter_id"
}
enum Mimetype = "mimetype" {
Charset = "charset"
// …
}
}you can use
cargo expandto see what it generates -
@steffo@a.junimo.party It took a while for me to wrap my Brain around this :drgn_sparkle: <- didn't expect it to be that … simple and especially readable.
Given that I can almost 1:1 copy this into criterium (licensed
LGPL-3.0-only): May I? If yes, what name do you want in the copyright header? -
@slatian sure! you can add
Stefano Pigozzi <me@steffo.eu>to thecontributorssection ofCargo.tomlif you want, but dw, it's just a snippet of code that fit in a fedi post
-
@slatian a thing to be aware of,
$(),*does not allow trailing commas, so this would workenum Mimetype = "mimetype" {
Charset = "charset"
}but this would give a weird error
enum Mimetype = "mimetype" {
Charset = "charset",
} -
The optional comma could just be eaten by a
$(…),* $(,)?Or am I missing something? (I don't write a lot of macros.)
-
@slatian yep
-
Questo post è eliminato! -
Questo post è eliminato! -
@slatian wait no i remembered correctly
-
This is what I ended up with:
https://codeberg.org/unobtanium/criterium/src/branch/main/src/sql/schema_macro.rs
Huge Thank You goes to @steffo@a.junimo.party :drgn_heart:
-
S steffo@a.junimo.party shared this topic on