Skip to content

Create PrimaryKey, etc., derive macro for enums, maybe some other simple types. #28

@0xForerunner

Description

@0xForerunner

Here is an example implementation for an enum. This would lend itself very well to a derive macro.

/// Unique addresses
#[derive(Copy, Display, FromPrimitive)]
#[cw_serde]
pub enum ConfigUnique {
    PriceOracle,
    Market,
    InterestModel,
    WarChest,
    Governance,
    AdminDoubleSig,
    AdminTripleSig,
    Owner,
}

impl<'a> PrimaryKey<'a> for ConfigUnique {
    type Prefix = ();
    type SubPrefix = ();
    type Suffix = Self;
    type SuperSuffix = Self;

    fn key(&self) -> Vec<Key> {
        vec![Key::Val8([*self as u8])]
    }
}

impl KeyDeserialize for ConfigUnique {
    type Output = ConfigUnique;

    #[inline(always)]
    fn from_vec(value: Vec<u8>) -> StdResult<Self::Output> {
        FromPrimitive::from_u8(value[0]).ok_or_else(|| StdError::generic_err("Invalid ConfigUnique"))
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions