alloy-sol-types 1.7.1

Compile-time ABI and EIP-712 implementations
Documentation
use crate::{
    Result, SolType, Word,
    abi::{AbiDecoderConfig, token::WordToken},
};
use alloc::vec::Vec;

/// A Solidity enum. This is always a wrapper around a [`u8`].
///
/// # Implementer's Guide
///
/// It should not be necessary to implement this trait manually. Instead, use
/// the [`sol!`](crate::sol!) procedural macro to parse Solidity syntax into
/// types that implement this trait.
pub trait SolEnum: Sized + Copy + Into<u8> + TryFrom<u8, Error = crate::Error> {
    /// The number of variants in the enum.
    ///
    /// This is generally between 1 and 256 inclusive.
    const COUNT: usize;

    /// Tokenize the enum.
    #[inline]
    fn tokenize(self) -> WordToken {
        WordToken(Word::with_last_byte(self.into()))
    }

    /// ABI decode the enum from the given buffer.
    #[inline]
    fn abi_decode(data: &[u8]) -> Result<Self> {
        <crate::sol_data::Uint<8> as SolType>::abi_decode(data).and_then(Self::try_from)
    }

    /// ABI-decodes the enum with a custom decoder configuration.
    #[inline]
    fn abi_decode_with_config(data: &[u8], config: AbiDecoderConfig) -> Result<Self> {
        <crate::sol_data::Uint<8> as SolType>::abi_decode_with_config(data, config)
            .and_then(Self::try_from)
    }

    /// ABI decode the enum from the given buffer, with validation.
    ///
    /// This is the same as [`abi_decode`](Self::abi_decode), as validation
    /// is inherent in the `TryFrom<u8>` conversion.
    #[inline]
    // TODO: Deprecate in favor of a validating decoder configuration.
    // #[deprecated(note = "use a validating decoder configuration")]
    fn abi_decode_validate(data: &[u8]) -> Result<Self> {
        Self::abi_decode_with_config(data, AbiDecoderConfig::new().validate(true))
    }

    /// ABI encode the enum into the given buffer.
    #[inline]
    fn abi_encode_raw(self, out: &mut Vec<u8>) {
        out.extend(self.tokenize().0);
    }

    /// ABI encode the enum.
    #[inline]
    fn abi_encode(self) -> Vec<u8> {
        self.tokenize().0.to_vec()
    }
}