Crates.io | define_into_enum |
lib.rs | define_into_enum |
version | 0.1.1 |
source | src |
created_at | 2023-01-08 05:04:26.825727 |
updated_at | 2023-01-08 05:07:42.2741 |
description | Defines Into |
homepage | |
repository | https://github.com/ctrlcctrlv/define_into_enum.rlib |
max_upload_size | |
id | 753418 |
size | 16,055 |
define_into_enum!
v0.1.1This defines a single macro, define_into_enum!
, which is meant to be compile as e.g.:
use define_into_enum::define_into_enum;
define_into_enum! {
#[derive(Debug, PartialEq)]
pub enum TestEnum {
Variant1((usize, usize)),
Variant2((usize, usize)),
Variant3((usize, usize)),
}
(v1, v2),
(v1, v2)
}
And then it will impl Into<(usize, usize)> for TestEnum
, converting all (v1, v2)
to (v1, v2)
.
Works with meta-tags as well:
define_into_enum! {
#[non_exhaustive]
#[derive(enum_kinds::EnumKind)]
#[enum_kind(WordKind, derive(IsVariant, Unwrap))]
#[derive(Clone, Debug, PartialEq, Eq, IsVariant, Unwrap)]
pub enum Word<'a> {
SimpleWord(Cow<'a, str>),
SingleQuotedWord(Cow<'a, str>),
DoubleQuotedWord(Cow<'a, str>),
DollarSingleQuotedWord(Cow<'a, str>),
DollarDoubleQuotedWord(Cow<'a, str>),
UnquotedWord(Cow<'a, str>),
ErrorWord(Cow<'a, str>),
}
( s1 ),
( s1 )
}
macro_rules! define_into_enum {
{
$(#[$attr:meta])*
$vis:vis enum $name:ident $(< $generic:lifetime >)* {
$(#[$variant_attr1:meta])*
$variant1:ident ($type1:ty),
$(
$(#[$variant_attr:meta])*
$variant:ident ($type:ty),
)*
}
$into:pat,
$output:expr
} => {
$(#[$attr])*
$vis enum $name $(<$generic>)* {
$(#[$variant_attr1])*
$variant1($type1),
$(
$(#[$variant_attr])*
$variant($type),
)*
}
impl$(<$generic>)* Into <$type1> for $name $(<$generic>)* {
fn into(self) -> $type1 {
match self {
$(
Self::$variant( $into ) => ( $output ),
)+
Self::$variant1( $into ) => ( $output ),
}
}
}
}
}
Copyright 2023 Fredrick R. Brennan
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.