Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse the whole thrift definition using rust macros? #12

Closed
jhorstmann opened this issue Jun 12, 2024 · 1 comment
Closed

Parse the whole thrift definition using rust macros? #12

jhorstmann opened this issue Jun 12, 2024 · 1 comment

Comments

@jhorstmann
Copy link
Owner

Crazy idea, but the thrift grammar should consist of valid rust tokens, so should not even need procedural macros. Proof of concept:

fn skip_field(field_type: u8) {
    
}

macro_rules! thrift {
    (struct $identifier:ident { $($(#[$($attrss:tt)*])* $field_id:literal : $required_or_optional:ident $field_type:ident $(< $element_type:ident >)? $field_name:ident,)* }) => {
        pub struct $identifier {
            $($(#[$($attrss)*])* pub $field_name: required_or_optional!($required_or_optional field_type!($field_type $($element_type)?))),*
        }
        
        impl $identifier {
            pub fn fill(&mut self) {
                let mut last_field_id = 0_i16;
                loop {
                    let field_type = 1_u8; // TODO
                    if field_type == 0 {
                        break;
                    }

                    match last_field_id {
                        $($field_id => {
                            self.$field_name.fill();
                        }),*
                        _ => {
                            skip_field(field_type);
                        }
                    }
                }
            }
        }
    }
}

macro_rules! field_type {
    (list $element_type:ident) => { Vec<$element_type> };
    (set $element_type:ident) => { Vec<$element_type> };
    (binary) => { Vec<u8> };
    (string) => { String };
    ($field_type:ident) => { $field_type };
}

macro_rules! required_or_optional {
    (required $field_type:ty) => { $field_type };
    (optional $field_type:ty) => { Option<$field_type> };
}

thrift! {
    struct SomeStructure {
        /** doc */
        1: required i64 offset,
        2: optional i64 length,
        3: optional list<i64> foobar,
        4: optional string data,
    }
}
@jhorstmann
Copy link
Owner Author

Benefits:

  • Avoid separate code generation step
  • Can customize the generated structs relatively easily, for example renaming/removing fields or changing types
  • Relatively easy to update thrift definitions (depending on the amount of customization)

Limitations

  • Error messages for typos in the idl might be quite complex
  • Currently not possible to prefix/suffix names of local variables, might clash with method parameters ([concat_idents](Tracking issue for concat_idents rust-lang/rust#29599 is experimental)
  • All fields need explicit numeric identifiers, no automatic numbering (this is good practice anyway)
  • Lifetime annotations need to be added to the struct definitions when needed, depending on the customized types of the fields

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant