Derive Macro trustfall_derive::TrustfallEnumVertex
source · #[derive(TrustfallEnumVertex)]
{
// Attributes available to this derive:
#[trustfall]
}
Expand description
Adds the Typename
trait and as_<variant>()
methods on an enum used as a Trustfall vertex.
For example:
#[derive(Debug, Clone, TrustfallEnumVertex)]
enum Vertex {
User(String),
Message { author: String, content: String },
EmptyVariant,
}
will get the following implementations:
impl Typename for Vertex {
fn typename(&self) -> &'static str {
match self {
Self::User { .. } => "User",
Self::Message { .. } => "Message",
Self::EmptyVariant { .. } => "EmptyVariant",
}
}
}
impl Vertex {
fn as_user(&self) -> Option<&String> {
match self {
Self::User(x) => Some(x),
_ => None,
}
}
fn as_message(&self) -> Option<(&String, &String)> {
match self {
Self::Message { author, content } => Some((author, content)),
_ => None,
}
}
fn as_empty_variant(&self) -> Option<()> {
match self {
Self::EmptyVariant => Some(()),
_ => None,
}
}
}
A variant can opt out of having a generated conversion method using
the #[trustfall(skip_conversion)]
attribute.
In this example, only the User
variant gets a conversion method:
#[derive(Debug, Clone, TrustfallEnumVertex)]
enum Vertex {
User(String),
#[trustfall(skip_conversion)]
Message { author: String, content: String },
}
To add only the Typename
implementation without the as_<variant>()
conversions,
use the Typename
derive macro instead.