1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use num_enum::{TryFromPrimitive, TryFromPrimitiveError};

#[derive(Debug, PartialEq, Eq)]
pub enum Code<Enum, N> {
    Name(Enum),
    Num(N),
}

impl<Enum, N> From<Result<Enum, TryFromPrimitiveError<Enum>>> for Code<Enum, N>
where
    Enum: TryFromPrimitive<Primitive = N>,
{
    fn from(result: Result<Enum, TryFromPrimitiveError<Enum>>) -> Self {
        match result {
            Ok(e) => Self::Name(e),
            Err(TryFromPrimitiveError { number: n }) => Self::Num(n),
        }
    }
}