revive_solc_json_interface/standard_json/input/
source.rs

1//! The `solc --standard-json` input source.
2
3use std::io::Read;
4use std::path::Path;
5
6use serde::Deserialize;
7use serde::Serialize;
8
9/// The `solc --standard-json` input source.
10#[derive(Clone, Debug, Serialize, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct Source {
13    /// The source code file content.
14    pub content: String,
15}
16
17impl From<String> for Source {
18    fn from(content: String) -> Self {
19        Self { content }
20    }
21}
22
23impl TryFrom<&Path> for Source {
24    type Error = anyhow::Error;
25
26    fn try_from(path: &Path) -> Result<Self, Self::Error> {
27        let content = if path.to_string_lossy() == "-" {
28            let mut solidity_code = String::with_capacity(16384);
29            std::io::stdin()
30                .read_to_string(&mut solidity_code)
31                .map_err(|error| anyhow::anyhow!("<stdin> reading error: {}", error))?;
32            solidity_code
33        } else {
34            std::fs::read_to_string(path)
35                .map_err(|error| anyhow::anyhow!("File {:?} reading error: {}", path, error))?
36        };
37
38        Ok(Self { content })
39    }
40}