macro_rules! field_property {
    ($field:ident) => { ... };
    ($conversion:ident, $field:ident) => { ... };
    ($conversion:ident, $field:ident, $b:block) => { ... };
}
Expand description

Helper for making property resolver functions based on fields.

Generally used with resolve_property_with.

Retrieves a FieldValue from a vertex by converting it to the proper type, and then retrieving the field of a struct.

If the property is computed by a function, use accessor_property! instead.

Examples

#[derive(Debug, Clone)]
struct User {
    id: String
    // ...
}

// In implementation of `BasicAdapter`
fn resolve_property(
    // &mut self,
    contexts: ContextIterator<'static, User>,
    type_name: &str,
    property_name: &str,
) -> ContextOutcomeIterator<'static, User, FieldValue> {
    match (type_name, property_name) {
        ("User", "id") => {
            resolve_property_with(contexts, field_property!(id)) // Macro used here
        },
        // ...
        _ => unreachable!()
    }
}

Sometimes a vertex may have to be converted to another type before the property can be accessed. To do this, simply pass a conversion method implemented on the Vertex type (in this case as_user) to the macro like in the example below.

#[derive(Debug, Clone)]
struct User {
    id: String,
    // ...
}

#[derive(Debug, Clone)]
struct Bot {
    user: User,
    purpose: String,
}

#[derive(Debug, Clone)]
enum Vertex {
    UserVertex(Rc<User>),
    BotVertex(Rc<Bot>),
    // ...
}

impl Vertex {
    pub fn as_user(&self) -> Option<&User> {
        match self {
            Vertex::UserVertex(u) => Some(u.as_ref()),
            Vertex::BotVertex(b) => Some(&b.user),
            _ => None,
        }
    }
    // ...
}

// In implementation of `BasicAdapter`
("User" | "Bot", "id") => {
    resolve_property_with(contexts, field_property!(as_user, id)) // Macro used here
},

It is also possible to pass a code block to additionally handle the property.