In Rust, what is the idiomatic way to associate members of one enum with other enum types and map between their integer and string representations?

Goal:

  • I’d like to turn a pair of (usagePage, usageID) integers to Rust enumeration variants and get the names of both the page and specific usages as strings for debugging purposes.
  • I’d like to define this in the most idiomatic way.
  • I’d like to reuse the enums provided by the usbd_human_interface_device::page module.
  • I’d like this mapping to fail gracefully – that is, given integers out of range of both enums, I’d like to detect that and return strings indicating that the mapping was not found.
    • This failure case could be a failure to look up the usagePage or a failure to look up the usageID within the correct enumeration.

Concrete:

  • I’m using the enum types from usbd_human_interface_device::page, namely Consumer, Desktop, Game and the like.
    • These enums have a numeric primitive association for each variant. For example, Desktop::SystemPowerDown is 129.
  • That library does not provide an enum for each page, so I’ve written HIDPage below.
  • I’d like to turn a pair of integers into a HIDPage variant and the associated page-specific varient, such as Desktop::SystemPowerDown
  • Each of those enum types are used when a HIDPage numeric enum variant specifies to do so. That is, given the tuple of integers (1, 129) I’d like to return (HIDPage::Desktop,Desktop::SystemPowerDown).

Challenges:

  • The type of enumeration must be determined at runtime, as the enum to look up depends on an integer that is not known until then.
    • This implies that the second return value is generic over the set of enums I’d like to return, but I’m not sure how to write that. I tried defining a new trait that each enum implements but I couldn’t get that working.
  • I’d like to return the enum type directly, but I think that’s not possible as enums are not types. /a/72438660/84041 seemed to have a similar question, but I’m not sure how to apply it here.
  • I’d like to not be overly verbose in this mapping – I have a solution below, but it seems overly verbose. Is there a better way to write this?

Abstract:

  • Is this the most idiomatic way to do this in Rust?
// My HIDPage enum implementation
use num_enum::{TryFromPrimitive};

#[repr(u16)]
#[derive(
    Debug,
    Copy,
    Clone,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    Hash,
    TryFromPrimitive,
)]
pub enum HIDPage {
    Desktop = 1,
    Simulation = 2,
    Game = 5,
    Keyboard = 7,
    Telephony = 0x0b,
    Consumer = 0x0c,
}
// This enumeration, plus HIDPage, seem overly verbose
#[derive(Debug)]
pub enum HIDEnum {
    Desktop(Desktop),
    Simulation(Simulation),
    Game(Game),
    Keyboard(Keyboard),
    Telephony(Telephony),
    Consumer(Consumer),
}


fn new_usage_from_ints(page:u16 , id: u16) -> Result<String, Box<dyn Error>> {
    let knownPage: Option<HIDPage> = HIDPage::try_from(page as u16).ok();

    if let Some(knownPage) = knownPage.as_ref() {
        let knownId: Option<HIDEnum> = match knownPage {
            // Each line requires repeating the type (in different contexts) three times
            HIDPage::Desktop => Desktop::try_from(id as u8).ok().map(|v| HIDEnum::Desktop(v)),
            HIDPage::Simulation => Simulation::try_from(id as u8).ok().map(|v| HIDEnum::Simulation(v)),
            HIDPage::Game => Game::try_from(id as u8).ok().map(|v| HIDEnum::Game(v)),
            HIDPage::Keyboard => Keyboard::try_from(id as u8).ok().map(|v| HIDEnum::Keyboard(v)),
            HIDPage::Telephony => Telephony::try_from(id as u8).ok().map(|v| HIDEnum::Telephony(v)),
            HIDPage::Consumer => Consumer::try_from(id as u16).ok().map(|v| HIDEnum::Consumer(v)),
        };
        return Ok(format!("nui {:?}/{:?} (0x{:x}/0x{:x})", knownPage, knownId, page, id));
    }
    return Ok(format!("nui {:?} (0x{:x}/0x{:x})", knownPage, page, id));
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật