Derive Macro telers::FromContext

source ·
#[derive(FromContext)]
{
    // Attributes available to this derive:
    #[context]
}
Expand description

Derive an implementation of FromEventAndContext for the given type.

This macro supports the following attributes:

  • #[context(key = "...")] - the key by which the type will be extracted from context.
  • #[context(into = "...")] - the type into which the type will be converted.
  • #[context(from = "...")] - the type from which the type will be converted.
  • #[context(description = "...")] - the description of the type in context.
    This attribute is used only for documentation purposes and perhaps for debugging.

Check the examples below to see how to use this macro and what types of deriving are supported.

§Whole struct by key in context

use telers_macros::FromContext;

#[derive(Clone, FromContext)]
#[context(key = "my_struct")]
struct MyStruct {
  field: i32,
}

async fn handler(my_struct: MyStruct) {
 // ...
}

§Whole enum by key in context

use telers_macros::FromContext;

#[derive(Clone, FromContext)]
#[context(key = "my_enum")]
enum MyEnum {
 Variant1,
 Variant2,
}

async fn handler(my_enum: MyEnum) {
 // ...
}

§Whole struct that can be converted from another one type that is in context by key

You need to implement From/Into trait for your type by yourself. This can be useful when you want to wrap your type to another one or if the type in context is a foreign type, and you want to convert it to your own type to use it in handler (because you can’t implement a foreign trait for a foreign type).

use telers_macros::FromContext;

#[derive(Clone, FromContext)]
#[context(key = "my_struct", into = MyStructWrapper)]
struct MyStruct {
 field: i32,
}

struct MyStructWrapper(MyStruct);

impl From<MyStruct> for MyStructWrapper {
 fn from(my_struct: MyStruct) -> Self {
  Self(my_struct)
 }
}

You can also use #[context(from = "...")] attribute to specify the type from which the type will be converted.

use telers_macros::FromContext;

#[derive(Clone)]
struct MyStruct {
 field: i32,
}

#[derive(FromContext)]
#[context(key = "my_struct", from = MyStruct)]
struct MyStructWrapper(MyStruct);

impl From<MyStruct> for MyStructWrapper {
 fn from(my_struct: MyStruct) -> Self {
  Self(my_struct)
 }
}

§Whole enum that can be converted from another one type that is in context by key

You need to implement From/Into trait for your type by yourself. This can be useful when you want to wrap your type to another one or if the type in context is a foreign type, and you want to convert it to your own type to use it in handler (because you can’t implement a foreign trait for a foreign type).

use telers_macros::FromContext;

#[derive(Clone, FromContext)]
#[context(key = "my_enum", into = MyEnumWrapper)]
enum MyEnum {
 Variant1,
 Variant2,
}

struct MyEnumWrapper(MyEnum);

impl From<MyEnum> for MyEnumWrapper {
 fn from(my_enum: MyEnum) -> Self {
  Self(my_enum)
 }
}

You can also use #[context(from = "...")] attribute to specify the type from which the type will be converted.

use telers_macros::FromContext;

#[derive(Clone)]
enum MyEnum {
 Variant1,
 Variant2,
}

#[derive(FromContext)]
#[context(key = "my_enum", from = MyEnum)]
struct MyEnumWrapper(MyEnum);

impl From<MyEnum> for MyEnumWrapper {
 fn from(my_enum: MyEnum) -> Self {
  Self(my_enum)
 }
}