#[derive(ContextMapper)]
{
// Attributes available to this derive:
#[context_mapper]
#[context_attribute]
}
Expand description
Generate mapping with the different contexts
§General usage
NOTE This is NOT a working example, just a pseudocode
§Mappers configuration
#[derive(ContextMapper)]
#[context_mapper(
/// Indicates that mapper will be using a trait
trait(
// Name of the context. Defaults to `default`
context = path::of::the::context::default,
// Result type of the mapping. Defaults to `std::string::String`
type = usize,
// Conversion function/method. Defaults to `std::string::ToString`
converter = my_function_with_proper_signature,
// Indicates how the type should be passed to the function. Flag
// If false (or not defined): passed by reference
// Else: value is moved
move
/// Traits require either simple or generic naming
simple(
path = path::to::the::trait,
method_name = name_of_the_method
)
generic(
path = path::to::the::trait,
method_name = name_of_the_method
)
)
function(
context = path::of::the::context::default,
type = usize,
converter = my_function_with_proper_signature,
move
/// Name of the generated function. Both are identical
naming = my_new_function
fn = my_new_function
/// Optional. Visibility of the generated function. Both are identical
vis = pub(crate)
visibility = pub(crate)
)
impls(
context = path::of::the::context::default,
type = usize,
converter = my_function_with_proper_signature,
move
// As in `function`
naming = my_new_function
fn = my_new_function
/// As in `function`
vis = pub(crate)
visibility = pub(crate)
)
)]
§Attribtues configuration
#[context_attribute(
context(
name = path::to::the::context,
converter = you::can::override::Mapper::function,
/// You can change mapper key for the certain context
rename = "new mapper key",
/// Indicates that field should be skipped in the given context. Flag
skip,
/// You can override context `move` flag
move
)
)]
§Basic e xample
use context_mapper::ContextMapper;
use context_mapper::IntoType;
#[derive(ContextMapper, Serialize)]
#[context_mapper(trait())]
#[context_mapper(
trait(
context=other_context,
type=usize,
converter=my_function,
)
)]
pub struct X {
#[context_attribute(
context(
name=other_context,
converter=my_other_function,
rename="my_other_name",
)
)]
pub x: String,
#[context_attribute(
context(
name=other_context,
move
)
)]
pub y: usize,
pub y0: i32,
#[context_attribute(context(name=other_context, skip))]
pub z: i32
}
That generates the following
impl ::context_mapper::IntoType<std::string::String> for X {
fn into_type_map(&self) -> std::collections::HashMap<String, std::string::String> {
let mut result = std::collections::HashMap::new();
result.insert("x".to_string(), std::string::ToString::to_string(&self.x));
result.insert("y".to_string(), std::string::ToString::to_string(&self.y));
result.insert("y0".to_string(), std::string::ToString::to_string(&self.y0));
result.insert("z".to_string(), std::string::ToString::to_string(&self.z));
result
}
}
impl ::context_mapper::IntoType<usize> for X {
fn into_type_map(&self) -> std::collections::HashMap<String, usize> {
let mut result = std::collections::HashMap::new();
result.insert("my_other_name".to_string(), my_other_function(&self.x));
result.insert("y".to_string(), my_function(self.y));
result.insert("y0".to_string(), my_function(&self.y0));
result
}
}