revive_common/
metadata.rs

1//! The metadata hash type.
2
3use std::str::FromStr;
4
5use serde::{Deserialize, Serialize};
6
7/// The metadata hash type.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9pub enum MetadataHash {
10    /// Do not include bytecode hash.
11    #[serde(rename = "none")]
12    None,
13    /// Include the `ipfs` hash.
14    #[serde(rename = "ipfs")]
15    IPFS,
16    /// Include the `keccak256`` hash.
17    #[serde(rename = "keccak256")]
18    Keccak256,
19}
20
21impl FromStr for MetadataHash {
22    type Err = anyhow::Error;
23
24    fn from_str(string: &str) -> Result<Self, Self::Err> {
25        match string {
26            "none" => Ok(Self::None),
27            "ipfs" => Ok(Self::IPFS),
28            "keccak256" => Ok(Self::Keccak256),
29            string => anyhow::bail!("unknown bytecode hash mode: `{string}`"),
30        }
31    }
32}
33
34impl std::fmt::Display for MetadataHash {
35    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
36        match self {
37            Self::None => write!(f, "none"),
38            Self::IPFS => write!(f, "ipfs"),
39            Self::Keccak256 => write!(f, "keccak256"),
40        }
41    }
42}