Macro trustfall_core::accessor_property
source · macro_rules! accessor_property { ($accessor:ident) => { ... }; ($accessor:ident($($arg:expr),* $(,)?)) => { ... }; ($conversion:ident, $accessor:ident) => { ... }; ($conversion:ident, $accessor:ident($($arg:expr),* $(,)?)) => { ... }; ($conversion:ident, $accessor:ident, $b:block) => { ... }; ($conversion:ident, $accessor:ident($($arg:expr),* $(,)?), $b:block) => { ... }; }
Expand description
Helper for making property resolver functions based on accessor methods.
In principle exactly the same as field_property!
,
but where the property is to be accessed using an accessor function instead
of as a field.
Examples
In the following example, name
would be accessed using a field, but the
age is accessed using a function:
#[derive(Debug, Clone)]
struct User {
id: String
// ...
}
impl User {
pub fn age(&self) -> u8 {
// Some calculation
age
}
}
// 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)),
("User", "age") => resolve_property_with(contexts, accessor_property!(age)),
// ...
_ => unreachable!()
}
}
If the function to be called requires additional arguments, they can be specified as part of naming the function and the argument values will be moved into the generated closure.
#[derive(Debug, Clone)]
struct User {
// ...
}
impl User {
pub fn age(&self, current_year: i64) -> i64 {
// Some calculation
age
}
}
// 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", "age") => resolve_property_with(contexts, accessor_property!(age(2024))),
// ...
_ => unreachable!()
}
}
The usage of conversion functions and possible extra processing with a code
block is analogous to the ones used with
field_property!
.