revm_bytecode/opcode/
parse.rs

1use super::OpCode;
2use crate::opcode::NAME_TO_OPCODE;
3use core::fmt;
4
5/// An error indicating that an opcode is invalid
6#[derive(Debug, PartialEq, Eq)]
7pub struct OpCodeError(());
8
9impl fmt::Display for OpCodeError {
10    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11        f.write_str("invalid opcode")
12    }
13}
14
15impl core::error::Error for OpCodeError {}
16
17impl core::str::FromStr for OpCode {
18    type Err = OpCodeError;
19
20    #[inline]
21    fn from_str(s: &str) -> Result<Self, Self::Err> {
22        Self::parse(s).ok_or(OpCodeError(()))
23    }
24}
25
26impl OpCode {
27    /// Parses an opcode from a string.
28    ///
29    /// This is the inverse of [`as_str`](Self::as_str).
30    #[inline]
31    pub fn parse(s: &str) -> Option<Self> {
32        NAME_TO_OPCODE.get(s).copied()
33    }
34}