revive_solc_json_interface/combined_json/
selector.rs

1//! The `solc --combined-json` expected output selection flag.
2
3use std::str::FromStr;
4
5use serde::{Deserialize, Serialize};
6
7/// The solc `--combind-json` invalid selector message.
8pub const MESSAGE_SELECTOR_INVALID: &str = "Invalid option to --combined-json";
9
10/// The `solc --combined-json` expected output selection flag.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12pub enum Selector {
13    /// The ABI JSON.
14    #[serde(rename = "abi")]
15    ABI,
16    /// The function signature hashes JSON.
17    #[serde(rename = "hashes")]
18    Hashes,
19    /// The metadata.
20    #[serde(rename = "metadata")]
21    Metadata,
22    /// The developer documentation.
23    #[serde(rename = "devdoc")]
24    Devdoc,
25    /// The user documentation.
26    #[serde(rename = "userdoc")]
27    Userdoc,
28    /// The storage layout.
29    #[serde(rename = "storage-layout")]
30    StorageLayout,
31    /// The transient storage layout.
32    #[serde(rename = "transient-storage-layout")]
33    TransientStorageLayout,
34    /// The AST JSON.
35    #[serde(rename = "ast")]
36    AST,
37    /// The EVM assembly.
38    #[serde(rename = "asm")]
39    ASM,
40
41    /// The assembly.
42    #[serde(rename = "assembly", skip_serializing)]
43    Assembly,
44
45    /// The deploy bytecode.
46    #[serde(rename = "bin", skip_serializing)]
47    Bytecode,
48    /// The runtime bytecode.
49    #[serde(rename = "bin-runtime", skip_serializing)]
50    BytecodeRuntime,
51}
52
53impl Selector {
54    /// Converts the comma-separated CLI argument into an array of flags.
55    pub fn from_cli(format: &str) -> Vec<anyhow::Result<Self>> {
56        format
57            .split(',')
58            .map(|flag| Self::from_str(flag.trim()))
59            .collect()
60    }
61
62    /// Whether the selector is available in `solc`.
63    pub fn is_source_solc(&self) -> bool {
64        !matches!(
65            self,
66            Self::Assembly | Self::Bytecode | Self::BytecodeRuntime
67        )
68    }
69}
70
71impl FromStr for Selector {
72    type Err = anyhow::Error;
73
74    fn from_str(string: &str) -> Result<Self, Self::Err> {
75        match string {
76            "abi" => Ok(Self::ABI),
77            "hashes" => Ok(Self::Hashes),
78            "metadata" => Ok(Self::Metadata),
79            "devdoc" => Ok(Self::Devdoc),
80            "userdoc" => Ok(Self::Userdoc),
81            "storage-layout" => Ok(Self::StorageLayout),
82            "transient-storage-layout" => Ok(Self::TransientStorageLayout),
83            "ast" => Ok(Self::AST),
84            "asm" => Ok(Self::ASM),
85
86            "bin" => Ok(Self::Bytecode),
87            "bin-runtime" => Ok(Self::BytecodeRuntime),
88
89            "assembly" => Ok(Self::Assembly),
90
91            selector => anyhow::bail!("{MESSAGE_SELECTOR_INVALID}: {selector}"),
92        }
93    }
94}
95
96impl std::fmt::Display for Selector {
97    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
98        match self {
99            Self::ABI => write!(f, "abi"),
100            Self::Hashes => write!(f, "hashes"),
101            Self::Metadata => write!(f, "metadata"),
102            Self::Devdoc => write!(f, "devdoc"),
103            Self::Userdoc => write!(f, "userdoc"),
104            Self::StorageLayout => write!(f, "storage-layout"),
105            Self::TransientStorageLayout => write!(f, "transient-storage-layout"),
106            Self::AST => write!(f, "ast"),
107            Self::ASM => write!(f, "asm"),
108
109            Self::Bytecode => write!(f, "bin"),
110            Self::BytecodeRuntime => write!(f, "bin-runtime"),
111
112            Self::Assembly => write!(f, "assembly"),
113        }
114    }
115}